• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Rotation Quats and AI

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

## Recommended Posts

ok im working on AI at the moment in direct3d I have an enemy ship. I know its orientation (a quaternion.)and position (D3DXVECTOR3). and its destination a position (D3DXVECTOR3). what i need to do is calculate the ammount of roll and pitch needed to align towards the target position - it will then thrust forwards until it reaches it. Waddaya reckon good way of doing this?

##### Share on other sites
Funny that I see this now. I''m about to work on the same thing.

You won''t need the orientation of the enemy ship itself, just the orientation with respect to your AI ship.

The approach I''ll try will be to calculate the orientation (a quaternion I guess) that would point my AI ship exactly at the target. Then somehow determine the difference quaternion between that orientation and the AI ship''s current orientation.

Once I have that, I think I can determine the direction that the AI needs to turn. of course for realism the vehicle has physics. It won''t be just a matter of pointing a mesh in a certain direction.

Anyway I''m just getting started on it. If I make progress I''ll post it here.

Value of good ideas: 10 cents per dozen.
Implementation of the good ideas: Priceless.

Proxima Rebellion - A 3D action sim with a hint of strategy

##### Share on other sites
Well Ive made a little progress - I understand the theory as you stated it above (i have the same physics problem). But obviously its not as easy as that.

Ive been sort of going down the following road....

I get the vector from AI to target. I then get the targets Z axis and get the dot product between the two. Them acos the dot product to get the angle between the two - this gives me the rotation I need to turn to face towards the target.

then do a similar process for the pitch.

Its all a bit cumbersome though Im using the elite style control method where you cannot rotate around the Y axis instead you pitch and roll (a bit like a plane) to orientate urself.

My results so far are flaky at best (ok absoloutley useless!).

Any more specific help would be great.

Re: the above post - how to calculate the difference between two quats and decide the amount of roll / pitch required?

[edited by - RatLord on February 1, 2003 7:00:39 AM]

##### Share on other sites
Done a bit more and having a break to do something else for a bit. Heres where I am now.

I take the targets position and transorm it by the inverse of my rotation matrix - putting it into my rotational space. I then check its X coords relative to mine and decide whether to bank left or right.

I continue to bank until the dot product between my Z axis and the vector to the target is (practically) 1.0f (0.95f)

this works ok. but seems a bit cumbersome - furthermore my atempts to add vertical adjustment (pitch) to this system have met with amusing failure. As the ships have to roll then pitch to turn (ala Elite) they end up going nuts. I want to level them out before they turn but am having trouble with that

any thoughts?

##### Share on other sites
There''s a solution to your problem in the original Game Programming Gems called the ''shortest angle quaternion.'' It solves the problem: given a current orientation (quaternion) and a desired direction (vector) what is the quaternion representing the shortest rotation towards the desired direction? Once you have that quaternion you can slerp between the two orientations.

I''ve been thinking about the same problem but with restrictions like:
- limited angular velocity
- limited angular acceleration
- different angular velocity/acceleration for different rotation axes (eg faster roll than pitch)

I know this is something of a vague question, and probably more suited for the math & physics forum, but anyone have any experience with this that they would like to share?

##### Share on other sites
I have read that article - and will be using that method for certain things like (as in their example) guided missles. Howeever my problem is the same as yours I am getting the AI to fly as if they were humans using the same restrictions - and using only roll and pitch. Its quite annoyingly tricky - if anyone has got any example code of this kind of system or any links or anything!Let us know!

##### Share on other sites
Sorry I guess I misread your original post. It seems we''re looking for the same thing so let me echo your plea for help.

Here''s a thought. Volition released the source code for Freespace a while back including all the AI and physics. I''ve looked through it before but not specifically in those areas. It''s fairly light on documentation/information but with some work you could probably decipher it and get a couple ideas.

##### Share on other sites
Ive got some of it working a bit of source to point the way with the next phase would help enourmoulsy - could u post a link ?

##### Share on other sites
Ah I finally found this post, it got buried.

I independently arrived at the same solution as you RatLord, i.e. get the difference vector, invert the quaternion, turn into matrix (cheapest to invert the quat instead of waiting and inverting the matrix).

That gives us the position relative to my local space.

What you do next depends on your vehicle controls. My vehicle essentially has pitch, yaw and roll thrusters, and forward and strafe thrusters (all positive and negative). Application of these controls applies thrust.

To make my vehicle aim well, I use a proportional-derivative control concept for pitch, yaw and roll (my engineering degree to the rescue, do a search for details). The output is the effort or desired thrust force that my AI wants to use.

The AI algorithm must do this at a fixed frequency, i.e. 10x per second. Here is the pseudo-code for computing the pitch effort:

      Vector Direction = Normalize(LocalDifferenceVector);float PitchDiff = asin(Direction.y);float PitchEffort = 2.0f * PitchDiff + 16.0f * (PitchDiff - m_PitchDiff);m_PitchDiff = PitchDiff;

m_PitchDiff is a member variable of whatever class would be doing this. This approach deals with overshoot and makes your AI bot aim nicely. The constants 2.0f and 16.0f are arbitrary. The second one must be generally larger than the first, but the best values depend on your vehicle physics and the rate at which the above code is executed. You'll want to experiment.

Do the same with yaw, and roll if necessary, although you might want to use much smaller constants for roll.

Value of good ideas: 10 cents per dozen.
Implementation of the good ideas: Priceless.

Proxima Rebellion - A 3D action sim with a hint of strategy

[edited by - BS-er on February 22, 2003 12:34:30 PM]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634131
• Total Posts
3015730
×