# Space Sim -- Orienting AI ships to pursue target

This topic is 2514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello Everyone,

My main question has to do with the AI ship's movement system. I know that I want a 'simple' On Rails system where ships move only in the direction they are pointed.

With regards to that, I would like to know how alignment rotations are generally calculated.

For example:

I have a ship facing some direction at some point in space and I want to it too yaw/pitch to face a target position. How does this look in code or pseudo code when using radial rotations and a maximum turning speed on each axis of rotation?

The way I had been trying to go about it was to calculated the ship's desired orientation and do angular comparisons between desired alignment planes and my ship's current planes, though I never got it working.

I also messed around with using half-space comparisons to determine whether the target was above/below or left/right of my AI. This worked nicely but once the alignment was close, I wasn't sure how not to continually overshoot my target and then rotate back. (it ended up facing the right direction and jittering back and forth very very quickly)

I am trying to do this all with the idea of building blocks in mind. I'd like to have things be reusable so that I can combine a handful of behaviors into more complex behaviors. (though that may be too much to worry about at this point.)

I would really appreciate any help with regards to this, and would welcome any suggestions regarding AI design for space combat sims as this is my first foray into this type of development.

Thanks everyone

##### Share on other sites
I'm not too pro at AI's, so I can't give many suggestions. However, I can help out with this problem I believe.

It sounds like you are treating each rotation separately, so that makes it easier. Let's focus on pitch. Yaw is exactly the same, and can be done at the same time.

So, your space ship currently has a certain pitch, which I will call P, and the pitch that he wants to have, I will call Q. We just need to figure out how to get from P to Q without going faster than your speed limit.
Here is some pseudo-code for one possible solution:

 if (P + max_speed <= Q) P = P + max_speed else if (P - max_speed >= Q) P = P - max_speed else P = Q 

max_speed is the maximum rotation speed. The reason we test if (P + max_speed <= Q) is because we want to add max_speed as long as it does not shoot above Q. If it would shoot above Q, it is instead set equal to Q. If that made sense, that solves your "jittering back and forth" problem.

Just a quick note, I would suggest an acceleration-based method, which looks similar to the above, but controls velocity instead of rotation directly. This just makes it look smoother, but I recommend you get working code before going fancy.

EDIT:

The way I had been trying to go about it was to calculated the ship's desired orientation and do angular comparisons between desired alignment planes and my ship's current planes, though I never got it working.

[/quote]

Upon a reread of your post, it seems that perhaps your issue is actually just calculating the correct angles. This may be a little complex in 3d, depending on your math skills. Say the position of your ship is A and the target ship is B.

The direction you want your ship to face is B - A, and we want to find this vector's rotation around the x-axis (for pitch; yaw is around the y-axis). In order to do this, we need to project the vector onto the YZ-plane. Then you just find the rotation of this 2d vector using a simple 2d rotation on the YZ-plane.

So in code:

 Vector3 direction = B - A Angle pitch = arctan (direction.y / direction.z) Angle yaw = arctan (direction.z / direction.x) // assuming that a 0 degree yaw means you are facing the z-direction 

##### Share on other sites
I always used a fairly simple vector-based approach; if enemy is at P and you're at Q, orient towards the vector (P - Q). If you have a good math library at your disposal (and who doesn't these days?) this is pretty trivial to write. The gist of it is in bpx95's post already.

As for steering, look up "steering behaviors" for the AI side. Particularly, look into "boids" and "arrival forces" - that'll give you the basics on how to gently adjust towards (and eventually arrive at) a target position/orientation without jittering.

##### Share on other sites
Thank you both, I think I got it now

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633726
• Total Posts
3013572
×