[SOLVED] H: Computing course correction (steering)

Problems building or running the engine, queries about how to use features etc.
Post Reply
Van
Hobgoblin
Posts: 512
Joined: Fri Nov 19, 2004 3:56 am
Contact:

[SOLVED] H: Computing course correction (steering)

Post by Van » Sun May 17, 2009 7:29 am

I am having one hell of a time. I have been studying that damn Quaternion and Rotation Primer for two days now and I just can't seem to get my mind wrapped around it!

Goal
We are trying to compute a scalar vector representing the amount force to apply in the physics engine to a body (a ship in this case).
Basically, we just want to get the heading difference in the three axises: Desired Heading to Target - Current Heading = Heading Correction
But it's eluding me... :(

Here is what I have so far:

Code: Select all

bool clsScripting::Seek(Ogre::Vector3 TargetPos, float Throttle, Ogre::Real ToWithInDist)
{
	Ogre::Vector3		mPosition		= mGlobalResource->LocalPlayerObjectScene->getInventoryPosition();
	Ogre::Vector3		mVectorTo		= TargetPos - mPosition;		// B-A = A->B
	// Are we there yet?
	if ( mVectorTo.length() <= ToWithInDist ) return true;	// Complete
	mVectorTo.normalise();

	Ogre::Quaternion	mOrientation	= mGlobalResource->LocalPlayerObjectScene->getInventoryOrientation();
	mOrientation.normalise();
	Ogre::Vector3		mCurrentTorque	= mGlobalResource->LocalPlayerObjectScene->getPhysicsTorque();
	mCurrentTorque.normalise();
	Ogre::Vector3		mCurrentHeading	= mOrientation * mPosition;		// Ships current heading
	mCurrentHeading.normalise();
	Ogre::Vector3		mDesiredHeading	= mOrientation * mVectorTo;
	mDesiredHeading.normalise();

	// Compute new torque scalar based on heading difference.
	Ogre::Vector3 mNewTorque = mDesiredHeading - mCurrentHeading; // - mCurrentTorque
	//Ogre::Vector3 mNewTorque = mCurrentHeading - mDesiredHeading; // - mCurrentTorque
	mNewTorque.normalise();

	float mPitch = 0.0f;
	float mYaw = 0.0f;
	float mRoll = 0.0f;

	mYaw = mNewTorque.x;
	mPitch = mNewTorque.y;
	mRoll = mNewTorque.z;

	return false;
} // Seek
I think I'm close but not sure. My ship keeps pointing AWAY from the target and not towards the target but eventually goes into a loop-dee-doo.

What am I missing?
Last edited by Van on Sun May 17, 2009 11:52 pm, edited 1 time in total.
0 x
Stop Global Whining.

Van
Hobgoblin
Posts: 512
Joined: Fri Nov 19, 2004 3:56 am
Contact:

Re: H: Computing course correction (steering)

Post by Van » Sun May 17, 2009 11:52 pm

I solved it. My original logic (not that bastardized version above) was correct I just wasn't using the orientation quaternion properly.

The two lines that are key are:

For Seek() : Ogre::Vector3 mNewTorque = mOrientation.Inverse() * VectorToTarget;

For Flee() : Ogre::Vector3 mNewTorque = mOrientation * VectorToTarget;

Code: Select all


void SomeFunctionYouHave()
{
...
	Ogre::Vector3 mVectorToTarget = TargetPos - PlayerObjectScene->getPosition(); // B-A = A->B
	Seek( mVectorToTarget );
...
}

void clsScripting::Seek(Ogre::Vector3 VectorToTarget)
{
	VectorToTarget.normalise();

	Ogre::Quaternion mOrientation = PlayerObjectScene->getOrientation();
	mOrientation.normalise();

	// Compute new torque scalar (-1.0 to 1.0) based on heading vector to target.
	Ogre::Vector3 mNewTorque = mOrientation.Inverse() * VectorToTarget;
	mNewTorque.normalise();

	float mYaw		= mNewTorque.x;
	float mPitch	= mNewTorque.y;
	float mRoll		= mNewTorque.z;

	if ( mRoll > 0.0f )
		mRoll -= 1.0f;
	else
		mRoll += 1.0f;

	//clsSystemControls::getSingleton().setRoll( mRoll );
	clsSystemControls::getSingleton().setPitch( mPitch );
	clsSystemControls::getSingleton().setYaw( mYaw );
} // Seek

void clsScripting::Flee(Ogre::Vector3 VectorToTarget)
{
	VectorToTarget.normalise();

	Ogre::Quaternion mOrientation = PlayerObjectScene->getInventoryOrientation();
	mOrientation.normalise();

	// Compute new torque scalar (-1.0 to 1.0) based on heading vector to target.
	Ogre::Vector3 mNewTorque = mOrientation * VectorToTarget;
	mNewTorque.normalise();

	float mYaw		= mNewTorque.x;
	float mPitch	= mNewTorque.y;
	float mRoll		= mNewTorque.z;

	if ( mRoll > 0.0f )
		mRoll -= 1.0f;
	else
		mRoll += 1.0f;

	//clsSystemControls::getSingleton().setRoll( mRoll );
	clsSystemControls::getSingleton().setPitch( mPitch );
	clsSystemControls::getSingleton().setYaw( mYaw );
} // Flee

0 x
Stop Global Whining.

Post Reply