JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/p2.js/0.7.1/p2.js"></script>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<canvas id="renderCanvas" touch-action="none"></canvas>
CSS
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
JavaScript
var world, boxBody, planeBody, sphere;
var controls = {left:false,right:false,jump:false};
var dynamicBodies = [], staticBodies = [], dynamicMeshes = [], staticMeshes = [];
var frictionContactMaterial;
var canvas = document.getElementById("renderCanvas"); // Get the canvas element
var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
var createScene = function () {
// Create the scene space
var scene = new BABYLON.Scene(engine);
var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2 - 0.4, Math.PI / 3, 16, new BABYLON.Vector3(10,0,0), scene);
camera.attachControl(canvas, true);
var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
//var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter:2}, scene);
var sphere = BABYLON.MeshBuilder.CreateBox("ground", 1, scene);
sphere.scaling = new BABYLON.Vector3(1,1,1);
var ground = BABYLON.MeshBuilder.CreateBox("ground", 1, scene);
ground.position.y -= 0.5;
ground.scaling = new BABYLON.Vector3(50,1,3);
createP2World();
scene.registerBeforeRender(function(){
if(world){
if(controls.right){
boxBody.velocity[0] = 3;
} else if(controls.left){
boxBody.velocity[0] = -3;
} else {
boxBody.velocity[0] = 0;
}
world.step(1/60);
console.log(boxBody.position[0]);
sphere.position.x = boxBody.position[0];
sphere.position.y = boxBody.position[1];
for(var i=0;i<dynamicBodies.length;i++){
if(dynamicMeshes[i]){
dynamicMeshes[i].position.set(dynamicBodies[i].position[0],dynamicBodies[i].position[1],0);
}
}
}
});
return scene;
};
var scene = createScene();
function createP2World(){
world = new p2.World({
gravity : [0, -10]
});
planeShape = new p2.Plane();
planeShape.material = new p2.Material();
planeBody = new p2.Body();
...