### Firing a projectile straight to the target

Just a simple question regarding physics and maths in firing of projectiles.

I've created this firing device and a target for it to shoot at. i am done with the firing part but encounter some problem regarding the path of the projectile travel to the target.

This is what my code does
http://imageshack.us/photo/my-images/829/wrongk.jpg/

I could'nt think of a linear way of the projectile's traveling route. Currently, my code is like this.

 ``123456789101112131415161718`` ``````//monster_x & monster_y is the position of the target //pos_x & pos_y is the position of the firing device and the bullet's location if (monster_x > pos_x) { //if the target's x location is greater than the projectile pos_x = pos_x + 1; //increase the position x of projectile } else { //if the target's x location is lesser than the projectile pos_x = pos_x - 1; //decrease the position x of projectile } if (monster_y > pos_y) { //if the target's y location is greater than the projectile pos_y = pos_y + 1; //increase the position y of projectile } else { //if the target's x location is lesser than the projectile pos_y = pos_y - 1; //decrease the position y of projectile }``````

I am sure that most of you can tell the problem of the code. the projectile wont travel in a linear way. Any idea how to improve on this such as the projectile will travel in a straight line to hit its target?

This is the picture of what i wanted the projectile to travel.
http://imageshack.us/photo/my-images/27/correctw.jpg/

The bullet_x and pos_x is the same name, i just renamed it.
Any form of help will be greatly appreciated. thanks in advance.
Last edited on
ok so does there have to be a particular interval the projectile travels each time function is called? or can it go like a fraction of 1
be a little more specific about the assignment if ya could
i'm guessing there is no field to be considered? (like gravity)

can't tell from the code you've posted whether pos_x, pos_y, monster_x, monster_y are integers or floats but..
if pos and monster x y are integers then the only way you COULD have a linear path all the way to monster would be if pos originated along an axis x, y or a perfect diagonal from monster.. so yeah thats my two cents in the geometry dept
Last edited on
Say your 20 feet in front of me in the x direction, and 10 feet higher than me in the y direction. To get to you, I need to move 20 feet forward, and 10 feet up.

So think of a right triangle with sides 20, 10, and hypotenuse. The hypotenuse will be of length sqrt(20 * 20 + 10 * 10). This is the distance between us.

In physics you often want to "normalize" so that your triangle has a hypotenuse of 1. So, you need to divide everything by the length of the hypotenuse.

Now you can move 10 / sqrt(20 * 20 + 10 * 10) in the y direction, and 20 / sqrt(20 * 20 + 10 * 10) in the x direction, and you will be 1 foot closer along a linear path between us. We call this pair of normalized value a unit vector, or normalized direction vector.

I'll abbreviate unit_vector_x as uv_x, and the distance in x direction just xd ...

 ``123456789`` ``````double xd = monster_x - pos_x; double yd = monster_y - pos_y; double d = sqrt(xd * xd + yd * yd); double uv_x = uv_x / d; double uv_y = uv_y / d; double speed = the speed you want it to move at.``````

Of course speed is distance over time.

We call the elapsed time delta time, and the change in position delta x or delta y.

 ``12345`` ``````double delta_x = uv_x * speed * delta_t; double delta_y = uv_y * speed * delta_t; pos_x = pos_x + delta_x; pos_y = pos_y + delta_y;``````

Last edited on
Thanks cplusN00b and iseeplusplus for the helping hand.

@cplusN00b
Yes, we can ignore the velocity, gravity and acceleration of the projectiles. Both values are by float. The default given speed in the hard code is the pos_x + 1, pos_x - 1 and pos_y+1, pos_y - 1. Such that when pos_x increased by 1, pos_y will increase by 1 too. i cant really see the diagonal part. I was thinking of making use the gradient but its invain somehow..

@iseeplusplus
Thanks for the reference code. But i got the debug error of uv_x and uv_y are not initialize. And yes, like what i've said earlier, we can ignore the speed and time in my hardcode.

Here's a part of the code in bullets.cpp
 ``1234567891011121314151617181920212223242526272829`` ``````void bullets::bulletUpdate(float monster_x, float monster_y) { if(CurrentState == IDLE) { pos_x = 250; pos_y = 410; } else if (CurrentState == ATTACK) { //monster_x & monster_y is the position of the target //pos_x & pos_y is the position of the firing device and the bullet's location if (monster_x > pos_x) { //if the target's x location is greater than the projectile pos_x = pos_x + 1; //increase the position x of projectile } else { //if the target's x location is lesser than the projectile pos_x = pos_x - 1; //decrease the position x of projectile } if (monster_y > pos_y) { //if the target's y location is greater than the projectile pos_y = pos_y + 1; //increase the position y of projectile } else { //if the target's x location is lesser than the projectile pos_y = pos_y - 1; //decrease the position y of projectile } } }``````

and in myApplication.cpp

 ``123456`` ``````void myApplication::Update(void) { //i've already declared a pointer in the header file // bullets* theBullet; theBullet->bulletUpdate(theEnemy->GetPos_x(), theEnemy->GetPos_y()); }``````

The few line of code which i post is in the Update() function. The projectile just spastic itself to the target ..
Anyway, is there a way to upload pictures in this forum? then i can show you all the problem clearly.
Last edited on
ya may have to post the pic elsewhere then give us a link to it
any way I'm goin to bed good luck
This one is what my code does currently.
http://imageshack.us/photo/my-images/829/wrongk.jpg/

And this is what i wanted..
http://imageshack.us/photo/my-images/27/correctw.jpg/
The pos_x is the same as bullet_x. i juz renamed it.

Any1 can enlighten me?
Last edited on
bring up my post. F> any source of help.
bring up my post...
Using linear algebra is going to be the best way to accomplish this as iseeplusplus showed an example of.
However I believe he made a small typo in his example which is why it didn't work for you. You need to divide each of the side lengths by the hypotenuse so it will be.

 ``123456789101112`` ``````double xd = monster_x - pos_x; double yd = monster_y - pos_y; // Get the side lengths double d = sqrt(xd * xd + yd * yd); // Calculate the hypotenuse double uv_x = xd/ d; double uv_y = yd/ d; // Normalize the sides to give you the directional values (looking up some of these terms might be helpful for you) //(I suggest you multiply these by some form of speed, but go ahead and see what it does without it) pos_x = pos_x + uv_x ; pos_y = pos_y + uv_x ;``````

Last edited on
Normalize the position if the object, which will give you a vector as it relates the origin. Then you multiply the normalized vector by the velocity and add it to the position of the projectile.
Last edited on
Topic archived. No new replies allowed.