- Notifications
You must be signed in to change notification settings - Fork2
node-3d/bullet-raub
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is a part ofNode3D project.
npm i -s bullet-raub
This addon is ABI-compatible across Node.js versions.There is no compilation during
npm i
.
Node.js addon providing a Bullet-driven physics API.
This library is a simplified interpretation ofBullet Physics.Only rigid bodies and DOF-6 constraint are supported.
Only rigid body primitives are supported. The API is simplified to just 3 classes:Scene, Body, Joint
.Body can be static or dynamic and with different shapes. Joint is a DOF6 that you can set up inmany possible ways.
SeeTypeScript declarations for more details.
constscene=newScene();scene.gravity=[0,-9.8,0];constframe=()=>{scene.update();// <-- call update() to progress simulation!setTimeout(frame,15);};frame();
Thescene
contains all bodies and joints. It is a high-level wrapper that initializes:
btDefaultCollisionConfiguration
btCollisionDispatcher
btBroadphaseInterface
btConstraintSolver
btDynamicsWorld
Fromscene
you can also run ray hits or traces. It should be possible to use many scenesin parallel.
const{ body}=scene.hit(start,end);
Herebody
is whateverBody
the ray hits first on its path. For subclasses ofBody
,this will respect the dynamic type. I.e. forclass House extends Body {...}
the expressionbody instanceof House
will betrue
.Seescene.hit
andscene.trace
in theexample.
constplane=newBody({ scene});// static boxplane.type='plane';// change shape to ("ground") planeconstbox=newBody({ scene});// 'box' is default typebox.pos=[0,100,0];// 100 meters abovebox.mass=1;// becomes dynamic
Passscene
as a required constructor option. All bodies always exist within their parent scenes.
constjoint=newJoint();joint.minl=[0,1,0];joint.maxl=[0,1,0];joint.a=bodyA;joint.b=bodyB;
This will connectbodyA
andbodyB
and constrain their origins to always be 1 meter apart.You can also change the origin offsets, add rotation constraints and many other joint parameters.
Objects ofScene, Body, Joint
- all have events for every property modification. E.g.as soon as you setscene.gravity=[0,0,0]
, and event will fire forscene.on('gravity', () => {...})
.
You should callscene.update(dt: number = 0.0)
in order for the scene to progress. Heredt
is optional delta time in seconds. If not provided,dt
will be calculated from internal timers.It is during this call toscene.update()
the below'update'
events will fire.
Objects ofBody
will fire'update'
events every tick while not sleeping:
body.on('update',(event)=>{event.pos;// position of body origin (usually center)event.quat;// rotation quaternionevent.vell;// linear velocity (where it goes)event.vela;// angular velocity (how much it spins)});
This is mostly designed to be fed into visualization APIs (such as Three.js), thereforea Quaternion is used to represent rotation. You may need linear/angular velocity forinterpolation, shading, networking, etc.
Objects ofJoint
will fire'update'
events every tick while not sleeping:
joint.on('update',(event)=>{event.posa;// current position of body Aevent.posb;// current position of body Bevent.broken;// boolean, is this joint broken});
Joints can be broken when overwhelmed with impulse. This is controlled byjoint.maximp
-by default it's very high. But you can lower it so that, for instance, car wheels fall offupon extreme impact.
About
Bullet-driven physics API