How to apply rotation in script ?
by Sylvain · in Technical Issues · 09/04/2007 (8:11 am) · 6 replies
Hi !
Ok, here's my problem :
let's say we've got a square floating in the air. Its Transform being "x0 y0 z0 1 0 0 0".
I can easily calculate its corners coordinates :
1st corner : x0 y0-1.5 z0+1.5
2nd corner : x0 y0+1.5 z0+1.5
3rd corner : x0 y0+1.5 z0-1.5
4th corner : x0 y0-1.5 z0-1.5
(the square has a side of 3, as you've guessed!)
... so far so good ...
now, let say we rotate that square so its transform is "x0 y0 z0 X Y Z R".
I believe that if I want to retrieve the corners coordinates, I need to apply the rotation ("X Y Z R") to the vectors the extremities of which are the center of the square and the corners :
(0 -1.5 1.5) : vector OA, O being the center, A one corner
(0 1.5 1.5) : vector OB
(0 1.5 -1.5) : vector OC
(0 -1.5 -1.5) : vector OD
and then I need to add the rotated vectors to "x0 y0 z0", right ?
The question is : how do I do that in script ?
I've tried to use MatrixMultiply("0 -1.5 1.5","0 0 0 X Y Z R") and every other variation of MMultiply(...), but it won't give me the expected results ...
If there is a Torque-Maths guru around here, pleaase help me !
Thanks !
Ok, here's my problem :
let's say we've got a square floating in the air. Its Transform being "x0 y0 z0 1 0 0 0".
I can easily calculate its corners coordinates :
1st corner : x0 y0-1.5 z0+1.5
2nd corner : x0 y0+1.5 z0+1.5
3rd corner : x0 y0+1.5 z0-1.5
4th corner : x0 y0-1.5 z0-1.5
(the square has a side of 3, as you've guessed!)
... so far so good ...
now, let say we rotate that square so its transform is "x0 y0 z0 X Y Z R".
I believe that if I want to retrieve the corners coordinates, I need to apply the rotation ("X Y Z R") to the vectors the extremities of which are the center of the square and the corners :
(0 -1.5 1.5) : vector OA, O being the center, A one corner
(0 1.5 1.5) : vector OB
(0 1.5 -1.5) : vector OC
(0 -1.5 -1.5) : vector OD
and then I need to add the rotated vectors to "x0 y0 z0", right ?
The question is : how do I do that in script ?
I've tried to use MatrixMultiply("0 -1.5 1.5","0 0 0 X Y Z R") and every other variation of MMultiply(...), but it won't give me the expected results ...
If there is a Torque-Maths guru around here, pleaase help me !
Thanks !
About the author
#2
09/04/2007 (11:28 am)
Check out my post containing a function called "localToWorldPoint" in this thread: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13364.
#3
So I tried something else yesterday that seems to work, here it is, tell me what you think about it :
%offset1 = MatrixMulVector("0 0 0" SPC getWords(%trans,3,6) , "0 -1.5 1.5");
...
%offset4 = ...
%offset1 = getWords(%offset1,0,2)
...
%corner1 = getWord(%trans,0)+getWord(%offset1,0) + getWord(%trans,1)+getWord(%offset1,1) +
getWord(%trans,2)+getWord(%offset1,2);
same thing for corner2,3 and 4.
%trans being the Transform of the considered object (square).
09/05/2007 (2:26 am)
Oops, I didn't see your replies until now. The notificatioons have been considered as spam ...So I tried something else yesterday that seems to work, here it is, tell me what you think about it :
%offset1 = MatrixMulVector("0 0 0" SPC getWords(%trans,3,6) , "0 -1.5 1.5");
...
%offset4 = ...
%offset1 = getWords(%offset1,0,2)
...
%corner1 = getWord(%trans,0)+getWord(%offset1,0) + getWord(%trans,1)+getWord(%offset1,1) +
getWord(%trans,2)+getWord(%offset1,2);
same thing for corner2,3 and 4.
%trans being the Transform of the considered object (square).
#4
@ Gabriel : i just tried but it seems to give almost the same results as what i tried : the coordinates are not too far from being good ... but there is still a difference ..
@ Orion : I tried using that function, however i don't have a vector convolve function ! I thought that i might comment this line out since the scale of my object is "1 1 1", but it still doesn't give satisfying results ... Or perhaps am I using this function the wrong way : i tried this :
myObj.localToWorldPoint("0 -1.5 1.5");
since my object isn't a cube but a square
09/05/2007 (3:28 am)
Ok, my bad, my method doesn't work ...@ Gabriel : i just tried but it seems to give almost the same results as what i tried : the coordinates are not too far from being good ... but there is still a difference ..
@ Orion : I tried using that function, however i don't have a vector convolve function ! I thought that i might comment this line out since the scale of my object is "1 1 1", but it still doesn't give satisfying results ... Or perhaps am I using this function the wrong way : i tried this :
myObj.localToWorldPoint("0 -1.5 1.5");
since my object isn't a cube but a square
#5
And localToWorldPoint is working when I comment out the 'bummer line' !
Thanks a lot !
09/05/2007 (4:35 am)
Ok ok ok, nevermind for vectorConvolve, i found it !And localToWorldPoint is working when I comment out the 'bummer line' !
Thanks a lot !
#6
09/05/2007 (8:46 am)
Glad you got that working. yeah, the "bummer" line is maybe not necessary. i found it necessary for triggers, but possibly not other objects. definitely something fishy.
Torque Owner Gabriel Notman
MatrixMulPoint("PosX PosY PoZ RotX RotY RotZ theta", "0 -1.5 1.5");You need to include the transform position data as well as the orientation data. I haven't tested this myself, but I believe that is how it works. Also you may need to modify the point vector (parameter 2) by the scale of the object if the scale is not "1 1 1".
Gabriel
TDN:
tdn.garagegames.com/wiki/TorqueScript_Console_Functions_21#MatrixMulPoint.28_tra...