Cameras
To allow user input, a camera must be attached to the canvas using:
1 | camera.attachControl(canvas, true); |
The second parameter is optional and defaults to false, which prevents default actions on a canvas event. Set to true to allow canvas default actions.
Universal Camera
This camera is controlled by the keyboard, mouse, touch, or gamepad depending on the input device used, with no need for the controller to be specified. The UniversalCamera has the same functionality as Free Camera but also adds built-in support for Touch and Gamepads.
1 | // Parameters : name, position, scene |
Arc Rotate Camera
This camera always points towards a given target position and can be rotated around that target with the target as the center of rotation. It can be controlled with cursors and mouse, or with touch events.
1 | // Parameters: name, alpha, beta, radius, target position, scene |
Follow Camera
Give it a mesh as a target, and from whatever position it is currently at, this camera will move to a goal position from which to view the target. When the target moves, so will the Follow Camera.
1 | // Parameters: name, position, scene |
Anaglyph Camera
The AnaglyphUniversalCamera and AnaglyphArcRotateCamera extend the use of the Universal and Arc Rotate Cameras for use with red and cyan 3D glasses. They use post-processing filtering techniques.
1 | // Parameters : name, position, eyeSpace, scene |
The eyeSpace parameter sets the amount of shift between the left-eye view and the right-eye view. Once you are wearing your 3D glasses, you might want to experiment with this float value.
Device Orientation Camera
The DeviceOrientationCamera is specifically designed to react to device orientation events such as a modern mobile device being tilted forward, back, left, or right.
1 | // Parameters : name, position, scene |
Fly Camera
FlyCamera imitates free movement in 3D space, think “a ghost in space.” It comes with an option to gradually correct Roll, and also an option to mimic banked-turns.
Its defaults are:
Keyboard - The A and D keys move the camera left and right. The W and S keys move it forward and backward. The E and Q keys move it up and down.
Mouse - Rotates the camera about the Pitch and Yaw (X, Y) axes with the camera as the origin. Holding the right mouse button rotates the camera about the Roll (Z) axis with the camera as the origin.
1 | const camera = new BABYLON.FlyCamera("FlyCamera", new BABYLON.Vector3(0, 5, -10), scene); |
Camera Collisions
Define and apply gravity
In the real world, gravity is a force (ok, sort of) that is exerted downward — i.e., in a negative direction along the Y-axis. On Earth, this force is roughly 9.81m/s². Falling bodies accelerate as they fall, so it takes 1 second to fully reach this velocity, then the velocity reaches 19.62m/s after 2 seconds, 29.43m/s after 3 seconds, etc. In an atmosphere, wind drag eventually matches this force and velocity ceases to increase (“terminal velocity”).
Babylon.js follows a much simpler gravitational model, however — scene.gravity represents a constant velocity, not a force of acceleration, and it is measured in units/frame rather than meters/second. As each frame is rendered, the cameras you apply this gravity to will move by the vector’s value along each axis (usually x and z are set to 0, but you can have “gravity” in any direction!), until a collision is detected.
If you need a more accurate representation of gravitational (or other) forces, you can use the physics engines