hi all. This might be more of a math problem then anything, but i'll give it a shot. I have an idea for a game i am working on, in which the player must advanced thru a level my the means of propelling a cube in the air (across the level and around barriers). The way this is done, is the player clicks on anywhere on the screen, and the cube is propelled in the direction of the angle between the player and the mouse position. here's a picture that attempts to explain it. So pretty much, i have a function return_cube_slope(mpos,obj_pos), that returns the slope for the cube:

def dist(p1,p2):
     # returns the distance (length of the line)
     return math.sqrt( (p2[0]-p1[0])**2+(p2[1]-p1[1])**2 )

def return_cube_slope(mpos,obj_pos):    
     # velocity for how fast the cube will move
     velocity = 3
     l1 = dist(obj_pos,mpos)                           # hypotenus
     l2 = dist(mpos,[mpos[0],obj_pos[1]])       # adjacent
     # radians
     radians = math.asin( l2/l1 )
     # angle
     angle = math.degrees(radians)
     print angle
     # actual slope
     move_x = math.sin(radians)*-velocity
     move_y = math.cos(radians)*-velocity
     # return the slope
     return [move_x,move_y]

Look at it this way: this can only return a value from 0 to 90. how would I go about re-writing my function to have it support angles 0 thru 360?

You could use cmath.phase which returns the phase of a complex number in -pi, + pi. Note the equivalent math.atan2(y, x) for the phase of the point (x, y).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.