tom66
January 5th, 2009, 03:31 PM
I have the following code:
def ship_move():
global ship_angle, ship_speed, ship_pos
rads = ship_angle * (pi / 180)
mul_x = sin(rads)
mul_y = cos(rads)
print "X*: " + str(mul_x) + ", Y*: " + str(mul_y) + ", Ship Angle: " + str(ship_angle) + " deg (" + str(ship_angle_rads) + " rads)"
speed = ship_speed
# if rads <= 1: speed = +speed
# else: speed = -speed
ship_pos[0] += (-speed) * mul_x
ship_pos[1] += (-speed) * mul_y
It is in python, but the 'formula' should apply to all programming languages. I am trying to move an object, specifically a polygon, a distance in pixels, at a specific angle. The angle can vary, and is not always a multiple of 90 or 45. It is converted from degrees into radians.
I have tried sine and cosine but with no luck - the ship moves at angles, but it is: a) in the incorrect direction in some angles, and b) the angle modifies the speed, and some angles go faster than others. Tangent seems to converge to infinity and just flies all over the place when it reaches a 90 degree angle.
Any help appreciated!
def ship_move():
global ship_angle, ship_speed, ship_pos
rads = ship_angle * (pi / 180)
mul_x = sin(rads)
mul_y = cos(rads)
print "X*: " + str(mul_x) + ", Y*: " + str(mul_y) + ", Ship Angle: " + str(ship_angle) + " deg (" + str(ship_angle_rads) + " rads)"
speed = ship_speed
# if rads <= 1: speed = +speed
# else: speed = -speed
ship_pos[0] += (-speed) * mul_x
ship_pos[1] += (-speed) * mul_y
It is in python, but the 'formula' should apply to all programming languages. I am trying to move an object, specifically a polygon, a distance in pixels, at a specific angle. The angle can vary, and is not always a multiple of 90 or 45. It is converted from degrees into radians.
I have tried sine and cosine but with no luck - the ship moves at angles, but it is: a) in the incorrect direction in some angles, and b) the angle modifies the speed, and some angles go faster than others. Tangent seems to converge to infinity and just flies all over the place when it reaches a 90 degree angle.
Any help appreciated!