Game Development Community

Modifying faceMouse example behavior to change animations on targetRotation

by Derek Traver · in Torque Game Builder · 12/12/2009 (12:05 pm) · 3 replies

I'm trying to make my isometric knight guy with 8 directions of animation, to change the direction hes facing, by modifying the sample faceMouse rotation. I thought this task would be pretty straightforward, by just adding a bunch of if blocks to the "onMouseMove" function callback, after %targetRotation was calculated, and depending on the coordinates, change the animation.

Here is my code:

function FaceMouseBehavior::onMouseMove(%this, %modifier, %worldPos)
{
	%vector = t2dVectorSub(%worldPos, %this.owner.position);
	%targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + 90;
   	  
	if( (%targetRotation > 22.5 ) && (%targetRotation < 67.5) )
	{
		%this.owner.playAnimation(knight_idle_ne);
	}
	if( (%targetRotation > 67.5) && (%targetRotation < 112.5) )
	{
		%this.owner.playAnimation(knight_idle_e);
	}
	if( (%targetRotation > 112.5) && (%targetRotation < 157.5) )
	{
		%this.owner.playAnimation(knight_idle_se);
	}
	if( (%targetRotation > 157.5) && (%targetRotation < 202.5) )
	{
		%this.owner.playAnimation(knight_idle_s);
	}
	if( (%targetRotation > 202.5) && (%targetRotation < 247.5) )
	{
		%this.owner.playAnimation(knight_idle_sw);
	}
	if( (%targetRotation > 247.5)  && (%targetRotation < 292.5) )
	{
		%this.owner.playAnimation(knight_idle_w);
	}
	if( (%targetRotation > 292.5)  && (%targetRotation < 337.5) )
	{
		%this.owner.playAnimation(knight_idle_nw);
	}
	if( (%targetRotation < 337.5) || (%targetRotation < 22.5) )
	{
		%this.owner.playAnimation(knight_idle_n);
	}
}

I broke it down into 45 degree wedges so each animation got a little more face time. These all work except the northwest wedge, and I cannot figure out why. These conditions:

(%targetRotation > 292.5)  && (%targetRotation < 337.5)

should be a 45 degree wedge in the northwest area around my character, shouldn't they?

#1
12/13/2009 (3:48 am)
I'm not positive, but if you look at your last if-statement, you'll see that you probably want the first less-than to be a greater-than.

The only thing that confuses me is that I would think that this last condition would almost always be the one being applied.
#2
12/13/2009 (1:01 pm)
Gah, your right. Thanks William. I was actually just removing that block the last time I tested when saw most of them working, because I thought it was messing something up. I fixed it now.

I STILL cannot see the northwest animation though, and its infuriating me. I noticed if I echo %targetAnimation in that function, when my mouse cursor is in the NW quadrant around the character, the values it echos are negative... I thought they would be 270-360 (hence my 45 degree wedge from 292.5 - 337.5 for my if conditions). For some reason as you approach true west it gets closer and close to 270, and then suddenly becomes -90, and approaches 0 as you get close to north.

To this end I tried changing those couple conditions to this:

if( (%targetRotation > 247.5)  || (%targetRotation < -67.5) )
{
	%this.owner.playAnimation(knight_idle_w);
}
if( (%targetRotation > -67.5)  && (%targetRotation < -22.5) )
{
	%this.owner.playAnimation(knight_idle_nw);
}
if( (%targetRotation > -22.5) && (%targetRotation < 22.5) )
{
	%this.owner.playAnimation(knight_idle_n);
}

But to no avail.

What the crap?
#3
12/13/2009 (1:06 pm)
Nevermind! I must have done something retarded, I just typed them over from scratch just to see if I had botched something and it worked :)

Heres my final behavior:

if (!isObject(FaceMouseBehavior))
{
   %template = new BehaviorTemplate(FaceMouseBehavior);
   
   %template.friendlyName = "Face Mouse";
   %template.behaviorType = "Mouse Input";
   %template.description  = "Set the object to always face the mouse";
   
}

function FaceMouseBehavior::onBehaviorAdd(%this)
{
   %this.owner.setUseMouseEvents(true);
}

function FaceMouseBehavior::onAddToScene(%this, %scenegraph)
{
   %this.owner.setMouseLocked(true);
}

function FaceMouseBehavior::onMouseMove(%this, %modifier, %worldPos)
{
	%vector = t2dVectorSub(%worldPos, %this.owner.position);
	%targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + 90;
	   	  
	if( (%targetRotation > 22.5 ) && (%targetRotation < 67.5) )
	{
		%this.owner.playAnimation(knight_idle_ne);
	}
	if( (%targetRotation > 67.5) && (%targetRotation < 112.5) )
	{
		%this.owner.playAnimation(knight_idle_e);
	}
	if( (%targetRotation > 112.5) && (%targetRotation < 157.5) )
	{
		%this.owner.playAnimation(knight_idle_se);
	}
	if( (%targetRotation > 157.5) && (%targetRotation < 202.5) )
	{
		%this.owner.playAnimation(knight_idle_s);
	}
	if( (%targetRotation > 202.5) && (%targetRotation < 247.5) )
	{
		%this.owner.playAnimation(knight_idle_sw);
	}
	if( (%targetRotation > 247.5)  || (%targetRotation < -67.5) )
	{
		%this.owner.playAnimation(knight_idle_w);
	}
	if( (%targetRotation > -67.5) && (%targetRotation < -22.5) )
	{
		%this.owner.playAnimation(knight_idle_nw);
	}
		if( (%targetRotation > -22.5) && (%targetRotation < 22.5) )
	{
		%this.owner.playAnimation(knight_idle_n);
	}
}

function FaceMouseBehavior::onMouseDragged(%this, %modifier, %worldPos)
{
   %this.onMouseMove(%modifier, %worldPos);
}