Skip to content

Preserve segment neighbor tangents when copying - #319

Merged
viblo merged 1 commit into
viblo:masterfrom
antonsoo:fix/segment-copy-neighbors
Oct 14, 2026
Merged

viblo merged 1 commit into
viblo:masterfrom
antonsoo:fix/segment-copy-neighbors

Conversation

@antonsoo

Copy link
Copy Markdown
Contributor

Copying a segment loses the neighbor settings that suppress collisions at joined endpoints:

import pymunk

space = pymunk.Space()
segment = pymunk.Segment(space.static_body, (-10, 0), (0, 0), 0)
segment.set_neighbors((-20, 0), (10, 0))
space.add(segment)
circle = pymunk.Circle(pymunk.Body(1, 1), 0.5)
circle.body.position = (0.25, 0.3)
print(len(space.shape_query(circle)), len(space.copy().shape_query(circle)))
# Before: 0 1
# After:  0 0

Record the neighbor tangents and restore them through set_neighbors. Saving the tangents preserves native behavior if unsafe_set_endpoints subsequently changes the endpoints. Existing states without this field still restore normally.

In 48 native segment-chain configurations covering copy/deepcopy, pickle protocols 2/5, rail angles, segment thickness and circle sizes, restored contact queries and all 240 simulation steps now match the original exactly. Another 48 configurations without neighbor settings retain identical trajectories. Scenes are copied before their first step; this does not claim to preserve active contact caches.

The new regressions cover copied spaces and individual shapes, changed endpoints and legacy state. The full suite passes (293 tests, two skipped; baseline 291 with the same skips). Black and the project's strict mypy settings pass for the changed source on Linux/Python 3.12. No other platform result is claimed.

@viblo

viblo commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Thanks for the bug find and fix!

@viblo
viblo merged commit 7fa5ee5 into viblo:master Oct 14, 2026
7 checks passed
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