Game Development Community

rotateTo ignoring current angular velocity?

by Sir H. Appleby · in Torque Game Builder · 07/25/2009 (2:27 pm) · 0 replies

Hello all.

I have recently started working with Torque and am liking what I am seeing so far but I have come across a problem I cannot get my head around.

I have a battleship with two turrets. The turrets are mounted to the battleship using mountpoints with MountTrackRotation set to 0 so that they can track individually.

I have assigned a modified "Face mouse" behaviour to the turrets with a turnSpeed of 2.0 DPS. The onUpdate code is below

function FaceMouseBehavior::onUpdate(%this)
{
   %worldPos = sceneWindow2D.getMousePosition();
   %vector = t2dVectorSub(%worldPos, %this.owner.position);
   %targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + %this.rotationOffset;

   %this.owner.rotateTo(%targetRotation, %this.turnSpeed, true, false, true, 0.1);
}

Now this all works wonderfully so far. However my turrets do not rotate with the ship, For example if I rotate the ship to the left at a rate of 2 DPS and the turret's target is on the right (so the turrets track right at 2PS) then the turrets SHOULD only be able to keep the current angle constant. Instead they rotate 2 DPS to the right no matter what the ship is doing.

Aha, thought I. So I need to set the angular velocity of the turrets as well as the ship. So what I did was a quick trial and added the following line

function FaceMouseBehavior::onUpdate(%this)
{
   %worldPos = sceneWindow2D.getMousePosition();
   %vector = t2dVectorSub(%worldPos, %this.owner.position);
   %targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + %this.rotationOffset;

   %this.owner.setAngularVelocity($carrier.AngularVelocity);  
   %this.owner.rotateTo(%targetRotation, %this.turnSpeed, true, false, true, 0.1);
}

$carrier is a quick and dirty global variable which is the battleship the turrets are currently mounted to.

So I thought this should work. Before rotating we set the turrets' angular rotation to match the ship. If I comment out the rotateTo command then yes the turrets rotate with the ship, but if I uncomment the rotateTo command then the turrets go back to their previous behavior.

So I assumed that rotateTo ignores the current angular velocity but in the docs for rotateTo I read:

"The object is not forced to the target rotation. It just has an angular velocity set that sends it toward the target. If it is interrupted by a collision or some other means, the target will be set, but the object may not reach it."


To me the rotateTo command should not "set" the angular velocity so much as modify the existing angular velocity to account for other posible factors.

However from what I can see this doesn't appear to be the case. Could someone point out what I could be doing wrong? Or is this a bug or am I expecting unexpected behavior?

If you have read this far, many thanks. Any help would be much appreciated