Homing rockets

Started by
11 comments, last by Jernej.L 18 years, 9 months ago
I didn't think it would be too hard, but I'm having some difficulty getting homing rockets to work in my 2D game. I tried having them rotate towards whatever their selected target was and then accelerate in the direction they were rotated in, but what tends to happen is that they'll just circle their targets - it's like centripetal force, since they always rotate to face their targets unless they come at them straight on, their acceleration will be tangent to their velocity. Does anyone have a good algorithm for rocket homing they wouldn't mind sharing?
--------------Trans2D - 2D library for C# / Managed DirectX
Advertisement
Quote:
acceleration will be tangent to their velocity


I think you answered your own question there :)

Instead of accelerating towards your target, save out your velocity and modify it directly by some homing factor.
Over-rotate by about 15 degrees--I believe this is how homing missiles in real life work.
Ok, what I ended up getting to work is sort of a combination of your two ideas. Each frame the rocket takes two vectors, the negative of its current unit velocity and the unit vector in the direction of its target. It multiplies each by some factor and combines them to get another unit vector, which it multiplies by its acceleration rate and uses to change the velocity. So, it's not really based on rotation, but it does work by selecting a vector that aims to the side of its target.
--------------Trans2D - 2D library for C# / Managed DirectX
This is not a problem with the missile not anticipating the target's movement - rather, it's a problem that the target is inside the missile's turning circle, so it just orbits.

The missile should be able to determine whether the target is inside its turning circle. If so, it should fly in a straight line (or possibly even turn in the opposite direction) until the target is outside its turning circle. Then it can resume normal homing behaviour, which will result in it flying straight, turning around and coming back bang onto the target.

For added security, make the circle you test for slightly bigger than the missile's actual turning circle.

Here's a picture



Mark


Rotate to aim at a target point which is a projection of the targets current movement vector (project X time periods ahead with X getting smaller by current distance between missile and target divided by some velocity).

It wont solve all the endcases but can make your intercept course more efficient.

Also limiting the maximum velocity of the missile can make your turn radius (during terminate maneuvering) smaller. It should still be fast enough to close with the target, but also be somewhat limited when close to the target.
Your problem is that the missile is always trying to fly at top speed. If it slowed down, it could turn in a tighter circle and hit the target.

Personally, I would code it like:

struct missile{    vec3  pos;    vec3  velocity;    vec3  accel;    float spring_const;  // = 0.3f or so.    float min_speed;    float max_speed;     // etc.};void missile::home_on_target(const vec3& target, float timestep){    // ideally, our velocity will be directly at the target, at top speed.    vec3 to_target = (target - pos).set_length(max_speed / timestep);    // adjust velocity.    damp_spring<vec3>(velocity, to_target, accel, spring_const, timestep);    // don't set min_speed too high...    clamp(velocity, min_speed, max_speed);    pos += velocity * timestep;    // done!}


where "damp_spring" is the function I define here.
Quote:Original post by bytecoder
Over-rotate by about 15 degrees--I believe this is how homing missiles in real life work.


No, in real life radar-guided or heat-seaking missile usually fly towards the place where the missile expects to find the target when the missile reaches it.



target------>BOOM             /           /         /       /     /missile
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"

Depends on the missle
some are cheaper than others
If you want a "newtonian" missile that only uses forward thrust, simply trying to point at the target isn't enough. What you need to do is determine the direction/velocity that you need in order to hit, then subtract the current velocity from that to get the direction you need to apply force in. Point the missile in this direction and apply thrust. This means the missile might point 90 degrees to the side when performing a turn, for example. In an orbiting situation it may even turn backwards to break the orbit.

This topic is closed to new replies.

Advertisement