Graphics Reference
In-Depth Information
4. The final step we need to take is to update the render loop:
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
scene.simulate();
}
Here, we add the scene.simulate function. This is used to calculate the
new positions of all the objects that have been wrapped in a Physijs library
specific mesh.
With these basic steps, you've got a fully working physics-enabled Three.js scene.
An important aspect to take into account when using this engine is that there is a
hit on performance. For each object of the scene, Physijs will need to calculate
its next position and rotation. This works great for tens of objects, but you'll see a
severe hit when working with hundreds of Physijs-managed objects.
How it works…
We call scene.simulate() , which we added to the render loop in step 4, for
each frame that is rendered. When this function is called, Physijs will look at all the
objects it knows about, and it also looks at the gravity configured on the scene and
will use that information to calculate new positions and rotations for each object if
collisions between objects occur. it will use the friction and restitution prop-
erties of the Physijs material and the weight function of an object to determine how
that object and the one it collides with should react. This is repeated in each render
loop and gives the simulation of real physics in the scene.
There's more…
What we've done in this recipe is only a very small part of what is possible with
this physics engine. You can find more information on the Physijs website at ht-
tp://chandlerprall.github.io/Physijs/ . Interesting subjects from that site are:
• Support for different object shapes: https://github.com/chandlerprall/Physijs/
wiki/Basic-Shapes .
Search WWH ::




Custom Search