Skip to content

Fix cylinder point containment - #28

Merged
VonTum merged 1 commit into
ThePhysicsGuys:masterfrom
killerdevildog:fix/cylinder-point-containment
Sep 21, 2026
Merged

VonTum merged 1 commit into
ThePhysicsGuys:masterfrom
killerdevildog:fix/cylinder-point-containment

Conversation

@killerdevildog

@killerdevildog killerdevildog commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #29

cylinderShape(1, 2).containsPoint(...) rejects (0, 0.75, 0), which is inside the cylinder, and accepts (0, -2, 0), which is outside. The radial check uses x*x + y + y, breaking the cylinder's symmetry.

Replace y + y with y * y so the check evaluates x*x + y*y. The two queries now return true and false, respectively.

Validation: built the standalone physics library in Debug mode with GCC 16.0.1 and ran all 13 geometry tests successfully. A local regression test failed before the fix and passed afterward with 30 assertions covering both signs of X/Y, two radii, and radial and cap boundaries. That test and the standalone reproducer are not included in this PR; this PR contains only the one-line implementation fix. The full project test suite was not run.

Local regression test

This is the test run from tests/geometryTests.cpp before applying the fix. It failed on the original implementation because containsPoint(Vec3(0.0, -2.0, 0.0)) returned true for a radius-1 cylinder, although the point is outside. After the fix, the same test passed all 30 assertions, and all 13 geometry tests passed. The failure demonstrated the existing bug; it was not a failure introduced by this PR.

The test is reproduced below for reference. It remains local and is not included in the committed changes.

TEST_CASE(testCylinderContainsPoint) {
	for(double radius : {1.0, 2.5}) {
		Shape cylinder = cylinderShape(radius, 4.0);
		ASSERT_TRUE(cylinder.containsPoint(Vec3(0.0, 0.0, 0.0)));
		for(double sign : {-1.0, 1.0}) {
			ASSERT_TRUE(cylinder.containsPoint(Vec3(sign * 0.75 * radius, 0.0, 0.0)));
			ASSERT_TRUE(cylinder.containsPoint(Vec3(0.0, sign * 0.75 * radius, 0.0)));
			ASSERT_FALSE(cylinder.containsPoint(Vec3(sign * 2.0 * radius, 0.0, 0.0)));
			ASSERT_FALSE(cylinder.containsPoint(Vec3(0.0, sign * 2.0 * radius, 0.0)));
			ASSERT_TRUE(cylinder.containsPoint(Vec3(0.0, sign * radius, 0.0)));
			ASSERT_TRUE(cylinder.containsPoint(Vec3(0.0, 0.0, sign * 2.0)));
			ASSERT_FALSE(cylinder.containsPoint(Vec3(0.0, 0.0, sign * 2.5)));
		}
	}
}

Square the Y coordinate in the radial check to correctly classify points inside and outside the cylinder.
@VonTum

VonTum commented Sep 21, 2026

Copy link
Copy Markdown
Member

Oh, you are correct. Well spotted.

@VonTum
VonTum merged commit 6d53170 into ThePhysicsGuys:master Sep 21, 2026
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.

Cylinder containsPoint rejects interior points and accepts exterior points

2 participants