Skip to content

Convert vectors correctly in nonorthogonal bases - #320

Merged
viblo merged 1 commit into
viblo:masterfrom
antonsoo:fix/nonorthogonal-basis
Oct 18, 2026
Merged

viblo merged 1 commit into
viblo:masterfrom
antonsoo:fix/nonorthogonal-basis

Conversation

@antonsoo

Copy link
Copy Markdown
Contributor

Vec2d.convert_to_basis currently computes two independent projections. These give basis coordinates only when the axes are perpendicular. For a common isometric basis:

from pymunk import Vec2d
print(Vec2d(32, 16).convert_to_basis((32, 16), (-32, 16)))
# before: Vec2d(1.0, -0.6)
# after:  Vec2d(1.0, 0.0)

Solve the two-coordinate system using its determinant. Add a nonperpendicular doctest, clarify that the axes must be independent, and cover skewed, reversed, scaled and ordinary perpendicular bases. Dependent axes raise ZeroDivisionError, as zero-length axes already do.

The native picking check projects world positions to screen coordinates and converts them back before Space.point_query_nearest. Across 30 isometric/skew/perpendicular view configurations, 4,278 wrong tile selections and 4,320 missed moving-body selections become zero. An independent NumPy dense solve agrees within 2.9e-15; native trajectories and direct-query controls are unchanged. These are reproducible constructed game scenes, not a claim about an affected released game or extreme/near-singular coordinate ranges.

Fresh Munk2D/CFFI source build, Python 3.12: python -m pymunk.tests runs 296 tests with two existing skips, all others passing (base: 293 tests, two skips). The new regression fails before the change. Mypy passes for vec2d.py; the new tests and changed method pass Black. Whole-file Black reports the same two pre-existing assert-layout differences on both sources.

@viblo

viblo commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Thanks!

@viblo
viblo merged commit c27ea3b into viblo:master Oct 18, 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