Skip to content

Commit ee6b278

Browse files
committed
Updated Chipmunk2D
1 parent 12a3de9 commit ee6b278

File tree

5 files changed

+29
-112
lines changed

5 files changed

+29
-112
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Changelog
44

55
..
66
Fix for 2 static bodies that are changed to dynamic and are attached to constraints
7-
7+
Updated the fork of Chipmunk2D used by Pymunk, fixing a number of issues, including maxForce on Spring constaints.
8+
89
Pymunk 6.7.0 (2024-05-01)
910
-------------------------
1011

Chipmunk2D

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Pymunk
66
Pymunk is an easy-to-use pythonic 2D physics library that can be used whenever
77
you need 2D rigid body physics from Python. Perfect when you need 2D physics
88
in your game, demo or simulation! It is built on top of the very
9-
capable 2D physics library `Chipmunk <http://chipmunk-physics.net>`_.
9+
capable 2D physics library `Chipmunk2D <http://chipmunk-physics.net>`_.
1010

1111
The first version was released in 2007 and Pymunk is still actively developed
1212
and maintained today, more than 15 years of active development!
@@ -19,7 +19,7 @@ the Pymunk webpage for some examples.
1919
2007 - 2024, Victor Blomqvist - vb@viblo.se, MIT License
2020

2121
This release is based on the latest Pymunk release (6.7.0),
22-
using Chipmunk 7 rev 2e8d4104b7e2380d1a73f5363a931b3eb3de8d07.
22+
using Chipmunk2D 7 rev 7a29dcfa49931f26632f3019582f289ba811a2b9.
2323

2424

2525
Installation

dump/so.py

