We’re now going to add some spheres (with an Amiga texture) and some boxes (with a wood texture) to the scene.
These meshes will have physics state set. For instance, this means that they will bounce on the floor if you launch them in the air, bounce between them after a collision has been detected, and so on. The physics engine needs to know which kind of impostor you’d like to use for the mesh (plane, sphere, or box today), as well as the mass and friction properties.
If you’ve chosen Option 1, you can download the two textures here.
Add this code to your project:
- function CreateMaterials() {
- materialAmiga = new BABYLON.StandardMaterial("amiga", scene);
- materialAmiga.diffuseTexture = new BABYLON.Texture("assets/amiga.jpg", scene);
- materialAmiga.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
- materialAmiga.diffuseTexture.uScale = 5;
- materialAmiga.diffuseTexture.vScale = 5;
- materialWood = new BABYLON.StandardMaterial("wood", scene);
- materialWood.diffuseTexture = new BABYLON.Texture("assets/wood.jpg", scene);
- materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
- }
- function addListeners() {
- window.addEventListener("keydown", function (evt) {
- // s for sphere
- if (evt.keyCode == 83) {
- for (var index = 0; index < 25; index++) {
- var sphere = BABYLON.Mesh.CreateSphere("Sphere0", 10, 0.5, scene);
- sphere.material = materialAmiga;
- sphere.position = new BABYLON.Vector3(0 + index / 10, 3, 5 + index / 10);
- sphere.setPhysicsState(BABYLON.PhysicsEngine.SphereImpostor, { mass: 1 });
- }
- }
- // b for box
- if (evt.keyCode == 66) {
- for (var index = 0; index < 10; index++) {
- var box0 = BABYLON.Mesh.CreateBox("Box0", 0.5, scene);
- box0.position = new BABYLON.Vector3(0 + index / 5, 3, 5 + index / 5);
- box0.material = materialWood;
- box0.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, { mass: 4 });
- }
- }
- });
- }
Note: If you need to understand how material works in Babylon.js, watch the module Introduction to WebGL 3D with HTML5 and Babylon.js: Understanding Materials and Inputs, or play with our dedicated Playground sample, Babylon.js Playground: Materials sample.
Add these two lines of code after the scene.enablePhysics line:
- CreateMaterials();
- addListeners();
![]() |
Let’s add another cool feature: the ability to click on one of the objects to throw it away. For that, you need to send a ray from the 2D coordinates of the mouse inside the 3D scene, check whether this ray touches one of the interesting meshes, and if so, apply an impulse force on it to try to move it.
Note: to understand how picking works, please view the MVA module Introduction to WebGL 3D with HTML5 and Babylon.js: Advanced Features. Or play with our online sample, Babylon.js Playground: Picking sample.
Add this code into the addListeners() function:
- canvas.addEventListener("mousedown", function (evt) {
- var pickResult = scene.pick(evt.clientX, evt.clientY, function (mesh) {
- if (mesh.name.indexOf("Sphere0") !== -1 || mesh.name.indexOf("Box0") !== -1) {
- return true;
- }
- return false;
- });
- if (pickResult.hit) {
- var dir = pickResult.pickedPoint.subtract(scene.activeCamera.position);
- dir.normalize();
- pickResult.pickedMesh.applyImpulse(dir.scale(1), pickResult.pickedPoint);
- }
- });
6. Displaying the Bounding Boxes to Better Understand the Whole Story
Finally, we’re going to create a debug scene to let you display/hide the colliders and activate/deactivate the physics properties on them.
We’re going to inject the UI into this div:
- <div id=“lcContainer”>
- <ul id=“listColliders”>
- </ul>
- </div>
- function CreateCollidersHTMLList() {
- var listColliders = document.getElementById(“listColliders”);
- for (var j = 0; j < meshesColliderList.length; j++) {
- var newLi = document.createElement(“li”);
- var chkVisibility = document.createElement(‘input’);
- chkVisibility.type = “checkbox”;
- chkVisibility.name = meshesColliderList[j].name;
- chkVisibility.id = “colvis” + j;
- var chkPhysics = document.createElement(‘input’);
- chkPhysics.type = “checkbox”;
- chkPhysics.name = meshesColliderList[j].name;
- chkPhysics.id = “colphysx” + j;
- (function (j) {
- chkVisibility.addEventListener(
- “click”,
- function (event) {
- onChangeVisibility(j, event);
- },
- false
- );
- chkPhysics.addEventListener(
- “click”,
- function (event) {
- onChangePhysics(j, event);
- },
- false
- );
- })(j)
- newLi.textContent = meshesColliderList[j].name + “ visibility/physx “;
- newLi.appendChild(chkVisibility);
- newLi.appendChild(chkPhysics);
- listColliders.appendChild(newLi);
- }
- function onChangeVisibility(id, event) {
- if (!meshesColliderList[id].isVisible) {
- meshesColliderList[id].isVisible = true;
- meshesColliderList[id].material.alpha = 0.75;
- meshesColliderList[id].material.ambientColor.r = 1;
- }
- else {
- meshesColliderList[id].isVisible = false;
- }
- }
- function onChangePhysics(id, event) {
- if (!meshesColliderList[id].checkCollisions) {
- meshesColliderList[id].checkCollisions = true;
- meshesColliderList[id].setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, { mass: 0,
- friction: 0.5, restitution: 0.7 });
- }
- else {
- meshesColliderList[id].checkCollisions = false;
- meshesColliderList[id].setPhysicsState(BABYLON.PhysicsEngine.NoImpostor);
- }
- }
- }
Call this new function and launch the web project. Now, for instance, display the colliders 12 and 17:
![]() |
![]() |
Please also have a look at this awesome demo built by Samuel Girardin that also uses Oimo.js on some funny characters:
This article is part of the web dev tech series from Microsoft.
Written by David Rousset
If you found this post interesting, follow and support us.
Suggest for you:
Build Responsive Real World Websites with HTML5 and CSS3
Build Professional Websites with HTML5 and CSS3 from Scratch
Advanced HTML5 Tutorial for Web Developers
Coding Made Easy: HTML & CSS For Beginners
Easily Learn HTML 5 From Scratch



No comments:
Post a Comment