## AI/Movement

Member
Posts: 94
Joined: 2008.08
Post: #1
Hi There,

I've developed this game: spacerage.co.uk and Im attempting to get the movement of the ships better.

At the moment, they work by the ships having a real target and a fake target (both 3d vectors). The ships always point towards the fake target, using this method. The fake targets are always chasing the real targets. The problem with this is, that the fake targets have to move slow enough so that the ships turn smoothly, but that means the ships catch the fake target up, causing the quaternionFacingPoint function to break because its trying to point the ship at a point which is too close to where it currently is. [I hope this makes sense] But this often causes the ships to suddenly 'flip' and not always have a smooth motion.

I've also tried sending out points in each direction around the ship to see which of these points is closest to its target, and then turning in that direction. But this has very unpredicatable results, and the ship tends to 'shake' and then often just spirals off.

One method Im thinking of is to again have the fake target and always have the ship pointing at it again. But instead of the fake target being a 3d vector as a position in space, it should sort of 'sit' on a sphere around the outside of the ship, and move gradually to point towards the real target. The problem is, Im not sure of how to work out the maths for this method. Any clues?

Anyway, does someone have a better idea of how to do this?

Many thanks!
Member
Posts: 59
Joined: 2007.12
Post: #2
You could stop updating the rotation when the target is really close, and just keep whatever orientation the ship had before.

"it should sort of 'sit' on a sphere around the outside of the ship, and move gradually to point towards the real target. The problem is, Im not sure of how to work out the maths for this method. Any clues?"
This sounds very much like quaternions to me.
Moderator
Posts: 1,563
Joined: 2003.10
Post: #3
You're likely to get better results if you apply rotation forces gradually, with some rotational acceleration, deceleration, and maximum speed constants, rather than setting the ships' orientations directly. I'd probably do something like take the cross product of ship's current front vector and vector to its target, clamp your angle to some maximum value, then rotate based on that.
Member
Posts: 94
Joined: 2008.08
Post: #4
ThemsAllTook Wrote:You're likely to get better results if you apply rotation forces gradually, with some rotational acceleration, deceleration, and maximum speed constants, rather than setting the ships' orientations directly. I'd probably do something like take the cross product of ship's current front vector and vector to its target, clamp your angle to some maximum value, then rotate based on that.

I agree, Im wondering how to work out which axis to rotate the ship on though, based on the resulting angle from the cross product?
Moderator
Posts: 1,563
Joined: 2003.10
Post: #5
Cross product gives you the axis. Dot product gives you the angle.
Member
Posts: 94
Joined: 2008.08
Post: #6
Ah of course I'll give that a try tonight, and let you know how it goes, thanks!
Member
Posts: 94
Joined: 2008.08
Post: #7
Hey, I just got around to trying that, and it works perfectly thanks!!

I was slipping up first as I needed to workout the vector to the target from the ship, rather than just using the targets coords.