Monday, May 6, 2013

Making a Guided Missile in libGDX with Box2D

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.

2 comments:

  1. Can you share full of source code?It's not clear how to run it within a full project.Thank you

    ReplyDelete
  2. Hi Jon, please can you post the whole code on how to make this happen.
    From creating the missile to hitting the target.
    That will help alot and as a newbie it's hard as no one wants to help us even though everyone was a newbie themselves.
    Please Jon that will be so much and I will mention you in my future projects.
    Thank you so much in advance.
    Johnny Graham

    ReplyDelete