Skip to content

Commit 06aaaa9

Browse files
committed
restrict arrows and constraints to avoid objects moving to fast #231
1 parent 3ebb812 commit 06aaaa9

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pymunk/examples/arrows.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Showcase of flying arrows that can stick to objects in a somewhat
22
realistic looking way.
33
"""
4-
import sys
4+
import math
55
from typing import List
66

77
import pygame
@@ -146,7 +146,10 @@ def main():
146146
mouse_position = pymunk.pygame_util.from_pygame(
147147
Vec2d(*pygame.mouse.get_pos()), screen
148148
)
149-
cannon_body.angle = (mouse_position - cannon_body.position).angle
149+
cannon_body.angle = max(
150+
-math.pi / 2,
151+
min(0, (mouse_position - cannon_body.position).angle),
152+
)
150153
# move the unfired arrow together with the cannon
151154
arrow_body.position = cannon_body.position + Vec2d(
152155
cannon_shape.radius + 40, 0
@@ -167,7 +170,7 @@ def main():
167170
# around even when fired straight up. Might not be as accurate, but
168171
# maybe look better.
169172
drag_force_magnitude = (
170-
(1 - abs(dot)) * flight_speed ** 2 * drag_constant * flying_arrow.mass
173+
(1 - abs(dot)) * flight_speed**2 * drag_constant * flying_arrow.mass
171174
)
172175
arrow_tail_position = flying_arrow.position + Vec2d(-50, 0).rotated(
173176
flying_arrow.angle
@@ -222,4 +225,4 @@ def main():
222225

223226

224227
if __name__ == "__main__":
225-
sys.exit(main())
228+
main()

pymunk/examples/constraints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def main():
210210
(0, 0),
211211
shape.body.world_to_local(nearest),
212212
)
213-
mouse_joint.max_force = 50000
213+
mouse_joint.max_force = 7500
214214
mouse_joint.error_bias = (1 - 0.15) ** 60
215215
space.add(mouse_joint)
216216

@@ -249,4 +249,4 @@ def main():
249249

250250

251251
if __name__ == "__main__":
252-
main()
252+
main()

0 commit comments

Comments
 (0)