Verge3D Physics Guide
Verge3D comes with a physics engine that can make you 3D objects behave like in the real world. This feature is especially useful for creating 3D games and AR/VR applications.
As physics backend we use the open source physics engine Bullet, which web version is called Ammo.js.
Contents
- Getting Started With Physics
- Using Puzzles
- General Recommendations
- Creating First-Person Character
- Simulating Car Physics
Getting Started With Physics
Before you can use physics, you must add the physics module to your application. You can do this at the app creation stage using the corresponding option in the App Manager.
Alternatively, you can add the physics module to an existing app. To do this, copy the files ammo.wasm.js and ammo.wasm.wasm from the build folder of Verge3D installation folder to your app folder.
Using Puzzles
Familiarize yourself with the physics puzzles.
General Recommendations
- Be sure to apply scale in your modeling suite as the physics engine just ignores the scale of objects.
- For best results, the origin of your object should coincide with the center of mass. To make it so e.g. in Blender, use the operator Object → Set Origin → Origin to Center of Mass (Volume).
- Never position or animate dynamic body directly. Instead, apply force/impulse/velocity to it using the apply body param puzzle or snap it to another object with the snap body puzzle.
- As performance optimization, Verge3D disables dynamic bodies after a period of inactivity. You can activate these bodies again by applying some vector, colliding them with other bodies or using the activate feature of the set body state puzzle. If you need your body to be active all the time, apply disable deactivation state to it using the same puzzle.
- Enable continuous collision detection (CCD) technique to improve simulation quality of quickly moving bodies, such as projectiles, pinball/billiard/bowling balls etc. To do that, set ccd motion threshold and ccd swept sphere radius parameters on that bodies. See here for more info.
- Keep in mind, that you can't detect collisions between static/static, static/kinematic, or kinematic/kinematic objects. If using a dynamic body is not an option, consider assigning ghost physics to implement proximity detection of the moving non-dynamic object with static/kinematic environment.
Creating First-Person Character
First-person character mode can be convenient not only for the shooter and RPG games but for any applications which require investigating large environments. It can be architecture visualizations, e-learning apps and interactive guides, museums and other cultural facilities, and even 3D virtual marketplaces. Besides, you can integrate the first-person character in your VR application making it much more realistic and fun!
For usage example, check out the Snowballs VR demo (also available in the Asset Store).
There are many possible ways to implement FPS mode, but for non-expert Verge3D users we recommend you to follow this short guide to achieve results quicker.
Create a capsule-shaped object which represents your character. Tune its height and width to look more realistic. Make sure you set the center of your object correctly.
In puzzles, assign dynamic capsule-shaped body to the character and disable its rotational degrees of freedom. This way, you allow your character to move but not rotate. Also, apply disable deactivation to your capsule.
Since character needs a floor (+walls) or ground to walk on, create such bodies using the corresponding create physics body puzzles.
The next step is configuring the camera. The camera won't have any physics assigned since we only need it to be our "eyes." Set the First-Person controls to it but don't assign any collision material (we'll be using full-featured collision detection provided by the physics engine, not basic floor/walls simulation). Also, uncheck the Allow Panning and set Movement Speed value to zero:
Now we need to attach your camera to the character's "head." There is no need to create a model for the head, add an empty object called "Head" to the scene, and parent it to the upper part of the capsule model. This way the "head" moves with the character.
By using the additional "Head" object you can attach/detach the camera to/from the character in runtime. To attach the camera use the parent and snap to object puzzles:
To detach the camera, use the parent puzzle with empty to object field.
Character created! Now it's time to make it move in your scene. In the most basic case, we need to move forward when the user press the up-arrow key. To do so, apply linear velocity vector to your character, which is calculated from the horizontal camera direction. To make your simulations robust you should always apply vectors using the on simulation tick puzzle.
To move your character on mobile device, you'll need additional "controller" elements. You can create them using 3D objects parented to the camera or with HTML elements.
To move your character in VR mode, you can use the get controller property and get gamepad property puzzles to obtain status of VR controller buttons/axes.
Simulating Car Physics
Realistic simulation of vehicle physics can be really useful in applications like interactive car customizers or racing games.
For usage example, check out the Arcade Racing demo (also available in the Asset Store).
Got Questions?
Feel free to ask on the forums!