Trying to correctly rotate an Object by using tan

Hello everyone,

I am trying to make an object (green square) become rotatable by dragging an object (transparent circle), but it constantly disappears. How do I fix this? It is supposed to be ‘facing’ the square.

Here is the link to the main.lua.

What’s going on? - Image

1j8xPaR.gif

a couple thoughts occur: 1) dy/dx will give you the tangent in radians, two problems there – you need the arctangent, not the tangent, (you need an angle, not the tangent of an angle) so use math.atan2(dy,dx) instead; and .rotation needs degrees so convert. 2) dy will be inverted due to screen y vs cartesian, so reverse the order of that calc.

fwiw, hth

Another thoughts.

Your tanNum sometimes returns NaN - not a number. Thus the missing green square.

This happens like when trying to divide 0 by 0 or some other calculations.

tanNum = (touchPoint.y-object.y)/(touchPoint.x-object.x)  —>> at some point = 0/0

Maybe you can do a check before doing the division and skip the calculations (tanProcessed). If…then statement.

Anyway i think you should try Dave advise because i do not see a proper smooth rotation unless that what you want.

burhan 

a couple thoughts occur: 1) dy/dx will give you the tangent in radians, two problems there – you need the arctangent, not the tangent, (you need an angle, not the tangent of an angle) so use math.atan2(dy,dx) instead; and .rotation needs degrees so convert. 2) dy will be inverted due to screen y vs cartesian, so reverse the order of that calc.

fwiw, hth

Another thoughts.

Your tanNum sometimes returns NaN - not a number. Thus the missing green square.

This happens like when trying to divide 0 by 0 or some other calculations.

tanNum = (touchPoint.y-object.y)/(touchPoint.x-object.x)  —>> at some point = 0/0

Maybe you can do a check before doing the division and skip the calculations (tanProcessed). If…then statement.

Anyway i think you should try Dave advise because i do not see a proper smooth rotation unless that what you want.

burhan