Lines changed: 23 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""This example lets you dynamically create static walls and dynamic balls
22
33
"""
4+
45
__docformat__ = "reStructuredText"
56

67
import pygame
@@ -18,133 +19,48 @@ def main():
1819
clock = pygame.time.Clock()
1920
running = True
2021
draw_options = pymunk.pygame_util.DrawOptions(screen)
21-
draw_options.flags = pymunk.SpaceDebugDrawOptions.DRAW_SHAPES
22-
draw_options.flags |= pymunk.SpaceDebugDrawOptions.DRAW_COLLISION_POINTS
22+
# draw_options.flags = pymunk.SpaceDebugDrawOptions.DRAW_SHAPES
23+
# draw_options.flags |= pymunk.SpaceDebugDrawOptions.DRAW_COLLISION_POINTS
2324

2425
### Physics stuff
2526
space = pymunk.Space()
26-
27-
static_body = space.static_body
28-
walls = [
29-
pymunk.Segment(static_body, (0, 0), (0, 150), 0.0),
30-
pymunk.Segment(static_body, (0, 150), (150, 150), 0.0),
31-
pymunk.Segment(static_body, (150, 150), (150, 50), 0.0),
32-
pymunk.Segment(static_body, (150, 50), (100, 50), 0.0),
33-
pymunk.Segment(static_body, (100, 50), (100, 0), 0.0),
34-
pymunk.Segment(static_body, (100, 0), (0, 0), 0.0),
35-
]
36-
for wall in walls:
37-
wall.collision_type = 3
38-
space.add(*walls)
39-
40-
x_offset = 5
41-
y_offset = 5
42-
sensor_depth = 50
43-
# body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
44-
body = pymunk.Body(1, 2)
45-
sensor_shape = pymunk.Poly(
46-
body,
47-
[
48-
(-5 + x_offset, 0 + y_offset),
49-
(-20 + x_offset, sensor_depth + y_offset),
50-
(20 + x_offset, sensor_depth + y_offset),
51-
(5 + x_offset, 0 + y_offset),
52-
],
27+
# space.gravity = 0, 900
28+
space.iterations = 3
29+
space.static_body.position = 300, 100
30+
31+
b = pymunk.Body()
32+
b.position = 300, 100
33+
b.angle = 3.0
34+
c = pymunk.Circle(b, 20)
35+
c.mass = 10
36+
space.add(c, b)
37+
b.angular_velocity = 0
38+
b.velocity = 0, 1000
39+
c = pymunk.constraints.DampedSpring(
40+
space.static_body, b, (0, 0), (0, 0), 200, 500, 20
5341
)
54-
sensor_shape.sensor = False
55-
sensor_shape.collision_type = 1
56-
57-
obj = {}
42+
# c = pymunk.constraints.DampedRotarySpring(space.static_body, b, 0, 90000, 2000)
43+
c.max_force = 5000
44+
space.add(c)
5845

59-
def sensor_pre_solve(arbiter: pymunk.Arbiter, space, data):
60-
if sensor_shape in arbiter.shapes:
61-
obj["last_set"] = arbiter.contact_point_set
62-
# for point in arbiter.contact_point_set.points:
63-
# obj["last_contact_points"].append((point.point_a, pygame.Color('red'))
64-
# obj["last_contact_points"].append((point.point_b, pygame.Color('green'))
65-
return True
66-
67-
sensor_collision_handler = space.add_collision_handler(1, 3)
68-
sensor_collision_handler.pre_solve = sensor_pre_solve
69-
space.add(body, sensor_shape)
46+
dt = 1 / 50.0
7047

7148
while running:
7249
for event in pygame.event.get():
7350
if event.type == pygame.QUIT:
7451
running = False
7552
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
7653
running = False
77-
mouse_pos = pygame.mouse.get_pos()
78-
79-
body.position = mouse_pos
80-
body.position -= pymunk.Vec2d(10, 50)
81-
body.angle = 1
82-
body.velocity = 0, 0
83-
body.angular_velocity = 0
84-
85-
### Update physics
86-
obj["last_set"] = None
87-
dt = 1.0 / 60.0
8854

8955
space.step(dt)
9056

91-
# point set
92-
93-
# cpBool swapped = arb->swapped;
94-
# cpVect n = arb->n;
95-
# set.normal = (swapped ? cpvneg(n) : n);
96-
97-
# for(int i=0; i<set.count; i++){
98-
# // Contact points are relative to body CoGs;
99-
# cpVect p1 = cpvadd(arb->body_a->p, arb->contacts[i].r1);
100-
# cpVect p2 = cpvadd(arb->body_b->p, arb->contacts[i].r2);
101-
102-
# set.points[i].pointA = (swapped ? p2 : p1);
103-
# set.points[i].pointB = (swapped ? p1 : p2);
104-
# set.points[i].distance = cpvdot(cpvsub(p2, p1), n);
105-
# }
106-
107-
# debug collision draw
108-
# cpSpaceDebugDrawSegmentImpl draw_seg = options->drawSegment;
109-
# cpDataPointer data = options->data;
110-
111-
# for (int i = 0; i < arbiters->num; i++)
112-
# {
113-
# cpArbiter *arb = (cpArbiter *)arbiters->arr[i];
114-
# cpVect n = arb->n;
115-
116-
# for (int j = 0; j < arb->count; j++)
117-
# {
118-
# cpVect p1 = cpvadd(arb->body_a->p, arb->contacts[j].r1);
119-
# cpVect p2 = cpvadd(arb->body_b->p, arb->contacts[j].r2);
120-
121-
# cpFloat d = 2.0f;
122-
# cpVect a = cpvadd(p1, cpvmult(n, -d));
123-
# cpVect b = cpvadd(p2, cpvmult(n, d));
124-
125-
# a = cpTransformPoint(options->transform, a);
126-
# b = cpTransformPoint(options->transform, b);
127-
# draw_seg(a, b, color, data);
128-
# }
129-
# }
130-
13157
### Draw stuff
13258
screen.fill(pygame.Color("white"))
13359
space.debug_draw(draw_options)
134-
if obj["last_set"] is not None:
135-
for point in obj["last_set"].points:
136-
137-
n = obj["last_set"].normal
138-
# print(n, point.distance.get_distance())
139-
a = point.point_a + point.distance * n
140-
b = point.point_b # - point.distance * n
141-
# print(point.distance, point.point_a.get_distance(point.point_b))
142-
# pygame.draw.circle(screen, pygame.Color("red"), a, 5) # sensor
143-
pygame.draw.circle(screen, pygame.Color("blue"), b, 3)
144-
60+
print(c.impulse / dt)
14561
### Flip screen
14662
pygame.display.flip()
147-
clock.tick(50)
63+
clock.tick(10)
14864
pygame.display.set_caption("fps: " + str(clock.get_fps()))
14965

15066

pymunk/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@
3636

3737
chipmunk_version = "%s-%s" % (
3838
ffi.string(cp.cpVersionString).decode("utf-8"),
39-
"5dd7d774053145fa37f352d7a07d2f75a9bd8039",
39+
"7a29dcfa49931f26632f3019582f289ba811a2b9",
4040
)

0 commit comments

Comments
 (0)