-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMath.py
38 lines (32 loc) · 1.69 KB
/
Math.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# This module is in charge of all mathematics in the game. A few conventions to note:
# point = (x, y)
# hitbox/rect = (width, height)
# The combination of the two above gives us an absolute rectangle on the screen.
# vel = [xVel, yVel]
# Recieves two velocities, vel1 and vel2, and returns the velocity of obj1 after collision
def get_collision_vel(vel1, vel2, adj):
if (vel1[0] > 0 and vel2[0] < 0) or (vel1[0] < 0 and vel2[0] > 0) or vel2[0] == 0:
return [(-vel1[0] + vel2[0])*adj, (vel1[1] + vel2[1])*adj]
else:
return [(vel1[0] + vel2[0])*adj, (vel1[1] + vel2[1])*adj]
# Recieves current position and velocity and returns new position according to velocity
def get_new_pos(pos, vel):
return [pos[0] + vel[0], pos[1] + vel[1]]
# Returns true if the two given rectangles converge
def boxes_converging(pos1, hitbox1, pos2, hitbox2):
converging = False
for point in rect_to_points(pos1, hitbox1):
converging = converging or point_in_rect(point, pos2, hitbox2)
return converging
# Returns true if point is within the boundaries of the hitbox
def point_in_rect(point, pos, hitbox):
tl, tr, bl, br = rect_to_points(pos, hitbox)
return (point[0] >= tl[0] and point[0] <= br[0]) and ((point[1] >= tl[1] and point[1] <= br[1]))
# Turns (x, y) and (width, height) to four points - (topleft, topright, bottomleft, bottomright)
def rect_to_points(point, rect):
return point, (point[0]+rect[0], point[1]), (point[0], point[1]+rect[1]), (point[0]+rect[0], point[1]+rect[1])
# Recieves two points and returns values of m and b in the function: y = mx + b that passes through both points
def interpolate_line(point1, point2):
m = (point1[1]-point2[1])/(point1[0]-point2[0])
b = m*(-point1[0]) + point1[1]
return m, b