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 - 07:17 AM

New Topic/Question
Reply




MultiQuote




|