# Direction of a bullet

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

## Recommended Posts

I have an object moving with some constant speed Vo and I want to fire a bullet toward it at speed of Vb = 400. The start location of object is at position Po and of the bullet is at Pb.

Where o is object and b is bullet.

How can I find the direction vector for a bullet so it hits the object ?

##### Share on other sites

The actual resource that you should look for on that thread that Álvaro posted is his own post, number 6.

When it's 2D, it considers the trajectory of the target as a line, and the possible directions of the bullet as a circle: The circle is intersecting the line at two points, behind and ahead of the target. You regard the point ahead, that the target will travel to in the future.

When it's 3D, the target is travelling in a 3D line, and the possible directions of the bullet form a sphere. The sphere intersects with that line at two points as well.

While that method is the fastest you can get, there's another one that's very clever and you should take a look at. It makes use of a coordinate space where the Y axis is aligned to the distance between the two objects: http://stackoverflow.com/a/2248934

In this method, the Y component of the vectors brings the objects closer together and the X component makes them stay synchronized while travelling.

So the X components of both objects are equal, and you use that to calculate the bullet's Y component based on the "length" of the speed vector for the bullet:

speed.Y = Sqr( speed.length² - speed.X² )

Then you transform these X and Y components from that coordinate space to the screen space and you have the vector that the bullet should travel by.

Edited by Kryzon

##### Share on other sites
Oh, I like that other method. I had never thought about the problem that way, but it's very neat.

The advantage of my approach is that you can solve other more complicated problems with the same basic idea: Find a time t at which the target is at a place that would take the bullet time t to get there. For instance, you can use it to hit constantly-accelerating targets, or to fire from a turret that needs to spend time rotating its cannon before shooting.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634046
• Total Posts
3015219
×