TL;DR: demo1, demo2 (controls: Mouse, A, D)
Historically I prefer to use custom physics simulation instead of third party engines. It started from igdc.ru contests in 2008 when I found the PDF from Hitman developers (see links at the bottom). The method is known as position based or Verlet physics. Almost every my further game was made using those physics. Unfortunately, I didn’t manage to implement collision response between dynamic convexes for that moment so my games had some limitations.
As for me, this approach is much easier to understand than impulse based physics.
Everything is built on particles and connections between them (constraints). For example, triangle convex is made of 3 particles and 3 fixed distance constraints.
You don’t have to deal with angular velocity and linear velocity. Actually, there is no explicit velocity: particles only store position and previous position.
If you want to simulate a softbody – just solve constraints in several steps instead of one. Physics works best with a fixed time step.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * @class Particle */ module.exports = Class.extend({ position: null, prevPosition: null, acceleration: null, mass: null, invMass: null, enabled: true, .... }); |
Collision response is done by moving the particles penetrated into the body out of it so that the particles touch the body without penetration.
In the example particle a must be moved to lay on the bc. If both bodies are dynamic – bc must be moved as well. It’s a little tricky because b and c particles must be moved by different distance (it depends on the distance to the a particle projection on the bc). The method is greatly explained in the Advanced Character Physics.
I decided to implement the physics in Javascript and port one of my early games to the browser – just for fun This time I managed to implement collision response for dynamic convexes using the separating axis theorem (please see the demo1). I use sweep and prune broadphase just because it’s easy. The friction is based on the penetration depth. Demo2 shows loading of the original game binary level format and basic rope implementation (click the mouse to shoot). Current debug render is done via WebGL (I’m going to make multiple light sources and shadows in future so canvas is not enough). There are still issues with stability in some rare cases (e.g. fast moving convex collides with static geometry), but I’m going to fix it.
If you want to implement it yourself, read this two articles:
My work in progress sources are available on the github: https://github.com/Division/bok
Physics: https://github.com/Division/bok/blob/master/app/core/verlet-physics/VerletPhysics.js