How math.sin works

I am working on finding some angles for my game and am using math.sin/math.cos to find the inverse tangent. I had all my code written then tested it and got some weird numbers. I then decided to try and just take the sine of 45 for a test… In my code I get .8509… but on my calc I get .7071… Whats going on? I’ve since been trying many things even looked it up on the website. It gave an example of taking math.sin(0.123) = 0.12269009002432 and when I write it in code it gives the same result. But when I use a calculator I get .002146753331046. How come the math.sin, cos, etc and my buttons on my calc are producing different results?

My end result is to get a number and find it’s inverse tangent. My solution was simply… math.sin(x) / math.cos(x) = myNumber.

I dunno if I’m even going about getting my angles correctly anyway but either way my math functions don’t seem to match up. [import]uid: 20272 topic_id: 35531 reply_id: 335531[/import]

Your calculator is taking degrees, but math.sin() takes radians. You can convert a degree amount to radians by calling math.rad(radians) or by multiplying it by math.pi / 180.

You’re actually computing the tangent. For the inverse, use math.atan2(math.sin(x), math.cos(x)). This will give you an angle in radians, so convert that back with math.deg() (or invert the fraction above and multiply) if you want degrees. [import]uid: 27791 topic_id: 35531 reply_id: 141218[/import]

Ok I’ve tried a few things and just don’t quite get it… Here is where I think I’m closest… I’m not to great with the math sections ;/

[lua] A = rocket.x - event.x
O = rocket.y - event.y

ang = O/A

ans = math.atan2(math.sin(ang), math.cos(ang))
math.deg(ans)

print(ans)[/lua] [import]uid: 20272 topic_id: 35531 reply_id: 141250[/import]

. [import]uid: 20272 topic_id: 35531 reply_id: 141249[/import]

Ah, in this case, forgo the [lua]ang=O/A[/lua] and just do [lua]ans = math.atan2(O, A)[/lua].

You CAN pass O/A to math.atan… but you need to deal with some hassle about quadrants. In practice, I’ve always used atan2 exclusively (and I think this is generally true of any other code I’ve seen, too). [import]uid: 27791 topic_id: 35531 reply_id: 141253[/import]

I’ve gotten it to work like this below but I still can’t figure your way out… I’ve got this set up so I can easily test with the calculator… If you care to please explain your way better so I can learn what I’m missing. Also right now I need help trying to figure a way to apply a linear force to the rocket an travel the angle to the event. Any suggestions as I don’t see much reading about it… Just testing and playing around with it now. Thanks.

[lua]local function screenTouch (event)

local O
local A
local angle

if event.x <= 100 then
if event.x < rocket.x and event.y < rocket.y then

A = rocket.x - event.x
O = rocket.y - event.y

print("----------------")
print(“O=y A=x”)
print(O , “”, A)
print(" ")

angle = O/A
print( “O / A = " … angle)
print(” ")
angle = math.deg(math.atan(O/A))
print("Angle: " … math.deg(math.atan(O/A)))

end
end
end
Runtime:addEventListener(“touch”, screenTouch)[/lua] [import]uid: 20272 topic_id: 35531 reply_id: 141286[/import]

Your calculator is taking degrees, but math.sin() takes radians. You can convert a degree amount to radians by calling math.rad(radians) or by multiplying it by math.pi / 180.

You’re actually computing the tangent. For the inverse, use math.atan2(math.sin(x), math.cos(x)). This will give you an angle in radians, so convert that back with math.deg() (or invert the fraction above and multiply) if you want degrees. [import]uid: 27791 topic_id: 35531 reply_id: 141218[/import]

Ok I’ve tried a few things and just don’t quite get it… Here is where I think I’m closest… I’m not to great with the math sections ;/

[lua] A = rocket.x - event.x
O = rocket.y - event.y

ang = O/A

ans = math.atan2(math.sin(ang), math.cos(ang))
math.deg(ans)

print(ans)[/lua] [import]uid: 20272 topic_id: 35531 reply_id: 141250[/import]

. [import]uid: 20272 topic_id: 35531 reply_id: 141249[/import]

Ah, in this case, forgo the [lua]ang=O/A[/lua] and just do [lua]ans = math.atan2(O, A)[/lua].

You CAN pass O/A to math.atan… but you need to deal with some hassle about quadrants. In practice, I’ve always used atan2 exclusively (and I think this is generally true of any other code I’ve seen, too). [import]uid: 27791 topic_id: 35531 reply_id: 141253[/import]

I’ve gotten it to work like this below but I still can’t figure your way out… I’ve got this set up so I can easily test with the calculator… If you care to please explain your way better so I can learn what I’m missing. Also right now I need help trying to figure a way to apply a linear force to the rocket an travel the angle to the event. Any suggestions as I don’t see much reading about it… Just testing and playing around with it now. Thanks.

[lua]local function screenTouch (event)

local O
local A
local angle

if event.x <= 100 then
if event.x < rocket.x and event.y < rocket.y then

A = rocket.x - event.x
O = rocket.y - event.y

print("----------------")
print(“O=y A=x”)
print(O , “”, A)
print(" ")

angle = O/A
print( “O / A = " … angle)
print(” ")
angle = math.deg(math.atan(O/A))
print("Angle: " … math.deg(math.atan(O/A)))

end
end
end
Runtime:addEventListener(“touch”, screenTouch)[/lua] [import]uid: 20272 topic_id: 35531 reply_id: 141286[/import]