# Physics Simulation Forum

 All times are UTC

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Turn Toward OrientationPosted: Sun Aug 12, 2012 11:18 pm

Joined: Mon Mar 05, 2012 11:39 pm
Posts: 11
Hi all,

I'm trying to write some code to make a body turn to face an orientation (perhaps derived from a point in world space) using forces and torque.

Can you give me some advice on how to do this?

Code:
// Angular.
Vector3 vDeltaW = Vector3.Subtract(this.vAngularVelTarget, Vector3.TransformNormal(pBody.AngularVelocity, mBodyInverse));
if (Math.Abs(vDeltaW.X) + Math.Abs(vDeltaW.Y) + Math.Abs(vDeltaW.Z) > 0.001f)
{
Vector3 vLocalForceW = (vDeltaW / fTimeStep) * fMass;
//Vector3 vLocalForceW = new Vector3(
//    (vDeltaW.X / fTimeStep) * (1.0f / pBody.InvInertiaDiagLocal.X),
//    (vDeltaW.Y / fTimeStep) * (1.0f / pBody.InvInertiaDiagLocal.Y),
//    (vDeltaW.Z / fTimeStep) * (1.0f / pBody.InvInertiaDiagLocal.Z));

Vector3 vMaxForceW = vAngularAcceleration * fMass;
//Vector3 vMaxForceW = new Vector3(
//   vAngularAcceleration.X * (1.0f / pBody.InvInertiaDiagLocal.X),
//   vAngularAcceleration.Y * (1.0f / pBody.InvInertiaDiagLocal.Y),
//   vAngularAcceleration.Z * (1.0f / pBody.InvInertiaDiagLocal.Z));

if (vLocalForceW.X > vMaxForceW.X) vLocalForceW.X = vMaxForceW.X;
else if (vLocalForceW.X < -vMaxForceW.X) vLocalForceW.X = -vMaxForceW.X;
if (vLocalForceW.Y > vMaxForceW.Y) vLocalForceW.Y = vMaxForceW.Y;
else if (vLocalForceW.Y < -vMaxForceW.Y) vLocalForceW.Y = -vMaxForceW.Y;
if (vLocalForceW.Z > vMaxForceW.Z) vLocalForceW.Z = vMaxForceW.Z;
else if (vLocalForceW.Z < -vMaxForceW.Z) vLocalForceW.Z = -vMaxForceW.Z;

Vector3 vWorldForceW = Vector3.TransformNormal(vLocalForceW, pBody.WorldTransform);
pBody.ApplyTorque(vWorldForceW);
}

The above code makes a body rotate given a target angular velocity (with a cap based on acceleration). I'm struggling to then compute the target velocity given (a) a point in worldspace that I want to rotate to, or (b) a quaternion rotation in world space.

Thanks in advance, sorry if its an obvious question!

John

Top

 Post subject: Re: Turn Toward OrientationPosted: Thu Aug 16, 2012 4:02 pm

Joined: Mon Mar 05, 2012 11:39 pm
Posts: 11
No ideas? Or is this just really obvious and I'm not seeing it?

Top

 Post subject: Re: Turn Toward OrientationPosted: Sat Aug 18, 2012 12:22 am

Joined: Wed Feb 24, 2010 9:49 pm
Posts: 26
I'm not sure I fully understand, but here's how I thought of it. The code extracts the body's orientation around each axis (yaw, pitch, roll) and calculates the delta towards the target orientation. The delta is applied as torque (and you probably want to fiddle with this to get the speed right). Once the orientation is right, it applies angular damping to stop the rotation.
Code:
Vector3 GetYawPitchRoll(Matrix m)
{
return new Vector3(
(float)Math.Atan(m.M21 / m.M11),
(float)Math.Atan(-m.M31 / Math.Sqrt(m.M32 * m.M32 + m.M33 * m.M33)),
(float)Math.Atan(m.M32 / m.M33)
);
}

Matrix targetRotMatrix = Matrix.RotationQuaternion(targetRotQuat)

Vector3 yprBefore = GetYawPitchRoll(pBody.WorldTransform);
Vector3 yprAfter = GetYawPitchRoll(targetRotationMatrix);
Vector3 yprDelta = yprAfter - yprBefore;

if (yprDelta.LengthSquared() < 0.05f)
pBody.SetDamping(pBody.LinearDamping, 0.9f);
else
pBody.ApplyTorque(yprDelta);

I haven't tested this much and it has some issues like it'll fail if it needs to turn 180 degrees (gimbal lock condition), but hopefully it gives you some idea that you can use.

Top

 Post subject: Re: Turn Toward OrientationPosted: Sat Aug 18, 2012 9:35 pm

Joined: Tue Dec 27, 2011 11:51 am
Posts: 41
I'm trying to build a look at behavior to turn a ship toward a target and then trust toward it but they somehow always faill and thrust the ship in a randoom place and they disapear.

Top

 Post subject: Re: Turn Toward OrientationPosted: Mon Aug 20, 2012 5:01 pm

Joined: Mon Mar 05, 2012 11:39 pm
Posts: 11
Seems we are in the same boat Granyte. I won't have chance to work on this for a another week, but I found some threads that look promising in the meantime:

http://www.ogre3d.org/addonforums/viewt ... f=4&t=5706 The solution at the bottom of this one is similar to anthrax11's idea
http://www.wiremod.com/forum/expression ... orial.html
http://www.wiremod.com/forum/finished-c ... light.html

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: Bing [Bot] and 4 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ BULLET PHYSICS LIBRARY USERS    General Bullet Physics Support and Feedback    Release Announcements    Applications, Games, Demos or Movies using Bullet PHYSICS AUTHORING TOOLS, SERIALIZATION AND STANDARDS    Physics authoring tools, serialization, standards and related topics RESEARCH AND DEVELOPMENT IN COLLISION DETECTION & PHYSICS. Don't post Bullet support questions here!    Research and development discussion about Collision Detection and Physics Simulation    Links, Papers, Libraries, Demos, Movies, Comparisons       Non-technical forum and license/patent discussion    Career Opportunities