Physics (i'm using havok) doesn't let animation to play

my code :

**

import { Animation, ArcRotateCamera, Engine, HavokPlugin, MeshBuilder, PhysicsAggregate, PhysicsBody, PhysicsMotionType, PhysicsShapeType, Scene, Vector3 } from “@babylonjs/core”;
import HK from “@babylonjs/havok”;

const canvas = document.getElementById(‘app’) as HTMLCanvasElement;
const engine = new Engine(canvas , true , {} , true);

const createScene = async function () {
const scene = new Scene(engine);
const hk = await HK();
const havokPlgin = new HavokPlugin(true , hk)
scene.enablePhysics(new Vector3(0 , -9.8 , 0) , havokPlgin)

scene.createDefaultLight(true);
const camera = new ArcRotateCamera(‘cam’ , 0 , 1.2 , 5 , new Vector3(0 , 0 , 0));
camera.attachControl(true)

const ground = MeshBuilder.CreateBox(‘ground’ , {
width : 25,
depth : 2
} );

const player = MeshBuilder.CreateBox(‘player’ , {size : 0.5});
player.position.y = 1;
player.position.x = 3;

const playerBody = new PhysicsBody(player , PhysicsMotionType.ANIMATED , false , scene);
playerBody.setMassProperties({mass :1})

const playerParent = MeshBuilder.CreateBox(‘playerParent’ , {size : 0.5});
playerParent.position.y = 1;
playerParent.position.x = 3;

player.setParent(playerParent);
console.log(player.parent);

ground.position.x = -7;
const groundBody = new PhysicsAggregate(ground , PhysicsShapeType.BOX);

const playerAni = new Animation(‘moveForward’ ,
‘position.x’ , 30 , Animation.ANIMATIONTYPE_FLOAT , Animation.ANIMATIONLOOPMODE_CONSTANT);

const keyFrames = [
{
frame : 0,
value : 2.5
},
{
frame : 360,
value : -50
}
]
playerAni.setKeys(keyFrames);
playerParent.animations.push(playerAni);
scene.beginAnimation(playerParent , 0 , 360);

return scene; 

};

const scene = await createScene();

engine.runRenderLoop(()=>{
scene.render();
});

window.addEventListener(‘resize’ , ()=>{
engine.resize();
}); **

i’m just trying to create a game i which there is a ground on which player moves forward and there are many enemies if player collide with any enemy game will end.

Hello @Aman_Raj_Aryan and welcome to the forum !!!

Could you repro your error in the playground instead https://playground.babylonjs.com/

cc @carolhmj and @Cedric

Hi @Aman_Raj_Aryan

Did you try to set PhysicsBody | Babylon.js Documentation to false?

@Cedric but now how can I detect collision

You probably want the motionType to be Dynamic.
Animated is for things like doors, elevators and other objects that should always reach their destination despite of any collisions.
That means the animated motionType will ignore collisions with other animated aswell as static bodies, and it will push dynamic bodies out of the way.

After setting motion type dynamic player cube doesn’t stops at top ground instead it passes through ground

If you can reproduce in a Playground we can help you better.

1 Like

Edit: Ohh I just seen the link to documentation in the comment section :slight_smile:

I set this mesh.physicsBody.disablePreStep = false;
and the animation is working again.
I read that this setting may occur performance issue, so i don’t know if this is a good answer.

There is a performance issue that is small if you are updating a few objects with preStep.
The idea behind this flag is to not have this performance issue for every mesh, by default.