Bullet Firing

Started by
18 comments, last by raptorstrike 19 years, 3 months ago
I'm writing a basic game in C++ using GDI for now, until I pick up directX. So I have my rectangle player and my rectangle monster and the player can move around. Now I need to do monster shooting at player. So I get the x,y of the player, and then start a bullet (guess what - small rectangle!) at the monster x,y position. Then I test for x and y differences between the bullet's current position and the player position it was aimed at, and move it accordingly in each iteration of the game loop. Everything works, except the bullet's path - A BBBBB__________ ________B________ __________B_______ ____________B_____ ______________B____ ________________M__ A - 'aim point' M - monster B - bullet See? It travels diagonally at first, because both x and y are not the same as the aimed position, until the x is the same, then it just moves horizontally. What I want to do is a straight path evenwhen the path is not exactly diagonal, horizontal or vertical. Hopefully this makes sense :)
Advertisement
The best thing to do is store the bullet position and velocity as 2D vectors. I assume you know some basic vector math and time-based movement.

The velocity of the bullet is calculated: Normalize(A-M)*Constant;

I don't really understand if you want the bullet to "follow" the player, or just follow a straigt path. In the first case, you update the velocity each frame, in the second, you calculate the velocity just once the bullet is fired.

And the position of the bullet is updated like this:
pos=pos+vel*dt;//dt=time that has elapsed since last update
You have to find the delta y for each delta x.

float DifferenceX = FinalX - StartX;
float DifferenceY = FinalY - StartY;
float DeltaX = 1.0f;
float DeltaY = DifferencY / DifferenceX;
Just to clarify, I want the bullet to just travel in a straight line towards the original aim point, rather than tracking the player.

Ironically, tracking the player seems easier than travelling in a straight line!

I haven't really done any vector stuff yet, but it looks fairly simple...
Quote:Original post by Pipo DeClown
You have to find the delta y for each delta x.

float DifferenceX = FinalX - StartX;
float DifferenceY = FinalY - StartY;
float DeltaX = 1.0f;
float DeltaY = DifferencY / DifferenceX;


That has some drawbacks. For instance, if (DifferenceX=0) we would have division by zero, the velocity is not the same for all directions(for directions almost vertical, the bullet would make a huge jump if we assume he always increases X by DeltaX=1.0), no support for time-based movement which he will need eventually... it's better for him to do this the right way from the start.
If you want a diagnal between 2 points then you might want to look a a tutorial like this http://www.falloutsoftware.com/tutorials/dd/dd4.htm


Its a simple explanation, hope it helps.

Fud

Quote:Original post by mikeman
Normalize(A-M)*Constant;


Getting lost here...
Quote:Original post by LMRFUD
If you want a diagnal between 2 points then you might want to look a a tutorial like this http://www.falloutsoftware.com/tutorials/dd/dd4.htm


Its a simple explanation, hope it helps.

Fud


That helped. Thanks. although I now know I need to look into vectors etc in a bit more detail :)
LMRFUD, that tutorial is for drawing lines. It has some connection, but moving a bullet is not exactly the same as plotting pixels.

Quote:Original post by cornflake
Quote:Original post by mikeman
Normalize(A-M)*Constant;


Getting lost here...


It's pseudocode.
Normalize() means you simply divide the (A-M) vector with its length, to come up with a vector with the correct direction, but with unit length(=1). You then multiply it by a constant to make the bullet travel faster/slower.

Ok, just a very simple code example to see how it works:


struct CVector2D{float x,y};CVector bullet_pos,bullet_vel;CVector monster_pos,player_pos;void Normalize(CVector2D &v){float length;length=sqrt(v.x*v.x+v.y*v.y);v.x=v.x/length;v.y=v.y/length;}void FireBullet()//Called when you want to fire a bullet{CVector2D temp;temp.x=player_pos.x-monster_pos.x;temp.y=player_pos.y-monster_pos.y;Normalize(temp);bullet_vel.x=temp.x*5.0;//Just some constantbullet_vel.y=temp.y*5.0;}void MoveBullet(float dt){bullet_pos.x=bullet_pos.x+bullet_vel.x*dt;bullet_pos.y=bullet_pos.y+bullet_vel.y*dt;}


This can easily extend to 3D as well.
Thanks for the example. One more idiot question - I understand the code except for the use of the * characters...

This topic is closed to new replies.

Advertisement