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

NaNs after collision bug ? #311

Closed aymen157 closed 4 months ago

aymen157 commented 4 months ago

same question as this old one (2011) : https://forum.bepuentertainment.com/viewtopic.php?t=1467

hello. kind of new to bepu. im struggling building a simple ball that falls onto a plane. the problem is the ball falls ok, but then when it hits the plane (exactly when narrow phase called) its position value goes to <Nan,Nan,Nan>. why so? (im using the Demo callbacks from the git) here's the function


Vector3 planePosition = Vector3.Zero;
float planeSize = 10f;
var planeShape = new Box(planeSize, 0.01f, planeSize);
var planeCollidable = new CollidableDescription(simulation.Shapes.Add(planeShape), 0.1f);
var planeStaticDesc = new StaticDescription(planePosition, Quaternion.CreateFromYawPitchRoll(0,Mathf.PI/4,0), planeCollidable.Shape, ContinuousDetection.Discrete);
simulation.Statics.Add(planeStaticDesc);

float ballRadius = 0.5f;
var ballShape = new Sphere(ballRadius);
var ballCollidable = new CollidableDescription(simulation.Shapes.Add(ballShape), 0.1f);
var ballPosition = new Vector3(0, 10, 0);
var ballVelocity = new Vector3(0, -1, 0);
var ballBodyDescription = new BodyDescription
{
    LocalInertia = ballShape.ComputeInertia(1f),
    Pose = new RigidPose { Position = ballPosition },
    Velocity = new BodyVelocity { Linear = ballVelocity },
    Collidable = ballCollidable,
};
simulation.Bodies.Add(ballBodyDescription);
RossNordby commented 4 months ago

Repeating from the discord chat for others: The orientation quaternion is default initialized and so has zero length. Only unit length quaternions are orientations, so a zero length quaternion causes an explosion.

Using a constructor like new RigidPose(ballPosition) or the implicit conversions like Pose = ballPosition would avoid this by initializing the orientation to identity.

This kind of bug is often easier to isolate when running the source in debug mode; there are tons of asserts that will catch oopsies like this.