2D point rotation

Hi,

I have this code for rotating a point in 2D around a central axis of (0.0, 0.0)…

local xx = x*math.cos(theta) - y*math.sin(theta)
local yy = x*math.sin(theta) + y*math.cos(theta)

Here, the ‘theta’ value is the amount to rotate in radians.

I converted this from my test program, written in C-sharp. It is exactly the same code - except for where I can only assume lua is using double point values as it is an untyped language.

The problem is that the above code does not produce the correct rotation, whereas the C-sharp code does. For example, rotating a (10.0, 10.0) point by 90 degrees would involve a very small radian (I won’t calculate it right now) and produce (-10.0, 10.0) - essentially flipping the point across the Y axis. This code does not.

What am I missing about lua that perhaps other are taking for granted? I am very new to this and only came to the language because of Corona, specifically for game programming. Is there something not obvious about the handling of floating point or double precision numbers?

Thanks,

Matt. [import]uid: 8271 topic_id: 1753 reply_id: 301753[/import]

The following example:

local x = 10  
local y = 10  
local degrees = 90  
local theta = (math.pi \* degrees) / 180  
  
local xx = x\*math.cos(theta) - y\*math.sin(theta)  
local yy = x\*math.sin(theta) + y\*math.cos(theta)  
  
print(xx .. "," .. yy)  
  

results in -10,10.
[import]uid: 54 topic_id: 1753 reply_id: 5178[/import]

Thank you so much! Of course, it had to be my degree -> radian conversion, didn’t it.
As an aside, do you know if the rotation always has to be around 1 axis at a time, causing 3D rotation to be more than 3 lines of code?

Thanks again,

Matt [import]uid: 8271 topic_id: 1753 reply_id: 5182[/import]