-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.py
More file actions
29 lines (25 loc) · 1.06 KB
/
physics.py
File metadata and controls
29 lines (25 loc) · 1.06 KB
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
import math
# Calculate the collision between two objects
def calculate_collision(obj1, obj2):
if obj1.rect.colliderect(obj2.rect):
dx = (obj1.rect.centerx - obj2.rect.centerx)
dy = (obj1.rect.centery - obj2.rect.centery)
dist = math.sqrt(dx * dx + dy * dy)
nx = dx / dist
ny = dy / dist
rel_vel_x = obj1.vel_x - (getattr(obj2, 'vel_x', 0))
rel_vel_y = obj1.vel_y - (getattr(obj2, 'vel_y', 0))
rel_vel_normal = rel_vel_x * nx + rel_vel_y * ny
if rel_vel_normal < 0:
e = 0.5
impact_speed = -rel_vel_normal * e
if hasattr(obj1, 'vel_x'):
obj1.vel_x = impact_speed * nx
obj1.vel_y = impact_speed * ny
return True
return False
# Calculate the damage done to a block by a bird
def calculate_damage(bird, block, impact_velocity):
base_damage = math.sqrt(impact_velocity[0]**2 + impact_velocity[1]**2) * 0.1
multiplier = bird.damage_multipliers.get(block.block_type, 1.0)
return base_damage * multiplier