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

Welcome to Dream.In.Code
Become an Expert!

Join 306,817 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,703 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


e_barroga

RE: 2d Shooter - Shooting Bullets [sdl + Opengl]

19 Apr, 2009 - 09:27 PM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2009
Posts: 435



Thanked: 24 times
Dream Kudos: 825
My Contributions
The only thing you really need to keep track of is the "Angle" and "Speed". Once you've got those two variables, you plug them into a formula and simply change the X and Y values.

CODE

void MoveBullet() {
    x += std::cos( Direction*( PI/180 ) ) * Speed;
    y += std::sin( Direction*( PI/180 ) ) * Speed;
}

User is offlineProfile CardPM
+Quote Post

Rohtie

RE: 2d Shooter - Shooting Bullets [sdl + Opengl]

20 Apr, 2009 - 09:50 AM
Post #3

New D.I.C Head
*

Joined: 7 Mar, 2007
Posts: 35


My Contributions
Thanks alot! This solved my problem smile.gif
I have one more problem... How do I, or what is the best way to shoot multiple bullets?

This post has been edited by Rohtie: 20 Apr, 2009 - 09:56 AM
User is offlineProfile CardPM
+Quote Post

e_barroga

RE: 2d Shooter - Shooting Bullets [sdl + Opengl]

20 Apr, 2009 - 09:53 AM
Post #4

D.I.C Regular
Group Icon

Joined: 16 Feb, 2009
Posts: 435



Thanked: 24 times
Dream Kudos: 825
My Contributions
The best way to shoot multiple bullets would be to first create a "Bullet" class.

Then, create a container (array, vector, etc.) that stores "Bullet" objects.

In the "shoot event", create a bullet object and store the reference into your container.

Create a loop that updates, draws, etc. all of the bullet instances stored in the container.

Regards,
Emmanuel Barroga
User is offlineProfile CardPM
+Quote Post

Rohtie

RE: 2d Shooter - Shooting Bullets [sdl + Opengl]

21 Apr, 2009 - 10:44 AM
Post #5

New D.I.C Head
*

Joined: 7 Mar, 2007
Posts: 35


My Contributions
Thanks for your directions. I tried to do something simelar, but something went wrong. A lot of bullets appear at random places in my window when I shoot for the first time.
CODE
class bullet{
    private:
    int container[128][3];
    int speed, id;

    public:
    bullet();
    void create();
    void move();
    void draw();
};

bullet::bullet(){
    speed = 5;
    id = 0;
}

void bullet::create(){
    if(docreate == 1){
        container[id][0] = bx;
        container[id][1] = by;
        container[id][2] = dir;
        printf("id: %d, bx: %d, by: %d, direction: %d",id,bx,by,dir);
    }
}

void bullet::move(){
    if(id > 0 || docreate == 1){
        for(int l=0; l < id; l++){
            container[l][0] += cos(container[l][2] * (PI/180)) * speed;
            container[l][1] += sin(container[l][2] * (PI/180)) * speed;
        }
    }
}

void bullet::draw(){
    if(id > 0 || docreate == 1){
        for(int l=0; l < id; l++){
            glTranslatef(container[l][0], container[l][1], 0);
            glRotatef(container[l][2],0,0,1); glTranslatef(-2.5,-50,0);

            glBegin(GL_QUADS);

                glVertex3f(0, 0, 0);

                glVertex3f(5, 0, 0);

                glVertex3f(5, 10, 0);

                glVertex3f(0, 10, 0);

            glEnd();

            glLoadIdentity();
        }
    }
    if(docreate == 1){
    id++;
    docreate = 0;    
    }
}


User is offlineProfile CardPM
+Quote Post

e_barroga

RE: 2d Shooter - Shooting Bullets [sdl + Opengl]

22 Apr, 2009 - 10:27 AM
Post #6

D.I.C Regular
Group Icon

Joined: 16 Feb, 2009
Posts: 435



Thanked: 24 times
Dream Kudos: 825
My Contributions
The first problem that I noticed in your class is that each bullet instance will have their own, separate "container". The container should be global, although since global variables is bad practice you should declare the container static to the class. Also, the container should hold Bullet types:
CODE

class Bullet {
    static Bullet Container;
};


I'm not sure if you're familiar with vectors but you might want to use it over arrays. It's simply an array with management built-in.

CODE

vector<Bullet> Container;


I hope this post was helpful!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:20PM

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