T2dShape3D rotateTo problem
by Brian Ratte · in Torque Game Builder · 02/23/2007 (5:23 am) · 2 replies
I am having some difficulty trying to use the rotateTo() function on a t2dShape3D object. It seems you cannot specify any rotation axes other than the X-axis. I need my objects to rotate in the Z axis. I am trying to avoid having to script an angle check in the onUpdateScene area, since I will be checking 35 objects and fear it may add too much overhead for a main loop function. Has anyone had any luck with using rotateTo() with a t2dShape3D object in any axis other than x?
The only other option I can think of is performing a setAngularVelocityZ() and scheduling an angle check to occur, and if it within a certain margin stopping the object. Basically writing my own setRotationTarget that works in all 3 axes.
Anyone have any suggestions?
The only other option I can think of is performing a setAngularVelocityZ() and scheduling an angle check to occur, and if it within a certain margin stopping the object. Basically writing my own setRotationTarget that works in all 3 axes.
Anyone have any suggestions?
About the author
#2
The following code is my interface to a t2dShape3D instance called soldier. When you click on his body, he rotates faster and faster with each click. I am using setShapeAngularVelocity, which is a class method of t2dShape3D.
Good luck!
02/28/2007 (11:27 am)
I hate to spam this in, but I'm behind the eight-ball, so here's the best I can do:The following code is my interface to a t2dShape3D instance called soldier. When you click on his body, he rotates faster and faster with each click. I am using setShapeAngularVelocity, which is a class method of t2dShape3D.
Good luck!
// soldier.cs
function soldier::onMouseDown( %this, %modifier, %worldPosition, %mouseClicks ) {
%this.incrementAngularVelocity( %worldPosition ) ;
}
function soldier::incrementAngularVelocity( %this, %worldPosition ) {
// %this.dumpClassHierarchy( ) ; // t2dShape3D -> t2dSceneObject -> SimObject ->
// The angular acceleration shall be proportional to the distance from the object's center (as if it were the C.O.G.)
// The sign of the force is such that we appear to be pressing on the object.
%pos = %this.getPosition( ) ;
%deltaXY = t2dVectorSub( %pos, %worldPosition ) ;
%dx = getWord( %deltaXY, 0 ) / %this.getSizeX( ) * -100 ;
%dy = getWord( %deltaXY, 1 ) / %this.getSizeY( ) * -100 ;
%dOmega = %dy SPC %dx SPC "0" ;
%omega = %this.AngularVelocityAdd( %dOmega ) ; // Returns the new angular velocity
}
function soldier::AngularVelocityAdd( %this, %sSAV ) {
// In order to increment the angular velocity of a t2dShape3D object, we have to deal with the strange API of this class.
// The calls to getShapeAngularVelocity and setShapeAngularVelocity use different units and basis vectors!
// So we have to retrieve the object's angular velocity, convert to the proper units, and then do the increment.
// Finally we are ready to call the latter function.
%magic = 100 / 1.745329 ; // Units are different: 100 / ( Pi / 2 ) ?
%gSAV = %this.getShapeAngularVelocity( ) ;
%sSAVx = getWord( %sSAV, 0 ) + getWord( %gSAV, 0 ) * %magic ;
%sSAVy = getWord( %sSAV, 1 ) + getWord( %gSAV, 2 ) * %magic ; // Y and Z axes are transposed
%sSAVz = getWord( %sSAV, 2 ) + getWord( %gSAV, 1 ) * %magic ;
%sSAV = %sSAVx SPC %sSAVy SPC %sSAVz ;
%this.setShapeAngularVelocity( %sSAV ) ;
return
%sSAV ;
}
function VectorAdd( %a, %b ) {
%result = "0 1 2" ;
%c = t2dGetMax( getWordCount( %a ), getWordCount( %b ) ) ;
for ( %i = 0; %i < %c ; %i++ ) {
%result = setWord( %result, %i, getWord( %a, %i ) + getWord( %b, %i ) ) ;
}
return
%result ;
}
Torque Owner D. Robert Duke
setShapeRotation(x/y/z)
setShapeAngularVelocity(x/y/z)
Someone please correct me if there's a better way to do this, but I've been using a Timer to check my shape3D rotations. I don't need/want it to fire onUpdateScene. Here's some extremely simplified pseudocode:
onTimer {
%diff = %shape.getShapeRotation() - %shape.vecTargetRotation;
%shape.setShapeAngularVelocity(%diff);
}