freezy / VisualPinball.Engine

:video_game: Visual Pinball Engine for Unity
https://docs.visualpinball.org
GNU General Public License v3.0
396 stars 62 forks source link

Add Kinematic Colliders #460

Closed freezy closed 10 months ago

freezy commented 10 months ago

This PR adds support for kinematic colliders. It closes #322.

The behavior is quite similar to Unity's kinematic rigidbody, i.e. it's not part of the simulation, but can transform freely with the colliders being transformed along.

https://github.com/freezy/VisualPinball.Engine/assets/70426/3d92a2b4-cb67-4f2b-85d0-4bb8f49607ce

How it works

We use the items' transformation matrix as a source. That means a component that transforms an item must go through the VPE-specific data in order to apply the transformation. That also means that just transforming it through Unity's Transform won't automatically update the colliders.

The rest is automatic. For example, if your component moves an object each frame by changing the primitive's X-position, the physics engine now automatically updates the colliders on each frame.

Caveats

It would be great if the physics engine supported parenting, i.e. transforming a parent object whose children are items that would then be automatically updated. VPX doesn't support this, but here it would make a lot of sense.

It would also be great if moving colliders came with a velocity. Right now, kinematic colliders cannot be used for continuous contact like a ball in a rotating gun, since the gun colliders don't push the ball away when they collide with it (they are literally teleported from position n to n+1 each frame).

Both of these caveats will eventually be addressed, but aren't part of this PR.

Implementation

The approach is the following:

TODO before merging

This PR and various comments on Discord made me realize that the next step before getting support for all items is to properly handle parenting. For example, all dragpoint-based items (rubbers, walls, ramps, etc) currently don't transform at all in VPX. They are uniquely defined by their dragpoint's (global) position. The first step would be to make all items transformable, i.e. make their colliders transformable. Once that's possible, the next step is parenting, and then we get kinematic support for all of these items basically for free.