Hi everyone. I am having some issues trying physics for the first time. I have looked at the simple examples of physics in the Deep Dive as well as a few from this forum (example: Babylon.js Playground).
You can see the soldier falling through the floor here:
The section of code that loads the soldier is as follows (I raised the soldier’s Y up a little bit so that the soldier drops down towards the floor when the scene loads):
const actor = new Actor(this.scene, 'soldier.glb');
actor.loadActorModel().then((result) => {
this.soldier = result.meshes[0];
this.soldierAnimations = result.animationGroups;
this.soldier.physicsImpostor = new PhysicsImpostor(
this.soldier,
PhysicsImpostor.CylinderImpostor,
{ mass: 91, restitution: 0.2 },
this.scene,
);
// Example print animations:
for (let animation of this.soldierAnimations) {
console.log(animation.name);
}
this.soldierAnimations[0].stop();
this.soldierAnimations[3].start(true);
this.soldier.position.x = 2.0;
this.soldier.position.y = 2.0;
this.soldier.position.z = -2.0;
The section of code that loads the room model is shown below. The creator of the model named all of the meshes that are stored; I am iterating over them and printing their names as this is mostly just a project for my notes/future-reference.
I am attempting to set the PhysicsImpostor on ‘Plane001’ once it shows up in the loop.
I am also temporarily setting the HighlightLayer on ‘Plane001’ so that I can be sure that it looks accurate (hence the red lines shown in the screenshot).
SceneLoader.ImportMesh(
'',
`${location.href}static/`,
'room.glb',
this.scene,
(meshes) => {
this.roomMeshes = meshes;
this.soldier.parent = this.roomMeshes[0];
this.soldier.setParent(this.roomMeshes[0]);
this.roomMeshes[0].addChild(this.soldier);
this.roomMeshes[0].position = new Vector3(0.0, 0.0, 0.0);
// Example print all scene meshes in the baked room model:
for (let node of this.roomMeshes) {
console.log(node.name);
if (node.name === 'Plane001') {
// Example hightlight mesh:
const hLayer1 = new HighlightLayer('hl1', this.scene);
hLayer1.addMesh(node, Color3.Red());
node.physicsImpostor = new PhysicsImpostor(
node,
PhysicsImpostor.BoxImpostor,
{ mass: 0, restitution: 0.2 },
this.scene,
);
}
}
So all loads fine and I think the physics part is close, because when the scene loads the soldier drops from a few feet above the floor and then falls right through the floor.
How can I make it collide like in the simple examples?
Also, I am not so sure of the accuracy of the Impostors that I have chosen (ie: Cylinder vs Box vs Plane etc).
Is there a way to temporarily show the collision boundaries? Similar to the HighlighLayer
technique?
I am a bit new to 3d programming, but I am extremely impressed with the library and excited to learn it Any help would be greatly appreciated!