Skip to content

Commit 10d6e4e

Browse files
committed
Use property attribute instead of function everywhere (requires recent mypy)
1 parent 55f24a6 commit 10d6e4e

File tree

5 files changed

+180
-184
lines changed

5 files changed

+180
-184
lines changed

pymunk/arbiter.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,26 @@ def friction(self) -> float:
155155
def friction(self, friction: float) -> None:
156156
lib.cpArbiterSetFriction(self._arbiter, friction)
157157

158-
def _get_surface_velocity(self) -> Vec2d:
158+
@property
159+
def surface_velocity(self) -> Vec2d:
160+
"""The calculated surface velocity for this collision pair.
161+
162+
Setting the value in a pre_solve() callback will override the value
163+
calculated by the space. the default calculation subtracts the
164+
surface velocity of the second shape from the first and then projects
165+
that onto the tangent of the collision. This is so that only
166+
friction is affected by default calculation. Using a custom
167+
calculation, you can make something that responds like a pinball
168+
bumper, or where the surface velocity is dependent on the location
169+
of the contact point.
170+
"""
159171
v = lib.cpArbiterGetSurfaceVelocity(self._arbiter)
160172
return Vec2d(v.x, v.y)
161173

162-
def _set_surface_velocity(self, velocity: tuple[float, float]) -> None:
174+
@surface_velocity.setter
175+
def surface_velocity(self, velocity: tuple[float, float]) -> None:
163176
lib.cpArbiterSetSurfaceVelocity(self._arbiter, velocity)
164177

