bepu / bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.
Apache License 2.0
2.25k stars 261 forks source link

Bug in raycast of cylinder #253

Closed Gatevin closed 9 months ago

Gatevin commented 1 year ago

Hello, I read the code of Raycast function in Cylinder and found something seems like a bug.

What I test

I created a cylinder with radiu = 3 and height = 4, pose is identity and then put a ray with origin = (0, 0, 0) direction = (0, 1, 0), the raytest result is False. If I change direction to (0, 0, 1) the result is True.

The Result Expected

As the ray origin is at the center of cylinder (inside the shape), the raytest result should both be True (hitted).

Discussion

When direction = (0, 1, 0), the ray is parallel to the side of cylinder, thus infinite length cylinder hit test won't get hit position. When do circle hit test, the position of ray origin is not considered.

Below is my test code, I use the released version v2.4.0:

using BepuPhysics;
using BepuPhysics.Collidables;
using System.Numerics;

// radiu=3, length=4, center at (0, 0, 0)
var cylinder = new Cylinder(3, 4);

// ray inside cylinder
var origin = Vector3.Zero;
var directionY = new Vector3(0f, 1f, 0f);
var directionZ = new Vector3(0f, 0f, 1f);

// parallel to the side of cylinder
var isHit = cylinder.RayTest(RigidPose.Identity, origin, directionY, out var t, out var normal);
Console.WriteLine("Ray Direction Y hit={0}, t={1}, normal={2}", isHit, t, normal);

// parallel to the bottom circle of cylinder
isHit = cylinder.RayTest(RigidPose.Identity, origin, directionZ, out t, out normal);
Console.WriteLine("Ray Direction X hit={0}, t={1}, normal={2}", isHit, t, normal);
bug
RossNordby commented 1 year ago

Thanks for the report! I've reproduced it locally; should be able to fix it soon.

RossNordby commented 1 year ago

Should be fixed in 15547f977952b9cb274a0fc19060a03d60784681 (and the soon-to-be-uploaded 2.5.0-beta.11). Thanks again!