Calling function before collision callbacks?

Question: Is there a way to call a function right before collision/trigger callbacks, but after contacts were generated?

Background: I am doing a custom physics simulation with all kinematic RBs. I know the position/velocity of each RB before/after my calculations. Then I manually call Physics2D.Simulate to get collision callbacks and contacts. As I understand it, for a normal dynamic RB, simulate starts with old vel/pos, generates contacts based on old vel/pos, solves, changes to new vel/pos, then calls collision callbacks. I have methods that use both ContactPoint2D.relativeVelocity and Rigidbody2D.velocity. With Kinematic RBs, these two are misaligned/different when manually calling Physics2D.Simulate (because kinematic RBs obviously donâ€™t change velocity during simulation). I would like to be able to get in the middle there and modify to make it work more similar to a dynamic RB. I just need a way to reliably execute code right before the first OnTrigger callback.

MelvMay once said collision callback order is undefined ( Continuous Collision Order ), so I donâ€™t know if there is a way to reliably get in there.

Iâ€™m actually not following what you want here and why and Iâ€™ve read it several times now. Sorry.

There is not wa to ask for contacts only or callbacks only right now if thatâ€™s what you mean.

When the callbacks happen, everything has already happened i.e. the simulation step is complete.

Maybe I can break it down a bit with a timeline.

In order:

• In FixedUpdate, I have only kinematic RBs, and I use a custom solver to know their current position, current velocity, next position, and next velocity.
• rb.position = currentPosition; rb.velocity = currentVelocity; rb.MovePosition(nextPosition);
• I manually call Physics2D.Simulate. Everything below happens in order automatically.
• Physics makes contacts. ContactPoint2D.relativeVelocity is based on currentPos and currentVelocity.

• Physics solves, and sets rb.position = newPosition. (It would assign rb.velocity = newVelocity for dynamic RBs, but wonâ€™t for kinematic RBs)

• Physics calls Collision callbacks while rb.position = newPosition, and rb.velocity = currentVelocity (not newVelocity)

I want my codeâ€™s logic to work on my kinematic RBs similar to dynamic RBs, so I want to run code around step 5 to assign rb.velocity = new velocity. This way collision callbacks can do their thing based on what weâ€™d normally have with the rigidbody API for dynamic RBs (ContactPoint2D.relativeVelocities based on currentVelocity, and rigidbody.velocity = newVelocity).

Does this make more sense?

â€”â€”â€”â€”-
EDIT: I think I figured out that CollisionCallbacks do seem to obey execution order of scripts. So I just put a collider out in the middle of nowhere, to always give me a trigger callback. Itâ€™s just an extremely janky workaround.

In short I think youâ€™re saying:

• You want contacts calculated

• You want to adjust the velocities of the RB based upon contacts?

• You then want the callbacks.

I think I follow but no, there isnâ€™t a single callback before you get the physics callbacks themselves.

Have you considered not using the physics callbacks and calling them yourself from your custom solver? You can turn-off the physics callbacks completely which actually improves performance too.

So when the simulation runs, you then iterate all your Rigidbody2D, grab their contacts, perform the solve and perform the callback i.e. your own â€śvoid OnContact()â€ť using SendMessage. Of course, if youâ€™re talking a lot of Rigidbody2D and callbacks this might be worse performance-wise and might be a terrible idea.

Not at all, that execution order isnâ€™t used. The order is completely undefined. Of course there is a logical order but it is undefined and is likely related to the order in which contacts first came in which itself is defined by the order the physic components were activated which itself is defined by the execution order but this is a few levels of indirection and the physics callback order is undefined; it certainly isnâ€™t sorted and is subject to change.

I guess the easiest thing would be to add some callbacks you could hook into but thatâ€™s not something that can be backported as itâ€™s API changing. Iâ€™ve added it as an idea though. We did already have the idea to provide flags in the simulate call so you can select what happens i.e.

`Contacts | Integration | ContactSolve | JointSolve | WriteBack | Callbacks`

1 Like