What's Here?
- Members: 340,154
- Replies: 920,531
- Topics: 154,951
- Snippets: 4,855
- Tutorials: 1,257
- Total Online: 3,954
- Members: 115
- Guests: 3,839
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,154 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 3,954 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
2d shooter - shooting bullets [sdl + opengl]
2d shooter - shooting bullets [sdl + opengl]
Rate Topic:
   
Posted 19 April 2009 - 06:16 AM
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.
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 April 2009 - 06:17 AM
Posted 19 April 2009 - 09:27 PM
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.
void MoveBullet() {
x += std::cos( Direction*( PI/180 ) ) * Speed;
y += std::sin( Direction*( PI/180 ) ) * Speed;
}
Was This Post Helpful?
2
Posted 20 April 2009 - 09:50 AM
Thanks alot! This solved my problem 
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 April 2009 - 09:56 AM
Posted 20 April 2009 - 09:53 AM
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
Posted 21 April 2009 - 10:44 AM
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.
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;
}
}
Posted 22 April 2009 - 10:27 AM
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:
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.
vector<Bullet> Container;
I hope this post was helpful!
Was This Post Helpful?
1
1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|