bepu / bepuphysics2

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

Raycast problem involving BroadPhase #335

Closed WaneOshin closed 1 month ago

WaneOshin commented 1 month ago

Theres an issue when ray casting a simple ray onto a static cylinder thats on a static big box (used as ground) . . .

image When the ray is further away from the cylinder it detects the cylinder. . . image But when it gets closer it raycasts to the box and ignores the cylinder. . . image However ,When changing the box to be at Y=-1000 , the ray works as intended. . . Code:

private void CreateMap() { var builder = new CompoundBuilder(BufferPool, Simulation.Shapes, 2); builder.Add(new Box(100f, 0.7f, 100f), RigidPose.Identity, 1); builder.Add(new Cylinder(6f,40f),new RigidPose(new Vector3(0, -2.2f, -8),new Quaternion(new Vector3(1,0,0),1)) , 1); var bodyShapeIndex = Simulation.Shapes.Add(new Box(100f, 0.7f, 100f)); var bodyShapeIndex2 = Simulation.Shapes.Add( new Cylinder(4f,40f)); Simulation.Statics.Add(new StaticDescription(new Vector3(0, -1000, 0), bodyShapeIndex)); var r = new RigidPose(new Vector3(0, -4.6f, -8), new Quaternion( 0,0,0.7071068f, 0.7071068f )); Simulation.Statics.Add(new StaticDescription( r, bodyShapeIndex2)); }

is this a BroadPhase detection problem? how to fix it because its a big issue when creating raycast Cars for example. Thank you for the great Physics Engine btw .

RossNordby commented 1 month ago

I haven't been able to reproduce the issue. Is this on latest master? If it is, finding a specific ray and configuration where the failure occurs would help me isolate it.

WaneOshin commented 1 month ago

i cant upload code on git! (ill try send it to ur discord if u give me) I used the "Tank" example of the latest master and added some rays and changed the Map (static objects) to the code i provided in my first comment right above ^^^^.

https://github.com/user-attachments/assets/93f87d66-e529-489b-a972-3155e7534ca8

RossNordby commented 1 month ago

Could you provide the exact configuration of the statics that show the issue, plus a copy-pasted ray origin/direction that shows the issue? (The code provided earlier is helpful, but given my failure to replicate, having exact values may be important.)

WaneOshin commented 1 month ago

I did! I sent the full code to you on discord. did you not receive it?

RossNordby commented 1 month ago

Thanks for the additional information; I think I know what's happening now. With the given configuration and ray, the IRayHitHandler will report two locations. One will be the cylinder, and one will be the box:

Cylinder impact: T = 0.81722957, normal = <-8.863883E-08, 0.74355644, 0.66867316>
Box impact: T = 1.8486543, normal = <0, 1, 0>

If the IRayHitHandler expects that values will always be returned in sorted-by-T order, it may early out after observing the box impact. That would be an error, though, because the IRayHitHandler does not receive strictly sorted results. Instead, it receives results in traversal order. Results are often vaguely depth-sorted because the traversal visits closer nodes first, but that is based on node AABB intersections and cannot guarantee a true ordering.

Likewise, if the IRayHitHandler just accepts every result that is reported without terminating early and stores the reported values to a single field, it will tend to terminate with one of the last intersections.

The grabber has an example of an IRayHitHandler that terminates with the earliest hit: https://github.com/bepu/bepuphysics2/blob/master/Demos/Grabber.cs#L24