[Resolved] Rotation math

I need to know the math for if you wanted to make something rotate to point to your touch. For example:
if you have an arrow that if rotation=0 then it is pointing right. You tap the screen and it rotates until the arrow is pointing to your tap. PLEASE NOTE: I am not talking about rotating in the direction your finger moves. [import]uid: 147322 topic_id: 27335 reply_id: 327335[/import]

Hello? [import]uid: 147322 topic_id: 27335 reply_id: 111187[/import]

Nudge :slight_smile: [import]uid: 147322 topic_id: 27335 reply_id: 112434[/import]

I’ve got the code for this on my computer, but I’m on an iPad now. I’ll post it for you in the next hour or so. :slight_smile:

Brent [import]uid: 9747 topic_id: 27335 reply_id: 112436[/import]

Here’s the code:

local function angleBetween( srcX, srcY, dstX, dstY )  
 local angle = ( math.deg( math.atan2( dstY-srcY, dstX-srcX ) )+90 )  
 if ( angle \< 0 ) then angle = angle + 360 end  
 return angle % 360  
end  

Now just pass the necessary parameters to this function as follows, and use a transition to bring your arrow to the proper angle in the time duration you desire:

local ang = angleBetween( arrow.x, arrow.y, tapX, tapY )  
transition.to( arrow, { time=400, rotation=ang } )  

NOTE: the angle function assumes an object at 0 degrees is pointing up. Since it seems you have already “rotated” your arrow such that 0 degrees has it pointing to the right, just delete the +90 factor in the first line of the function.

Hope this helps!
Brent [import]uid: 9747 topic_id: 27335 reply_id: 112440[/import]

Thanks! Solved. [import]uid: 147322 topic_id: 27335 reply_id: 112488[/import]

Thanks for this! This problem was bugging me for a couple of hours now! [import]uid: 100618 topic_id: 27335 reply_id: 113840[/import]