forked from richstokes/cheekymonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics_utility.py
75 lines (61 loc) · 2.26 KB
/
physics_utility.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import arcade
from constants import (
DEFAULT_FRICTION,
DEFAULT_MASS,
SPRITE_SCALING
)
import pymunk
import math
class PymunkSprite(arcade.Sprite):
"""
We need a Sprite and a Pymunk physics object. This class blends them
together.
"""
def __init__(self,
filename,
center_x=0,
center_y=0,
scale=1,
mass=DEFAULT_MASS,
moment=None,
friction=DEFAULT_FRICTION,
body_type=pymunk.Body.DYNAMIC):
super().__init__(filename, scale=scale, center_x=center_x, center_y=center_y)
width = self.texture.width * scale
height = self.texture.height * scale
if moment is None:
moment = pymunk.moment_for_box(mass, (width, height))
self.body = pymunk.Body(mass, moment, body_type=body_type)
self.body.position = pymunk.Vec2d(center_x, center_y)
self.shape = pymunk.Poly.create_box(self.body, (width, height))
self.shape.friction = friction
self.shape.HITCOUNT = 0
self.shape.name = "Pymunk"
self.scale = SPRITE_SCALING
# self.category = None
def check_grounding(player):
""" See if the player is on the ground. Used to see if we can jump. """
grounding = {
'normal': pymunk.Vec2d.zero(),
'penetration': pymunk.Vec2d.zero(),
'impulse': pymunk.Vec2d.zero(),
'position': pymunk.Vec2d.zero(),
'body': None
}
def f(arbiter):
n = -arbiter.contact_point_set.normal
if n.y > grounding['normal'].y:
grounding['normal'] = n
grounding['penetration'] = -arbiter.contact_point_set.points[0].distance
grounding['body'] = arbiter.shapes[1].body
grounding['impulse'] = arbiter.total_impulse
grounding['position'] = arbiter.contact_point_set.points[0].point_b
player.body.each_arbiter(f)
# print(grounding)
return grounding
def resync_physics_sprites(sprite_list):
""" Move sprites to where physics objects are """
for sprite in sprite_list:
sprite.center_x = sprite.shape.body.position.x
sprite.center_y = sprite.shape.body.position.y
sprite.angle = math.degrees(sprite.shape.body.angle)