In this tutorial we will experiment how physics work inside nunuStudio, physics are represented as objects in the editor, physics object dont have a visual representation they are just used for physics simulations.
Physics in nunuStudio are powered by cannon.js developed by schteppe, for more information about the physics engine please consult http://www.cannonjs.org/.
nunuStudio encapsulates the physics bodies in objects, these bodies are managed and updated on a scene basis, physics configuration is managed by scene.
To add physics object select the desired physics object in the left toolbar and add it to the scene, after that add a mesh object as children of the newly created physics object to make it visible in the scene. Dont forget to add a ground object so that other physics object don't fall out of the screen.
Physics object can be configured as dynamic, static or kinenamic objects. Dynamic objects are updated dinamically and detect collisions, kinematic objects are updated only based on their speed, and static objects are not updated at all. Static objects behave likes walls or the floor.
There is a mode property that indicates how the physics coordinates are transformed from the physics world to the scene, the physics world does not consider the parental transformations applied to objects, World positioning is used to just copy world coordinates from physics to the object is fast and the coordinates will match the values stored in the physics body. Sometimes it is usefull to store physics object in the hierarchy, Local positioning can be used to adapt the physics world coordinates to match the scene, it has a small performance impact and the physics coordinates will not match the local transformation of the object.
Bellow we have a comparison between world and local positioned physics objects inside a object hierarchy. As we can obvserve the Local positioned object behaves as we would expect.
You can also download the example project file here, or open it directly inside the Web Editor.
To interact with physics objects using scrips we need to get the physics body attached to that object. Then we can access the body property that exists in every physics object and change the forces, acceleration and velocity values to make it move around the scene.
The following code gets an physics object body and using the keyboard WASD keys and the spacebar we add velocity to that body forcing the cube to move around.
var body;
this.initialize = function()
{
body = scene.getObjectByName("physics").body;
};
this.update = function()
{
if(Keyboard.keyPressed(Keyboard.A))
{
body.velocity.x -= 0.2;
}
if(Keyboard.keyPressed(Keyboard.D))
{
body.velocity.x += 0.2;
}
if(Keyboard.keyPressed(Keyboard.W))
{
body.velocity.z -= 0.2;
}
if(Keyboard.keyPressed(Keyboard.S))
{
body.velocity.z += 0.2;
}
if(Keyboard.keyJustPressed(Keyboard.SPACEBAR))
{
body.velocity.y += 5;
}
};
The code shown above can be seen running bellow for a physics cube, use the WASD keys to control the cube, and the spacebar to make the cube jump. Even when we dont specify rotation for the cube the physics engine automatically calculates it based on the friction beween the surface of the cube and floor. As we can observe the cube tumbles around the scene naturally.
You can also download the example project file here, or open it directly inside the Web Editor.