165-
surface_velocity = property(
166-
_get_surface_velocity,
167-
_set_surface_velocity,
168-
doc="""The calculated surface velocity for this collision pair.
169-
170-
Setting the value in a pre_solve() callback will override the value
171-
calculated by the space. the default calculation subtracts the
172-
surface velocity of the second shape from the first and then projects
173-
that onto the tangent of the collision. This is so that only
174-
friction is affected by default calculation. Using a custom
175-
calculation, you can make something that responds like a pinball
176-
bumper, or where the surface velocity is dependent on the location
177-
of the contact point.
178-
""",
179-
)
180-
181178
@property
182179
def total_impulse(self) -> Vec2d:
183180
"""Returns the impulse that was applied this step to resolve the

pymunk/body.py

Lines changed: 78 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -275,74 +275,62 @@ def moment(self, moment: float) -> None:
275275
), "Dynamic bodies must have moment > 0 if they are attached to a Space"
276276
lib.cpBodySetMoment(self._body, moment)
277277

278-
def _set_position(self, pos: tuple[float, float]) -> None:
279-
assert len(pos) == 2
280-
lib.cpBodySetPosition(self._body, pos)
278+
@property
279+
def position(self) -> Vec2d:
280+
"""Position of the body.
281281
282-
def _get_position(self) -> Vec2d:
282+
When changing the position you may also want to call
283+
:py:func:`Space.reindex_shapes_for_body` to update the collision
284+
detection information for the attached shapes if plan to make any
285+
queries against the space."""
283286
v = lib.cpBodyGetPosition(self._body)
284287
return Vec2d(v.x, v.y)
285288

286-
position = property(
287-
_get_position,
288-
_set_position,
289-
doc="""Position of the body.
290-
291-
When changing the position you may also want to call
292-
:py:func:`Space.reindex_shapes_for_body` to update the collision
293-
detection information for the attached shapes if plan to make any
294-
queries against the space.""",
295-
)
296-
297-
def _set_center_of_gravity(self, cog: tuple[float, float]) -> None:
298-
assert len(cog) == 2
299-
lib.cpBodySetCenterOfGravity(self._body, cog)
300-
301-
def _get_center_of_gravity(self) -> Vec2d:
302-
v = lib.cpBodyGetCenterOfGravity(self._body)
303-
return Vec2d(v.x, v.y)
289+
@position.setter
290+
def position(self, pos: tuple[float, float]) -> None:
291+
assert len(pos) == 2
292+
lib.cpBodySetPosition(self._body, pos)
304293

305-
center_of_gravity = property(
306-
_get_center_of_gravity,
307-
_set_center_of_gravity,
308-
doc="""Location of the center of gravity in body local coordinates.
294+
@property
295+
def center_of_gravity(self) -> Vec2d:
296+
"""Location of the center of gravity in body local coordinates.
309297
310298
The default value is (0, 0), meaning the center of gravity is the
311299
same as the position of the body.
312-
""",
313-
)
300+
"""
301+
v = lib.cpBodyGetCenterOfGravity(self._body)
302+
return Vec2d(v.x, v.y)
314303

315-
def _set_velocity(self, vel: tuple[float, float]) -> None:
316-
assert len(vel) == 2
317-
lib.cpBodySetVelocity(self._body, vel)
304+
@center_of_gravity.setter
305+
def center_of_gravity(self, cog: tuple[float, float]) -> None:
306+
assert len(cog) == 2
307+
lib.cpBodySetCenterOfGravity(self._body, cog)
318308

319-
def _get_velocity(self) -> Vec2d:
309+
@property
310+
def velocity(self) -> Vec2d:
311+
"""Linear velocity of the center of gravity of the body."""
320312
v = lib.cpBodyGetVelocity(self._body)
321313
return Vec2d(v.x, v.y)
322314

323-
velocity = property(
324-
_get_velocity,
325-
_set_velocity,
326-
doc="""Linear velocity of the center of gravity of the body.""",
327-
)
315+
@velocity.setter
316+
def velocity(self, vel: tuple[float, float]) -> None:
317+
assert len(vel) == 2
318+
lib.cpBodySetVelocity(self._body, vel)
328319

329-
def _set_force(self, f: tuple[float, float]) -> None:
330-
assert len(f) == 2
331-
lib.cpBodySetForce(self._body, f)
320+
@property
321+
def force(self) -> Vec2d:
322+
"""Force applied to the center of gravity of the body.
332323
333-
def _get_force(self) -> Vec2d:
324+
This value is reset for every time step. Note that this is not the
325+
total of forces acting on the body (such as from collisions), but the
326+
force applied manually from the apply force functions."""
334327
v = lib.cpBodyGetForce(self._body)
335328
return Vec2d(v.x, v.y)
336329

337-
force = property(
338-
_get_force,
339-
_set_force,
340-
doc="""Force applied to the center of gravity of the body.
341-
342-
This value is reset for every time step. Note that this is not the
343-
total of forces acting on the body (such as from collisions), but the
344-
force applied manually from the apply force functions.""",
345-
)
330+
@force.setter
331+
def force(self, f: tuple[float, float]) -> None:
332+
assert len(f) == 2
333+
lib.cpBodySetForce(self._body, f)
346334

347335
@property
348336
def angle(self) -> float:
@@ -404,28 +392,19 @@ def space(self) -> Optional["Space"]:
404392
)
405393
return self._space()
406394

407-
def _set_velocity_func(self, func: _VelocityFunc) -> None:
408-
if func == Body.update_velocity:
409-
lib.cpBodySetVelocityUpdateFunc(
410-
self._body, ffi.addressof(lib, "cpBodyUpdateVelocity")
411-
)
412-
else:
413-
self._velocity_func = func
414-
lib.cpBodySetVelocityUpdateFunc(self._body, lib.ext_cpBodyVelocityFunc)
395+
@property
396+
def velocity_func(self) -> _VelocityFunc:
397+
"""The velocity callback function.
415398
416-
velocity_func = property(
417-
fset=_set_velocity_func,
418-
doc="""The velocity callback function.
419-
420-
The velocity callback function is called each time step, and can be
399+
The velocity callback function is called each time step, and can be
421400
used to set a body's velocity.
422401
423402
``func(body : Body, gravity, damping, dt)``
424403
425-
There are many cases when this can be useful. One example is individual
426-
gravity for some bodies, and another is to limit the velocity which is
427-
useful to prevent tunneling.
428-
404+
There are many cases when this can be useful. One example is individual
405+
gravity for some bodies, and another is to limit the velocity which is
406+
useful to prevent tunneling.
407+
429408
Example of a callback that sets gravity to zero for a object.
430409
431410
>>> import pymunk
@@ -435,7 +414,7 @@ def _set_velocity_func(self, func: _VelocityFunc) -> None:
435414
>>> space.add(body)
436415
>>> def zero_gravity(body, gravity, damping, dt):
437416
... pymunk.Body.update_velocity(body, (0,0), damping, dt)
438-
...
417+
...
439418
>>> body.velocity_func = zero_gravity
440419
>>> space.step(1)
441420
>>> space.step(1)
@@ -456,10 +435,38 @@ def _set_velocity_func(self, func: _VelocityFunc) -> None:
456435
...
457436
>>> body.velocity_func = limit_velocity
458437
459-
""",
460-
)
438+
"""
439+
if self._velocity_func is None:
440+
return Body.update_velocity
441+
else:
442+
return self._velocity_func
443+
444+
@velocity_func.setter
445+
def velocity_func(self, func: _VelocityFunc) -> None:
446+
if func == Body.update_velocity:
447+
lib.cpBodySetVelocityUpdateFunc(
448+
self._body, ffi.addressof(lib, "cpBodyUpdateVelocity")
449+
)
450+
else:
451+
self._velocity_func = func
452+
lib.cpBodySetVelocityUpdateFunc(self._body, lib.ext_cpBodyVelocityFunc)
453+
454+
@property
455+
def position_func(self) -> _PositionFunc:
456+
"""The position callback function.
457+
458+
The position callback function is called each time step and can be
459+
used to update the body's position.
460+
461+
``func(body, dt) -> None``
462+
"""
463+
if self._position_func == None:
464+
return Body.update_position
465+
else:
466+
return self._position_func
461467

462-
def _set_position_func(self, func: _PositionFunc) -> None:
468+
@position_func.setter
469+
def position_func(self, func: _PositionFunc) -> None:
463470
if func == Body.update_position:
464471
lib.cpBodySetPositionUpdateFunc(
465472
self._body, ffi.addressof(lib, "cpBodyUpdatePosition")
@@ -468,17 +475,6 @@ def _set_position_func(self, func: _PositionFunc) -> None:
468475
self._position_func = func
469476
lib.cpBodySetPositionUpdateFunc(self._body, lib.ext_cpBodyPositionFunc)
470477

471-
position_func = property(
472-
fset=_set_position_func,
473-
doc="""The position callback function.
474-
475-
The position callback function is called each time step and can be
476-
used to update the body's position.
477-
478-
``func(body, dt) -> None``
479-
""",
480-
)
481-
482478
@property
483479
def kinetic_energy(self) -> float:
484480
"""Get the kinetic energy of a body."""

0 commit comments

Comments
 (0)