Advertisement

Direction of a bullet

Started by November 16, 2013 06:15 PM
2 comments, last by alvaro 11 years, 3 months ago

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 ?

http://www.gamedev.net/topic/401165-target-prediction-system--target-leading/
Advertisement

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.

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.

This topic is closed to new replies.

Advertisement