Page 1 of 1

V2 contact impulse

Posted: Tue Dec 24, 2019 10:26 am
by b1fg2
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

Re: V2 contact impulse

Posted: Tue Dec 24, 2019 7:25 pm
by Norbo
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.

Re: V2 contact impulse

Posted: Wed Dec 25, 2019 4:16 am
by b1fg2
which mean implement this into HandleManifoldForCollidable method? but i dun see any constraint in this demo

Re: V2 contact impulse

Posted: Wed Dec 25, 2019 10:47 pm
by Norbo
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.