Game Development Community

Centre legs to torso

by Daniel Buckmaster · in Torque Game Engine · 03/05/2008 (1:42 pm) · 1 replies

I implemented the 'do the twist' resource with Jeff Raab's FPS changes, but there's a problem - when you move, you move the direction your legs are facing, not the way your head is facing. So I decided to implement auto-centering of legs to torso - so that when you move, your legs automatically rotate to face the direction you're looking, so you run in the right direction.
My first attempt looks like this, and comes right after the code changes in the resource:
if(((move->x != 0.0f) || (move->y != 0.0f))
   && (mState == MoveState)
   && (mDamageState == Enabled))
{
   F32 rot = (mRot.z > M_PI_F)? (mRot.z - M_2PI_F) : mRot.z;
   F32 head = (mHead.z > M_PI_F)? (mHead.z - M_2PI_F) : mHead.z;
   F32 diff = rot - head;
   F32 diff *= 0.3;
   mRot.z -= diff;
   mHead.z += diff;
}
This does something, but the behaviour's generally odd. When I remove the last line, it seems to work pretty much as desired - except that the head gets stuck facing sideways, because I'm not changing it. Is this totally the wrong way to do it, or is there something wrong with changing mHead.z?

About the author

Studying mechatronic engineering and computer science at the University of Sydney. Game development is probably my most time-consuming hobby!


#1
03/08/2008 (12:54 am)
Update bump. When I remove the last line, the code actually does nothing like what I want it to. Something really odd is going on, and I've got no idea how I should go about debugging it.

EDIT
Well, I solved it. I just used a far simpler method :P.
My additions go after the following part in the code (in Player::updateMove):
GameConnection* con = getControllingClient();
		if (move->freeLook && ((isMounted() && getMountNode() == 0) ||
			(con && !con->isFirstPerson())))
		{
			...
		}
		else
		{
			...
		}

Just add
//Dan's twist improvements ->
		//If we're moving, centre legs to twist
		if(((move->x != 0.0f) || (move->y != 0.0f))
			&& (mState == MoveState)
			&& (mDamageState == Enabled))
		{
			//Get head vector in absolute terms
			F32 head = mRot.z + mHead.z;
			//Constrain it to the proper values
			if(head > M_PI_F)
				head -= M_2PI_F;
			//It's our new rotation
			mRot.z = head;
			//Face forwards
			mHead.z = 0.0f;
		}
		//<- Dan