School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,153 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,651 people online right now. Registration is fast and FREE... Join Now!




2d shooter - shooting bullets [sdl + opengl]

 

2d shooter - shooting bullets [sdl + opengl]

Rohtie

19 Apr, 2009 - 06:16 AM
Post #1

New D.I.C Head
*

Joined: 7 Mar, 2007
Posts: 35


My Contributions
I have searched alot on google about how to make a bullet that goes with the angle of my turret.
This is the code i have for the bullet... There is something very wrong with it, the bullet seems like it wants to fly to somewhere random. I use sdl + opengl.
CODE

Bullet::Bullet(){
    speed = 2;
    //velocity
    xvel = 0;
    yvel = 0;
    //mouse coordinates
    mx = 0;
    my = 0;
    //offset
    x = SCREEN_HEIGHT/2;
    y = SCREEN_WIDTH/2;
    hyp = 0;
    shoot = 0;
    life = 100;
}

void Bullet::Hevent(){
    //If the mouse moved

    if( event.type == SDL_MOUSEMOTION ) {

    //Get the mouse offsets

    mx = event.motion.x;

    my = event.motion.y;
    }
    //if left mouse button was pressed
    if(event.type == SDL_MOUSEBUTTONDOWN){
    if(event.button.button == SDL_BUTTON_LEFT){
        //hyp = sqrt(pow(mx-x,2)+pow(my-y,2);
        //debug
        printf ("x: %d y: %d mousex: %d mousey: %d Angle: %f \n", x, y, mx, my, Angle);
        shoot = 1;
    }}
}

void Bullet::move(){
    int Radians;
    if(shoot == 1){
        Radians = Angle * PI / 180;
        xvel = speed * cos(Radians);
        yvel = speed * sin(Radians);
        x -= xvel;
        y -= yvel;

/*        xvel = floor(x * speed / hyp);
        yvel = floor(y * speed / hyp);
        x -= xvel;
        y -= yvel;*/
        life--;
    }
    if(life == 0){
        shoot = 0;
        life = 100;
    }
}

void Bullet::show(){
    //Move to offset

    glTranslatef( x, y, 0 );


    //Start quad

    glBegin( GL_QUADS );



    //Set color to white

        glColor4f( 1.0, 1.0, 1.0, 1.0 );



        //Draw bullet

        glVertex3f( 0, 0, 0 );

        glVertex3f( 5, 0, 0 );

        glVertex3f( 5, 5, 0 );

        glVertex3f( 0, 5, 0 );



    //End quad

    glEnd();



    //Reset

    glLoadIdentity();
}


It would be helpful if you give me some mathematic formulas or something simelar.

This post has been edited by Rohtie: 19 Apr, 2009 - 06:17 AM

User is offlineProfile CardPM
+Quote Post



Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:50PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month