how do you keep objects relative with rotation?

Monkey Forums/Monkey Beginners/how do you keep objects relative with rotation?

RedGTurtlepa(Posted 2016) [#1]
I have a platform, like a train, and I have objects on top of it, the train can rotate and I need objects to be able to maintain their current position and rotation, while overlapping it. Can someone explain to me the math? I found a couple of formulas I dont understand how to plug in:

x - distance * sin(degtorad(direction))
y + distance * cos(degtorad(direction))


I cant understand the math steps going on, I know I need to update the new x and y with an x that is relative to the overlapped base object, with its x and y transformed with a rotation. However, the numbers cant use addition since if you add two different x and y you will get a larger number that is no where near the overlapping object.

So I need a difference in position added to the x of the object overlapping the baseobject.

So is the formula
x += x - (myOverlappedObj_x - distance * sin(degtorad(myOverlappedObj_direction)))


or how do usually solve this problem?


Gerry Quinn(Posted 2016) [#2]
One way of looking at it is to define the coordinates in terms of the base object. So say the object is at (8, 10) relative to the train, which is at (0, 0) with angle 0. The train now moves to (30, 20) and an angle of 40 degrees.

Your object is now at 30 + 8 * Cos( 40 ) - 10 * Sin( 40 ), 20 + 10 * Cos( 40 ) + 8 X Sin( 40 )

You rotate the position relative to the origin of the platform (since the object rotates with the platform), then add the position of the platform. [I'm assuming the platform rotates about its origin - if not you need an extra translation.]


RedGTurtlepa(Posted 2016) [#3]
that looks very familiar, and I know i've used it before. But I still dont understand, problem because im so used to not combining two coordinates which you do in basic geometry(i need to review). So the formula you posted to get the correct ordered pair is this?
x = ( (x_OverlappedObject + self_x * Cos(angleFacing_OverlappedObject)) - (self_y * Sin(angleFacing_OverlappedObject)) )
y = ( (y_OverlappedObject + self_y * Cos(angleFacing_OverlappedObject)) + (self_x * Sin(angleFacing_OverlappedObject)) ) 


ill have to google this since I cant really understand what it is im reading ( no doubt it works). sadly.


Gerry Quinn(Posted 2016) [#4]
I always think of it as a sequence of translations (in the sense of translating languages). You start by finding/knowing the position of the object in platform space, then you translate platform space into world space, by first rotating the object by the platform's angle, then translating by the platform's position. Implement that sequence, and it should work out. (I never remember the formula. If you do a lot of this, it might be worth studying up on matrix methods.)