RT: Making a Guided Missile in libGDX with Box2D

Cite from: http://ilearnsomethings.blogspot.com/2013/05/making-guided-missile-in-libgdx-with.html

To see an example, take a look at this video:

Or this one with overhead strikes: 

Finally here is a video that shows a large number of missiles chasing another missile:


To create a guided missile like this, you have to do a few things:

  1. Have a target, that is updated every frame with its current position
  2. (Optional) set some fixed waypoints on the way to the target to alter the flight path, this creates the overhead strike you see in the video, and is one way to improve accuracy
  3. Create a “steer point” that will guide your missile towards the next waypoint/target, but corrects for velocity of the chaser and the chased
  4. Turn the body of the missile until it is facing the steer point.
  5. When we are happy with the facing of the body, we accelerate it.
  6. If you want the missile to explode, you can check distance from target, or use Box2D’s contact listener
Setting the targeting and updated it’s Vector2 position should be easy enough, so I won’t explain it here. Let’s skip to step 3.

Assuming we have a target, let’s face the target

let’s correct for velocities: 

Now let’s seek the waypoint:
And that’s about it.  Here is the code for constraining the angle to 180 and -180 degrees:

Now you need to implement acceleration and contact/explosion handling on your own.

Leave a Reply