# Derive TOP's center position from rotation

Hello!

I am trying to figure out how to find the “center” coordinates of a rectangle TOP that I am rotating in real time by plus-minus 30 degrees. My solution gets pretty close to result that I want to achieve, but it is not perfect. You can see an example of this problem in the attachments.

Here a brief explanation:
The rotation angle is calculated using the expression math.sin(absTime.seconds*3)*30 in the rotate parameter of the Transform TOP applied to the Rectangle TOP. The rotation is pivoted at px=0.5 and py=0 and the dimensions of the rectangle are width=0.01 and height=1.
Now: since I can’t find an operator that helps me to calculate the Rectangle’s center position I am using some basic trigonometry:

deg_offset = changeOp.par.rotate+90 # +90 is needed because the rectangle is oscillating around the y axis
x = math.cos(deg)
y = math.sin(deg)
# op('center') is a Constant CHOP
op("center").par.value0 = (x/2)*(9/16) # (9/16) scaling factor due to a 16:9 resolution
op("center").par.value1 = (y/2)-0.5 # -0.5 because the TD's coordinates goes from -0.5 to 0.5

this function is used in a OP Execute (the monitor OP is the Transform TOP which makes the Rectangle TOP rotate) inside the onPostCook callback. If I export the resulting x and y values from the Constant CHOP to set the center coordinates of a Circle TOP so that it moves together with the Rectangle, I should see the circle moving exactly at the center of the Rectangle, but somehow it is not…

Is there a better way to achieve this? Is there something that I am not considering?
Thank you very much!

example.toe (4.3 KB)

Hi there!

It’s the correct math you’re doing, though the problem lays in the fact you’re using a PostCook script. This script is only run on the very end of your frame calculation. So after updating the values, touch has no time anymore to update the circleTOP to set the right position, so it’s a frame too late.

What you can do is do the math in chops. (also possible with python using a parExecuteDAT or chopExecuteDAT, but purely in chops is faster). See attached example.

Hope this helps.
Cheers,
tim

RotationSync.tox (1.4 KB)

Fantastic!

the problem lays in the fact you’re using a PostCook script

I thought about that, and I actually moved the algorithm inside the PreCook callback. The result looked the same (one frame in advanced I guess … ), so I surrendered.

Thank you very much for your help!

1 Like