Need a Math Pro (Parent / Child Rotation and XY coords)

Posted By: Garrettwademan

Need a Math Pro (Parent / Child Rotation and XY coords) - 01/04/19 03:01

Okay guys, I am really sorry for this question, it seems so basic but I can't seem to get it. What I am trying to do is make a parent model (in this case the Computer Case) and child models (like CPU, RAM, Connectors) rotate in synconry yet keep their reletive X and Y positions to the computer case. Here is a video of what I have now, and here is the code I am using to do it. Is it because each model isn't aligned in its own axis in MED? Any help is greatly appreciated.

Its close, but not where it should be as it rotates.

YouTube Rotate Demo Current

Code:
//my is each object such as CPU, RAM, Hard Drive, Circuit Board
//arcade_Case is the Parent
//varLocDifX is the offset of the my model, relative to the arcade_Case

varLocDifX = my.x - arcade_Case.x;
varLocDifY = my.y - arcade_Case.y;
varLocDifZ = my.z - arcade_Case.z;

my.pan = arcade_Case.pan;		
my.x = arcade_Case.x + varLocDifX * cos(arcade_Case.pan + 90);
my.y = arcade_Case.y + varLocDifY * sin(arcade_Case.pan + 90);
my.z = arcade_Case.z + varLocDifZ;

Posted By: txesmi

Re: Need a Math Pro (Parent / Child Rotation and XY coords) - 01/04/19 09:30

Hi,
a 2d vector rotation is computed as follows

Code:
x1 = x * cos - y * sin;
y1 = x * sin + y * cos;



Anyway, the computation of the offset vector should not be performed each frame. Otherway it will continuously lose a little length caused by the accuracy limits of the variables.

Code:
vec_diff(my.skill1, my.x, entParent.x); // save the offset vector into three skills
while(1) {
   vec_set(my.x, my.skill1);
   vec_rotate(my.x, entParent.pan);
   vec_add(my.x, entParent.x);
   vec_set(my.pan, entParent.pan);
   wait(1);
}



Salud!
Posted By: Garrettwademan

Re: Need a Math Pro (Parent / Child Rotation and XY coords) - 01/05/19 01:42

Yea, that did the trick, amazing. Thank you! I am going to study more on these because I think this will help me with a bunch of other stuff too.
Posted By: Garrettwademan

Re: Need a Math Pro (Parent / Child Rotation and XY coords) - 01/07/19 06:08

I was able to use what you had and build upon it. You were very helpful. Here is a video of it all working perfectly now.

YouTube Demo Video
Posted By: txesmi

Re: Need a Math Pro (Parent / Child Rotation and XY coords) - 01/07/19 09:08

Congratulations! laugh
Posted By: Garrettwademan

Re: Need a Math Pro (Parent / Child Rotation and XY coords) - 07/02/19 18:46

Hello again. I added a bit of complexity to this code where parts can have offsets (if a tab or switch is flipped it adds to the pan/tilt/roll). So imagine a PC Desktop that has parts in it. As the user moves the PC desktop around the game, the internal objects follow with it. I added some code so that certain tabs / switches can be adjusted, but still has to follow the "arcade_Case" but with the pan/tilt/roll offset. When I run this code below, it seems to semi work. When any data is in the StartxxxOffset the items react, but only from a world perspective, not based upon the object access. I attempted to use ang_add() but it didn't give me the results I was looking for.

This example only works if the PC Desktop is in the original position at the start of the game, otherwise the tabs / switches get out of sync.

//me = tab or switch that follows arcade_Case
vec_set(my.x, my.skill1);
vec_rotate(my.x, arcade_Case.pan);
vec_add(my.x, arcade_Case.x);
vec_set(my.pan, arcade_Case.pan);
my.pan += my.StartPanOffset;
my.tilt += my.StartTiltOffset;
my.roll += my.StartRollOffset;

I am going to keep working on it but open for some help.

[Linked Image]
[Linked Image]
Posted By: Garrettwademan

Re: Need a Math Pro (Parent / Child Rotation and XY coords) - 07/02/19 19:16

I actually think I got it, but not confident it is the best solution.

vec_set(my.x, my.skill1);
vec_rotate(my.x, arcade_Case.pan);
vec_add(my.x, arcade_Case.x);
vec_set(my.pan, arcade_Case.pan);
ang_rotate(my.pan, vector(my.StartPanOffset, my.StartTiltOffset, my.StartRollOffset));
Posted By: txesmi

Re: Need a Math Pro (Parent / Child Rotation and XY coords) - 07/03/19 07:03

Hi,
yes, it is fine. Euler angles are not euclidean and can't be opperated raw. That is the reason of the existence of 'ang_rotate'.

As addition, in the case the backup angles are contiguous and correctly sorted you don't need to build a temporary vector. The use of the first member as reference is enough. Another little thing is that, conceptually, the added rotation is the case's one, but it doesn't really matter.

Code
vec_set(my.pan, my.StartPanOffset);
ang_rotate(my.pan, arcade_Case.pan);


Salud!
© 2024 lite-C Forums