Nanfeng

Notes on software development, code, and curious ideas

Converting Degrees and Radians in Cocos2d-x Lua

Angle calculations are common in game development. Use these formulas to convert between degrees and radians.

Degrees to radians

1
2
3
function math.angle2radian(angle)
return angle * math.pi / 180
end

Radians to degrees

1
2
3
function math.radian2angle(radian)
return radian / math.pi * 180
end

For example, math.angle2radian(180) returns π, while math.radian2angle(math.pi / 2) returns 90.

+