Skip to content

Preserve pin distance and ratchet angle when copying constraints - #316

Merged
viblo merged 1 commit into
viblo:masterfrom
antonsoo:fix/joint-copy-state
Oct 12, 2026
Merged

viblo merged 1 commit into
viblo:masterfrom
antonsoo:fix/joint-copy-state

Conversation

@antonsoo

Copy link
Copy Markdown
Contributor

Copying or pickling a PinJoint loses an explicitly set distance. RatchetJoint.angle is also replaced with the current relative body angle:

import pymunk

a, b = pymunk.Body(1, 1), pymunk.Body(1, 1)
b.position = (3, 4)
pin = pymunk.PinJoint(a, b)
pin.distance = 8
print(pin.copy().distance)  # Before: 5.0; after: 8.0

ratchet = pymunk.RatchetJoint(a, b, 0, 0.5)
ratchet.angle = 1.5
print(ratchet.copy().angle)  # Before: 0.0; after: 1.5

Include both properties in the existing general-state lists so restoration applies their native setters after construction. This also preserves them when copying or pickling an entire Space.

Validation:

  • Copy/deepcopy regressions and updated pickle tests fail on the original source and pass with the fix. The full python -m pymunk.tests run passes (289 tests, two skipped).
  • Ran 48 native pendulum/ratchet continuations across three checkpoints and four copy/pickle methods. All 32 previously divergent cases now match freshly constructed reference worlds with the saved physical settings; the 16 controls remain identical.
  • Ran the shipped constraints demo headlessly with configured joints, copying/pickling at three checkpoints. All six continuations match explicitly restored reference settings over 240 steps.

These checks cover the saved public joint state; they do not claim that all solver caches are serialized. Validation used Linux and Python 3.12. Existing unrelated mypy and whole-file formatting failures are unchanged; the edited test module passes Black.

@viblo
viblo merged commit c7bb871 into viblo:master Oct 12, 2026
7 checks passed
@viblo

viblo commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thanks for finding and fixing this bug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants