Rotation to desired angle

HexiDave

07-03-2006 00:37:24

I've been reading a few posts on the main Newton boards and I'm struggling (valiantly) to get this to work. The main thread I was looking through and had the most success was this.

I got to the same problem aidave2 was having and my character would spazz out right at the point where it should stop (spring action snaps it too far because of torque beeing so high and it would repeat.) At the bottom of the page is a "slow-down" spring listing, but I could not get it to work. Not even close.

I sat down with it for 3-4 hours trying everything I could think of, went through other methods (just calculating how much I want to rotate per-second) but the problem occurs where my character, despite having no torque added by me, will spin when pushed across the terrain (assuming friction here.)

The Newton SDK sample on CharacterController has a good method, but I can't figure out how to convert it to Ogre commands (the min/max statement containing the orientation matrix for the most part.) If anyone has any working sample code or suggestions, I'd greatly appriciate it - my physics knowledge is very weak right now.

Thanks again to anyone who offers help.

HexiDave

07-03-2006 04:48:23

Anyone? :cry:

walaber

07-03-2006 05:39:14

here is my solution for a side project I was working on.


///////////////////////////////////////////////////////////////////
// heading forces.
Ogre::Vector3 goalHeading = me->mDesiredHeading;
goalHeading.y = 0.0f;
goalHeading.normalise();

Ogre::Vector3 curHeading = bodyorient * Ogre::Vector3::UNIT_Z;

// angle between these vectors.
Ogre::Degree delta_o = Math::ACos( curHeading.dotProduct( goalHeading ) );
Ogre::Vector3 rot_axis = curHeading.crossProduct( goalHeading ).normalisedCopy();
body->addTorque( (rot_axis * delta_o.valueDegrees() * 40.0f) - (body->getOmega()*100.0f) );


this was for a character with an UpVector. mDesiredHeading is a vector pointing in the desired direction to face.

HexiDave

07-03-2006 07:26:14

I'm too tired at the moment to attempt this, but your work's quality speaks for itself - I'll just thank you in advance :D

Night Elf

01-09-2006 14:51:34

@walaber:

Could you clarify how the following line works? I've used this code, but it's not working for me... maybe because I'm using a different scale (1 = 1cm). But I don't really understand what you're doing and so I can't figure how to adjust the constants.

body->addTorque( (rot_axis * delta_o.valueDegrees() * 40.0f) - (body->getOmega()*100.0f) );

What I see when using this code is that the character does rotate towards the desired direction, but then it keeps rotating. It also rotates very slowly.

Thanks in advance!

HexiDave

01-09-2006 16:14:53

It's adding torque (rotation velocity) to the axis (rot_axis) that's perpendicular to your heading and rotates by the angle (delta_o.valueDegrees()) needed to get to your destination heading. The first constant (40.0) is a multiply of how fast it turns in degrees (if delta_o.valueDegrees() comes out to 2 Degrees per update, the 40.0 would make it 80.0 degrees per update) and the second multiplier tries to keep it from spinning out of control. You are subtracting the product of the object's rotational velocity (spin) by the second constant, which is just a multiplier to slow the "wobble" effect (spins too fast past where it needs to go, so it swings back and forth until it gets to the desired angle.)

So in short: the bigger the first constant, the faster it swings - the bigger the second constant, the faster it stops when getting to the desired angle.

Night Elf

01-09-2006 17:05:19

Oh, I get it now. I've changed the values and now it works fine. Thanks for your help!

Kreso

10-07-2008 22:33:20

hey, thanks a lot, you saved me hours of sweat. I implemented your code in my project and it works perfectly :)