V2 contact impulse

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
b1fg2
Posts: 16
Joined: Mon Sep 24, 2018 3:36 pm

V2 contact impulse

Post by b1fg2 » Tue Dec 24, 2019 10:26 am

I try to play around with the ContactEventsDemo. Is there any way to get contact impulse for each contact point like unity?
https://docs.unity3d.com/ScriptReference/Collision.html

Norbo
Site Admin
Posts: 4885
Joined: Tue Jul 04, 2006 4:45 am

Re: V2 contact impulse

Post by Norbo » Tue Dec 24, 2019 7:25 pm

Yes, but the impulses aren't available in the narrow phase callbacks since the constraints haven't been solved yet (and they might not even exist yet).

The impulses associated with any constraint handle can be grabbed using Simulation.Solver.GetAccumulatedImpulseMagnitude[Squared] or EnumerateAccumulatedImpulses. To get the constraint handles connected to a given body, you can use the BodyReference.Constraints list.

Not all constraint handles are guaranteed to be contact constraints, though- you could filter them out based on their type by looking up the Simulation.Solver.HandleToConstraint[constraintHandle].TypeId and checking if it's a contact constraint using NarrowPhase.IsContactConstraintType.

If you want to extract more context from contact constraints, you can also use an IDirectContactDataExtractor. It provides the solver's complete view of a contact constraint's state. Here's an example usage:

Code: Select all

                for (int constraintIndex = 0; constraintIndex < body.Constraints.Count; ++constraintIndex)
                {
                    ref var constraint = ref body.Constraints[constraintIndex];
                    ref var location = ref simulation.Solver.HandleToConstraint[constraint.ConnectingConstraintHandle];
                    if (simulation.NarrowPhase.TryGetContactConstraintAccessor(location.TypeId, out var accessor))
                    {
                        accessor.ExtractContactData(constraint.ConnectingConstraintHandle, simulation.Solver, ref contactExtractor);
                    }
                }
Where the contactExtractor is an instance of a struct type implementing IDirectContactDataExtractor. The implementation can be a little hairy since it gives you a lot of degrees of freedom, but it provides a minimum overhead, maximum control view.

b1fg2
Posts: 16
Joined: Mon Sep 24, 2018 3:36 pm

Re: V2 contact impulse

Post by b1fg2 » Wed Dec 25, 2019 4:16 am

which mean implement this into HandleManifoldForCollidable method? but i dun see any constraint in this demo

Norbo
Site Admin
Posts: 4885
Joined: Tue Jul 04, 2006 4:45 am

Re: V2 contact impulse

Post by Norbo » Wed Dec 25, 2019 10:47 pm

That demo is concerned only with knowing a contact was generated, not anything about the resulting contact constraints. You can't collect information about the impulse from those callbacks because the information may not even exist yet- the solver hasn't run.

You can just do the impulse analysis outside of any callback in between timesteps. Nowhere special required.

Post Reply