Rotating: Facing Direction Point
by CDK · in Torque Game Builder · 03/01/2007 (6:01 am) · 8 replies
I have built a rotating object... it rotates around a central dot.
The object that rotates around the central dotneeds to constantly "look" towards it.
So in other words, if I have a dot in the middle of the screen and I have a triangle as the rotating object, the Triangle is at the bottom of the screen with the top point of the triangle pointing towards the middle dot. ( lots of points there ;) ) so when one press left or right on the KB it needs to rotate around the central dot but the top of point of the triangle needs to keep pointing towards this central dot. i.e the triangle will point downwards if it reaches the top of the screen.
I have made the rotating part and it works great but I'm struggling to get the code right so it points towards the center... I got it moving somewhat but it's not the right solution I'm sure.
Any advice?
The object that rotates around the central dotneeds to constantly "look" towards it.
So in other words, if I have a dot in the middle of the screen and I have a triangle as the rotating object, the Triangle is at the bottom of the screen with the top point of the triangle pointing towards the middle dot. ( lots of points there ;) ) so when one press left or right on the KB it needs to rotate around the central dot but the top of point of the triangle needs to keep pointing towards this central dot. i.e the triangle will point downwards if it reaches the top of the screen.
I have made the rotating part and it works great but I'm struggling to get the code right so it points towards the center... I got it moving somewhat but it's not the right solution I'm sure.
Any advice?
About the author
#2
If you triangle is mounted to another object, this will still work if you don't track the rotation of your parent object. This assumes that the tip of your triangle is 0 degrees in your image. If not you could add the offset to the angle.
Hope that helps.
03/02/2007 (10:02 am)
If your triangle is not mounted to another object we can use a little trig to find the rotation angle. If you triangle is mounted to another object, this will still work if you don't track the rotation of your parent object. This assumes that the tip of your triangle is 0 degrees in your image. If not you could add the offset to the angle.
//Get the vector to our center point %directionVectorX = centerpoint.getpositionX() - triangle.getpositionX(); %directionVectorY = centerpoint.getpositionY() - triangle.getpositionY(); //Calculate the angle. We need to subtract from 180 because TGB uses an inverted Y %angle = 180 - mRadtoDeg(maTan(%directionVectorX , %directionVectorY)); //Set to roatation angle of our triangle triangle.setRotation(%angle);
Hope that helps.
#3
I will try this on the W.E.
03/02/2007 (12:04 pm)
Thanks guys, it makes a whole lot more sense to me now.I will try this on the W.E.
#4
Guy - your submission works 2 thanks a million.
03/04/2007 (4:08 am)
Again thank you all for the info I have tried the easy way as per Igor Kuryatnikov point 2 (Posted: Mar 02, 2007 08:29 ) - It worked perfectly.Guy - your submission works 2 thanks a million.
#5
07/12/2007 (10:01 pm)
Thanks Guy Lewis!! You rock! I used your code for my turret to target the player. Works great!!!!!!!!!
#6
its a lil easier to read if you know which T2D functions to use.
EDIT: modified to reflect the way it would make the maTan function to work
07/13/2007 (2:44 pm)
The function Guy Lewis wrote, could be also written like this://Get the vector to our center point %directionVector = t2dVectorSub(centerpoint.getposition(), triangle.getposition()); //Calculate the angle. We need to subtract from 180 because TGB uses an inverted Y %angle = 180 - mRadtoDeg(maTan(getWord(%directionVector,0), getWord(%directionVector,1)); //Set to roatation angle of our triangle triangle.setRotation(%angle);
its a lil easier to read if you know which T2D functions to use.
EDIT: modified to reflect the way it would make the maTan function to work
#7
07/13/2007 (8:37 pm)
If memory serves me the reason that I separated the x and y components is the mtan function in TGB 1.13 would not parse the vector list, it had to be comma separated.
#8
07/17/2007 (12:36 pm)
One question here... does anyone know how to use the t2dGunTurret function'?... it seems like the function to use in this cases, but its pretty much undocumented.
Torque Owner Igor Kuryatnikov
1) Make you triangle bigger, in graphical editor for example. The point is to make the center of this new sprite outside of the original triangle. This works, but sometimes inefficient - if radius of rotation is huge.
2) Mount triangle to the dummy object placed in a center and rotate the dummy and/or use setMountRotation()