I am trying to filter out specific bodies during raycasting. While searching for solutions I found this question asked before: viewtopic.php?f=4&t=2565&p=14245, but I can't seem to get it working, as the RayCast does not register any hits at all. My goal is to have a ragdoll standing upright by keeping the head up with a OneBodyLinearServo, with the Target set 2 meters above ground. The ground is a Box located at (0, 0, 0) with dimensions of (64, 1, 64).
I have fetched the IRayHitHandler from the Grapper class in the Demos, and modified it to this:
Code: Select all
struct RagdollRayHitHandler : IRayHitHandler
{
public Ragdoll ragdoll;
public float T;
public CollidableReference HitCollidable;
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public bool AllowTest (CollidableReference collidable)
{
return !ragdoll.bodies.Contains (collidable.Handle); <-- I have tried filtering out collidables here, if they are a part of the ragdoll
}
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public void OnRayHit (in BepuPhysics.Trees.RayData ray, ref float maximumT, float t, in Vector3 normal, CollidableReference collidable)
{
//We are only interested in the earliest hit. This callback is executing within the traversal, so modifying maximumT informs the traversal
//that it can skip any AABBs which are more distant than the new maximumT.
if (t < maximumT)
maximumT = t;
if (t < T)
{
//Cache the earliest impact.
T = t;
HitCollidable = collidable;
}
}
}
Here is how I cast the ray:
Code: Select all
simulation.Bodies.GetDescription (ragdoll.handles.Head, out BodyDescription head);
RagdollRayHitHandler rayHit = new RagdollRayHitHandler ();
rayHit.ragdoll = ragdoll;
rayHit.T = 2;
simulation.RayCast (head.Pose.Position, new Vector3 (0, -1, 0), 2, ref rayHit);
if (rayHit.T < 2)
{
Vector3 target = head.Pose.Position + new Vector3 (0, -1 * rayHit.T + 2, 0);
if (!ragdoll.hasMotor)
ragdoll.AddUprightMotor (target, simulation);
else
ragdoll.UpdateUprightMotor (target, simulation);
} else
ragdoll.RemoveUprightMotor (simulation);
I have verified that the Ragdoll.AddUprightMotor method is working, as I could fix the ragdoll to a specific Target when it was created in earlier tests.