Game Development Community

Is there a get rotation function?

by Jovin Sveinbjornsson · in Game Design and Creative Issues · 05/19/2009 (10:21 am) · 19 replies

I was wondering if there was a function like %object.getTransform() for rotation in torque game engine? I've tried %object.getRotation() and it wont work :S

And finally if there is an associated %object.setRotation() to go with it?

Cheers in advance

About the author

Recent Threads


#1
05/19/2009 (10:37 am)
I may be thinking about this wrong since I'm fairly new to developing games but TGE is 3d and I believe rotation is 2d so what axis would the rotation be against? Seems like you would want to use matrix math to convert the transform to a rotation...
#2
05/19/2009 (10:42 am)
Pretty much what I want to do (and excuse the bad code here) is:
function rotateSomething()
{
  $rotation = getWord(object.getRotation(),4);
  //I put 4 because in the world view it has the rotation parameters as
  //X Y Z A = A affects XYZ by the value in XYZ

  $rotation += 1;

  %rot = "0 0 1" SPC $rotation;

  object.setRotation(%rot);

  schedule(10,0,rotateSomething);
}

So essentially every 10ms it rotates the object 1 degree, this is my current code however I have no idea how to actually get it working :(
#3
05/19/2009 (11:11 am)
getword() starts counting at 0, not 1.
#4
05/19/2009 (11:13 am)
Ok thats an error by my fault, but it says there is no method "getRotation" :S
#5
05/19/2009 (11:20 am)
hey Jovin -

Drethon is on the right track - when working in 3D you want to use matrices (aka transforms) to rotate stuff.

here's something which does what you probably want to do:

function rotateSomething(%object)
{
   cancel(%object.timerID);               // keeping timer IDs tidy
   %object.timerID = "";

   // get the object's current transform   
   %transform      = %object.getTransform();   // transform is posX posY posZ axisX axisY axisZ angle
   
   // set up a transformation which is a rotation of one degree around the Z axis
   %rotationMatrix = "0 0 0 0 0 1" SPC mDegToRad(1);

   // multiply the object's transform times the rotation transform   
   %transform      = matrixMultiply(%transform, %rotationMatrix);

   // re-apply the whole transform
   %object.setTransform(%transform);

   %object.timerID = schedule(10, 0, rotateSomething, %object);
}
#6
05/19/2009 (11:31 am)
Use getTransfrom(), which returns a set of numbers (7 in all), and then use getWord() for the individual components.

The first three numbers are the XYZ coordinates of the object. The next three are the axis normals. The final value indicates how much rotation is applied around the rotation axes

Here is a simplistic concept that should point you in the right direction.

function RotateShape(%shape, %angle)
{
	%trans = %shape.getTransform();
	%tx = getword(%trans, 0); // first, get the current transform values
	%ty = getword(%trans, 1);
	%tz = getword(%trans, 2);
	%rx = getword(%trans, 3);
	%ry = getword(%trans, 4);
	%rz = getword(%trans, 5);
	%angle += 1.0;
	%ra = %angle; // Set the rotation angle
	%shape.setTransform(%tx SPC %ty SPC %tz SPC %rx SPC %ry SPC %rz SPC %ra);
}

Now it is possible to write your own getRotation() function that simply returns the value(s) of the last four words in an object's transform. It's also further possible to access an object's rotation by use of a 'member' field. But I'll leave these last two concepts as an 'exercise' to do on your own.
#7
05/19/2009 (11:34 am)
Somehow I knew that someone like Orion would probably answer this by the time I typed that in :D
#8
05/19/2009 (11:34 am)
Wow, thanks for the nice in-depth explanations!

I'll give both of them a try, thanks a lot!
#9
05/19/2009 (11:40 am)
Orions's example will probably work more as intended, mine was just an example of how to get the rotation from a transform. I had meant to also show how to rotate the object, but I forgot to add the schedule to it. I'm sure you'll figure it out.
#10
05/19/2009 (1:01 pm)
heh - michael, i actually first wrote up almost the exact same code as you, but found a funny problem which is that TGE flips the rotation axis sometimes. so adding one degree to a rotation of "0 0 1 2.3" could yield something like "0 0 -1 3.9", and then adding one degree to that would flip it back to "0 0 1 2.3" and so on, so the object effectively stopped rotating.
#11
05/21/2009 (7:44 am)
I ran into the same problem in T3D. When rotating an object somewhere around 245 degrees the z rotation flips to negative, causing the rotation to start over around 100 degrees

matrixMultiply(%transform, %rotationMatrix); solves the problem

Thanks for the solve Orion and thanks for asking the question Jovin.
#12
05/21/2009 (7:53 am)
np. note if you're going to be doing this a lot, you might get better results by implementing something in C to rotate the item each interpolateTick(). the rotation will be smoother and you won't have a bazillion schedules ticking away. check out the "Item" class from TGE - it supports rotating right in the C side. (not sure if it's still around in T3D, tho)
#13
05/21/2009 (8:06 am)
Quote:
check out the "Item" class from TGE - it supports rotating right in the C side. (not sure if it's still around in T3D, tho)
Yeah, it's still there.
#14
05/21/2009 (8:11 am)
I didn't even think of it at the time -- even though I use them -- but if you want a bunch of "tight" scripts for vector & angle manipulation(s) check out library.cs in Zod's MGStarter. There is a lot of useful & awesome script buried in that thing.
#15
05/21/2009 (9:10 am)
downloading....... thanks Michael

just glanced at library.cs.....sweet!
#16
04/14/2010 (11:30 am)
I need to rotate an object to an specific direction, and stop.

I've been trying to use the Orion's function, but I can´t came up with a method for properly stoping the rotation where I want. I've tried comparing it to the transform, to the rotation, and even to the forwardVector, but the granularity of the schedule call is tricky for a comparison exit.

Any ideas?
#17
04/14/2010 (3:09 pm)
Ok worked simplifying the forwardVector to one decimal using mFloatLength.

The wicked part was that (%vector1 == %vector2) doesnt consider the elements sign. For instance ("0 -0.5 0" == "0 0.5 0") would return true.

I would like to understand what happened there, but for now it worked implementing a simple compareVector() function that compares element by element.
#18
04/15/2010 (3:45 pm)
if %vector1 is literally the string "0 -0.5 0" and %vector2 is literally the string "0 0.5 0" then you'd want to use $=(for string comparison) instead of ==(for numeric comparison) or strcmp()(i think). Kinda confusing but "0 -0.5 0" isn't actually a number though you can use it for vector related functions.
#19
04/15/2010 (7:04 pm)
Ah! Thanks man, silly me :)