Public Group

# Bullet Firing

This topic is 5065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 :)

##### Share on other sites
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

##### Share on other sites
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;

##### Share on other sites
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...

##### Share on other sites
Quote:
 Original post by Pipo DeClownYou 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.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by mikemanNormalize(A-M)*Constant;

Getting lost here...

##### Share on other sites
Quote:
 Original post by LMRFUDIf 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.htmIts 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 :)

##### Share on other sites
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 mikemanNormalize(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.

##### Share on other sites
Thanks for the example. One more idiot question - I understand the code except for the use of the * characters...

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 9
• 19
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633380
• Total Posts
3011578
• ### Who's Online (See full list)

There are no registered users currently online

×