forked from pybox2d/pybox2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakable.py
100 lines (84 loc) · 3.56 KB
/
breakable.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# C++ version Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
# Python version by Ken Lauer / sirkne at gmail dot com
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
from .framework import (Framework, Keys, main)
from Box2D import (b2Cross, b2EdgeShape, b2FixtureDef, b2PolygonShape, b2_pi)
class Breakable (Framework):
name = "Breakable bodies"
description = "With enough of an impulse, the single body will split [press b to manually break it]"
_break = False # Flag to break
broke = False # Already broken?
def __init__(self):
super(Breakable, self).__init__()
# The ground
self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]))
# The breakable body
self.shapes = (b2PolygonShape(box=(0.5, 0.5, (-0.5, 0), 0)),
b2PolygonShape(box=(0.5, 0.5, (0.5, 0), 0))
)
self.body = self.world.CreateDynamicBody(
position=(0, 40),
angle=0.25 * b2_pi,
shapes=self.shapes,
shapeFixture=b2FixtureDef(density=1),
)
self.fixtures = self.body.fixtures
def PostSolve(self, contact, impulse):
# Already broken?
if self.broke:
return
# If the impulse is enough to split the objects, then flag it to break
if max(impulse.normalImpulses) > 40:
self._break = True
def Break(self):
# Create two bodies from one
body = self.body
center = body.worldCenter
body.DestroyFixture(self.fixtures[1])
self.fixture2 = None
body2 = self.world.CreateDynamicBody(
position=body.position,
angle=body.angle,
shapes=self.shapes[1],
shapeFixture=b2FixtureDef(density=1),
)
# Compute consistent velocities for new bodies based on cached
# velocity.
velocity1 = (self.velocity +
b2Cross(self.angularVelocity, body.worldCenter - center))
velocity2 = (self.velocity +
b2Cross(self.angularVelocity, body2.worldCenter - center))
body.angularVelocity = self.angularVelocity
body.linearVelocity = velocity1
body2.angularVelocity = self.angularVelocity
body2.linearVelocity = velocity2
def Step(self, settings):
super(Breakable, self).Step(settings)
if self._break:
self.Break()
self.broke = True
self._break = False
if not self.broke:
self.velocity = self.body.linearVelocity
self.angularVelocity = self.body.angularVelocity
def Keyboard(self, key):
if key == Keys.K_b and not self.broke:
self._break = True
if __name__ == "__main__":
main(Breakable)