Advertisement

2D chase steering algorithm

Started by May 30, 2014 10:52 PM
1 comment, last by PhilObyte 10 years, 3 months ago

Hello community,

I do not know how to make enemies chase the player.

I already have the code to find the angle the enemy needs to shoot at

to hit the moving player, and how to steer to it.

(I don't want to make them cheat, they need to use their left-/right-jets like the player).

But how do I find out which translational acceleration needs to be applied?

The challenge is to cope with the different physical attributes the ships have.

Enemies might be much faster than the player, or much more inert.

If I let them accelerate continuously, they are much too fast and rush by the player

and can't rotate fast enough back which makes them lose the player

(they can accelerate translational into the direction they head to and rotational left-/right)

If I take the braking distance into account, they try to approach the point where the player

is, which works, but if the player moves it results in too slow enemies because the

braking distance is higher than the actual distance to the ship and they slow down.

I tried vector projection, to find out how much the velocity of an enemy matches with

the direction to the player or the player's velocity, but that didn't help me either.

I hope you understood my problem: How do I take the player's velocity into account to find

the translational acceleration an enemy needs to have to chase it?

Here are the properties a ship has, I hope it helps you:


int Mass { get; set; }
float TranslationalAcceleration { get; set; }
float RotationalAcceleration { get; set; }
float TranslationalFriction { get; set; } // Amount the velocity vector is reduced each second
float RotationalFriction { get; set; }
float MaxTranslationalVelocity { get; set; }
float MaxRotationalVelocity { get; set; }

Here is the code for the rotational acceleration, in case you need it:


        public float ApproachAngle(float desiredAngle)
        {
            if (Math.Abs(desiredAngle) > .001f)
            {
                if (Physics.GetBrakingDistance(Math.Abs(ship.WorldRotationalVelocity), ship.RotationalAcceleration / ship.Mass + ship.RotationalFriction) > Math.Abs(desiredAngle))
                {
                    return -MathHelper2.GetSign(desiredAngle);
                }
                else
                {
                    return MathHelper2.GetSign(desiredAngle);
                }
            }
            else return 0;
        }

        public float GetIntersectionOffset(float currentAngle, Vector2 targetVelocity, float ammoVelocity)
        {
            float tangential = targetVelocity.RotateBy(-currentAngle).Y;
            return MathHelper2.GetAngle(new Vector2((float)Math.Sqrt(Math.Pow(ammoVelocity, 2) - Math.Pow(tangential, 2)), tangential));
        }

It would be very nice if you help me.

Phil

It sounds like you can get your objects to reach a point, can you get the objects to reach where they think the player will be? Take a look at the player's velocity, and figure out where it will be in x milliseconds from now, and have the object steer to that point? It's a little kludgey because I'm not sure how you'd go about calculating the best x value, but you could probably just fudge it based upon the distance and the velocity of the objects involved.

Alternatively, you could try for something like the Dubin's Car algorithm, but that's more for an object with a flat turn radius, always travelling at the same speed.

Advertisement

It sounds like you can get your objects to reach a point, can you get the objects to reach where they think the player will be? Take a look at the player's velocity, and figure out where it will be in x milliseconds from now, and have the object steer to that point?


I already do, this is what the method GetIntersectionOffset() does (The shots need to have the same tangential velocity as the player relative to the enemy to hit the player). But I can't find the acceleration the enemy needs to keep up with the player :(

This topic is closed to new replies.

Advertisement