Skip to content

Commit a675cfc

Browse files
committed
Assert body.moment > 0 when added to space
1 parent ef9e787 commit a675cfc

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ Other improvements:
9494
GCed.
9595
- Improved documentation in many places (``Vec2d``, ``Poly``, ``Shape`` and
9696
more)
97-
- Improved assert for ``body.mass`` sanity check.
97+
- Improved assert for ``body.mass`` sanity check (``0 < mass < math.inf``) for
98+
dynamic bodies added to a space.
99+
- Improved assert for ``body.moment`` sanity check (``0 < moment``) for dynamic
100+
bodies added to a space.
98101
- Internal cleanup of code
99102

100103

pymunk/body.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
__docformat__ = "reStructuredText"
22

3+
import math
34
import weakref
45
from collections.abc import KeysView
56
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Optional # Literal,
@@ -252,21 +253,26 @@ def mass(self) -> float:
252253

253254
@mass.setter
254255
def mass(self, mass: float) -> None:
255-
assert self.space is None or 0 < mass < float(
256-
"inf"
256+
assert (
257+
self.space is None or 0 < mass < math.inf
257258
), "Dynamic bodies must have mass > 0 if they are attached to a Space."
258259
lib.cpBodySetMass(self._body, mass)
259260

260261
@property
261262
def moment(self) -> float:
262263
"""Moment of inertia (MoI or sometimes just moment) of the body.
263264
264-
The moment is like the rotational mass of a body.
265+
The moment is like the rotational mass of a body. Note that it is
266+
valid to set moment to float('inf'). This will make a body that cannot
267+
rotate.
265268
"""
266269
return lib.cpBodyGetMoment(self._body)
267270

268271
@moment.setter
269272
def moment(self, moment: float) -> None:
273+
assert (
274+
self.space is None or moment > 0
275+
), "Dynamic bodies must have moment > 0 if they are attached to a Space"
270276
lib.cpBodySetMoment(self._body, moment)
271277

272278
def _set_position(self, pos: tuple[float, float]) -> None:

pymunk/space.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ def step(self, dt: float) -> None:
562562

563563
for b in self._bodies_to_check:
564564
assert b.body_type != Body.DYNAMIC or (
565-
b.mass > 0 and b.mass < math.inf
566-
), f"Dynamic bodies must have a mass > 0 and < inf. {b} has mass {b.mass}."
565+
b.mass > 0 and b.mass < math.inf and b.moment > 0
566+
), f"Dynamic bodies must have a mass > 0 and < inf and moment > 0. {b} has mass {b.mass}, moment {b.moment}."
567567
self._bodies_to_check.clear()
568568

569569
try:

pymunk/tests/test_body.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import pickle
23
import unittest
34

@@ -182,7 +183,7 @@ def test_mass(self) -> None:
182183
b.mass = 0
183184

184185
with self.assertRaises(AssertionError):
185-
b.mass = float("inf")
186+
b.mass = math.inf
186187

187188
s.remove(b)
188189
b.mass = 0
@@ -202,6 +203,39 @@ def test_mass(self) -> None:
202203
c.density = 10
203204
s.step(1)
204205

206+
def test_moment(self) -> None:
207+
s = p.Space()
208+
b = p.Body()
209+
210+
b.mass = 2
211+
b.moment = 3
212+
s.add(b)
213+
214+
# Cant set 0 moment on Body in Space
215+
with self.assertRaises(AssertionError):
216+
b.moment = 0
217+
218+
# inf moment is fine
219+
b.moment = math.inf
220+
221+
s.remove(b)
222+
b.moment = 0
223+
s.add(b)
224+
# Cant add 0 moment Body to Space and run step
225+
with self.assertRaises(AssertionError):
226+
s.step(1)
227+
228+
c = p.Circle(b, 1)
229+
s.add(c)
230+
231+
# Same with a Shape
232+
with self.assertRaises(AssertionError):
233+
s.step(1)
234+
235+
# Setting the Shape density should fix it
236+
c.density = 10
237+
s.step(1)
238+
205239
def test_mass_moment_from_shape(self) -> None:
206240
s = p.Space()
207241

0 commit comments

Comments
 (0)