Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pymunk/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ class PinJoint(Constraint):

_pickle_attrs_init = Constraint._pickle_attrs_init + ["anchor_a", "anchor_b"]

_pickle_attrs_general = Constraint._pickle_attrs_general + ["distance"]

def __init__(
self,
a: "Body",
Expand Down Expand Up @@ -828,6 +830,8 @@ class RatchetJoint(Constraint):

_pickle_attrs_init = Constraint._pickle_attrs_init + ["phase", "ratchet"]

_pickle_attrs_general = Constraint._pickle_attrs_general + ["angle"]

def __init__(self, a: "Body", b: "Body", phase: float, ratchet: float) -> None:
"""Works like a socket wrench.

Expand Down
28 changes: 28 additions & 0 deletions pymunk/tests/test_constraint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import gc
import pickle
import unittest
Expand Down Expand Up @@ -233,6 +234,7 @@ def test_distane(self) -> None:
def test_pickle(self) -> None:
a, b = p.Body(10, 10), p.Body(20, 20)
j = PinJoint(a, b, (1, 2), (3, 4))
j.distance = 8

s = pickle.dumps(j)
j2 = pickle.loads(s)
Expand All @@ -243,6 +245,18 @@ def test_pickle(self) -> None:
self.assertEqual(j.a.mass, j2.a.mass)
self.assertEqual(j.b.mass, j2.b.mass)

def test_copy_distance(self) -> None:
a, b = p.Body(10, 10), p.Body(20, 20)
b.position = (3, 4)
j = PinJoint(a, b)
j.distance = 8
for clone in (j.copy, lambda: copy.deepcopy(j)):
with self.subTest(clone=clone):
j2 = clone()
self.assertEqual(j2.distance, 8)
self.assertIsNot(j2.a, a)
self.assertIsNot(j2.b, b)


class UnitTestSlideJoint(unittest.TestCase):
def test_anchor(self) -> None:
Expand Down Expand Up @@ -532,15 +546,29 @@ def test_ratchet(self) -> None:
def test_pickle(self) -> None:
a, b = p.Body(10, 10), p.Body(20, 20)
j = RatchetJoint(a, b, 1, 2)
j.angle = 3

s = pickle.dumps(j)
j2 = pickle.loads(s)

self.assertEqual(j.angle, j2.angle)
self.assertEqual(j.phase, j2.phase)
self.assertEqual(j.ratchet, j2.ratchet)
self.assertEqual(j.a.mass, j2.a.mass)
self.assertEqual(j.b.mass, j2.b.mass)

def test_copy_angle(self) -> None:
a, b = p.Body(10, 10), p.Body(20, 20)
b.angle = 0.25
j = RatchetJoint(a, b, 0, 0.5)
j.angle = 1.5
for clone in (j.copy, lambda: copy.deepcopy(j)):
with self.subTest(clone=clone):
j2 = clone()
self.assertEqual(j2.angle, 1.5)
self.assertIsNot(j2.a, a)
self.assertIsNot(j2.b, b)


class UnitTestGearJoint(unittest.TestCase):
def test_phase(self) -> None:
Expand Down
Loading