Uncaught TypeError: Cannot read property 'calculateWorldAABB'

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Babylon Template</title>

        <style>
            html, body {
                overflow: hidden;
                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
            }

            #renderCanvas {
                width: 100%;
                height: 100%;
                touch-action: none;
            }
        </style>

        <script src="https://preview.babylonjs.com/babylon.js"></script>
		
		 <script src="https://cdn.babylonjs.com/cannon.js"></script>
        <script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>


        <script src="https://code.jquery.com/pep/0.4.3/pep.js"></script>


    </head>

   <body>

    <canvas id="renderCanvas" touch-action="none"></canvas> <!-- touch-action="none" for best results from PEP -->

    <script>
	
	



        var canvas = document.getElementById("renderCanvas"); // Get the canvas element
        var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine

        /******* Add the create scene function ******/
        var createScene = function () {
			
            // Create the scene space
            var scene = new BABYLON.Scene(engine);
			var gravityVector = new BABYLON.Vector3(0,-9.81, 0);
			var physicsPlugin = new BABYLON.CannonJSPlugin();
			scene.enablePhysics();
			
			
			
			
            // Add a camera to the scene and attach it to the canvas
            var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 2, new BABYLON.Vector3(0,0,5), scene);
            camera.attachControl(canvas, true);

            // Add lights to the scene
            var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
            var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);

            // Add and manipulate meshes in the scene
            var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter:2}, scene);
			
			
			
            return scene;
        };
        /******* End of the create scene function ******/

        var scene = createScene(); //Call the createScene function

		
		
		
		// Keyboard events
		var inputMap ={};
		scene.actionManager = new BABYLON.ActionManager(scene);
		scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, function (evt) {								
			inputMap[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
		}));
		scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) {								
			inputMap[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
		}));
	
		// load char
		//BABYLON.SceneLoader.Append("3d/", "char.glb", scene, function (scene) {
		BABYLON.SceneLoader.ImportMesh("", "3d/", "char.glb", scene, function (newMeshes, particleSystems, skeletons) {
			scene.onBeforeRenderObservable.add(()=>{
				 
				character = newMeshes[0];

				character.physicsImpostor = new BABYLON.PhysicsImpostor(character, BABYLON.PhysicsImpostor.MeshImpostor, {mass: 0, friction: 0, restitution: 0.3});


				var keydown = false;
				var shift = (inputMap["Shift"] ? 0.4 : 0);	// If shift is pressed 0.4 else 0			
						
				if(inputMap["w"]){						
					character.position.z += (0.1 + shift );
					keydown=true;
				    
				} 
				if(inputMap["a"]){
					character.position.x -= (0.1 + shift );
					keydown=true;
				} 
				if(inputMap["s"]){
					character.position.z -= (0.1 + shift );
					keydown=true;
				} 
				if(inputMap["d"]){
					character.position.x += (0.1 + shift );
					keydown=true;
				}
				var keydown = false;
			});
		});
		
		BABYLON.SceneLoader.ImportMesh("", "3d/", "level_1.glb", scene, function (newMeshes, particleSystems, skeletons) {
			
		});
		
		
		
		    scene.gravity = new BABYLON.Vector3(0, -0.9, 0);

			// Enable Collisions
			scene.collisionsEnabled = true;
			
			scene.enablePhysics();

			

        // Register a render loop to repeatedly render the scene
        engine.runRenderLoop(function () {
                scene.render();
        });

        // Watch for browser/canvas resize events
        window.addEventListener("resize", function () {
                engine.resize();
        });
		
		
		
    </script>

   </body>

</html>

Hello bug at line

			character.physicsImpostor = new BABYLON.PhysicsImpostor(character, BABYLON.PhysicsImpostor.MeshImpostor, {mass: 0, friction: 0, restitution: 0.3});
  1. i load cannon
  2. load model (mesh)
  3. and when i attac physycs have error text:

Uncaught TypeError: Cannot read property ‘calculateWorldAABB’ of undefined
at Body.computeAABB (cannon.js:5756)
at Body.updateMassProperties (cannon.js:5930)
at Body.addShape (cannon.js:5693)
at e.generatePhysicsBody (babylon.js:16)
at e.addImpostor (babylon.js:16)
at e._init (babylon.js:16)
at new e (babylon.js:16)
at r.callback (index.html:99)
at e.notifyObservers (babylon.js:16)
at t.render (babylon.js:16)

please help. thanks

A few things-

First, if you create a physics plugin, you will need to reference it when enabling physics.
Second - would you be able to reproduce it in the playground using the same model?

1 Like

And after looking carefully at the code - This code will run on each frame, which I am sure you didn’t mean to:

scene.onBeforeRenderObservable.add(()=>{
				 
				character = newMeshes[0];

				character.physicsImpostor = new BABYLON.PhysicsImpostor(character, BABYLON.PhysicsImpostor.MeshImpostor, {mass: 0, friction: 0, restitution: 0.3});


				var keydown = false;
				var shift = (inputMap["Shift"] ? 0.4 : 0);	// If shift is pressed 0.4 else 0			
						
				if(inputMap["w"]){						
					character.position.z += (0.1 + shift );
					keydown=true;
				    
				} 
				if(inputMap["a"]){
					character.position.x -= (0.1 + shift );
					keydown=true;
				} 
				if(inputMap["s"]){
					character.position.z -= (0.1 + shift );
					keydown=true;
				} 
				if(inputMap["d"]){
					character.position.x += (0.1 + shift );
					keydown=true;
				}
				var keydown = false;
			});