Hi,I have a question about Physics engine.I create five cars mesh to display in the game scene,but at the begin of the game,these cars will fly to the sky? If I only create one car,all is ok? The distance of cars is enough big,and they don’t collide each other.I don’t know why?
If I only call CreateMainPlayer(), all is Ok,but if I call CreateOtherPlayers(),all the cars flys to the sky.
I use the physics engie with CannonJSPlugin. And here is the source code:
// add the role
private CreateMainPlayer()
{
let curMap = gameDataMgr.curSceneMap;
let mainPlayer = playerDataMgr.AddGamePlayer(curMap.playerPt,curMap.playerRot,3,this.scene);
mainPlayer.mainPlayer = true;
playerDataMgr.mainPlayer = mainPlayer;
}
//add four other players
private CreateOtherPlayers()
{
let carSpanWidth = 50;
let carSpanHeight = 70;
let mainPlayer = playerDataMgr.mainPlayer;
let playerPos = mainPlayer.playSelf.position;
let postion = new BABYLON.Vector3(playerPos.x - carSpanWidth,playerPos.y,playerPos.z - carSpanHeight);
playerDataMgr.AddGamePlayer(postion,mainPlayer.playSelf.rotation,3,this.scene);
postion = new BABYLON.Vector3(playerPos.x - carSpanWidth,playerPos.y,playerPos.z + carSpanHeight);
playerDataMgr.AddGamePlayer(postion,mainPlayer.playSelf.rotation,3,this.scene);
postion = new BABYLON.Vector3(playerPos.x + carSpanWidth,playerPos.y,playerPos.z - carSpanHeight);
playerDataMgr.AddGamePlayer(postion,mainPlayer.playSelf.rotation,3,this.scene);
postion = new BABYLON.Vector3(playerPos.x + carSpanWidth,playerPos.y,playerPos.z + carSpanHeight);
playerDataMgr.AddGamePlayer(postion,mainPlayer.playSelf.rotation,3,this.scene);
}
//function of adding player to the scene
AddGamePlayer(playerPos:BABYLON.Vector3,playerRot:BABYLON.Vector3,prefabId:number,scene:BABYLON.Scene):void
{
let curMap = gameDataMgr.curSceneMap;
// Create a physics root and add all children
let carDefault = gameDataMgr.carPrefabs[prefabId].node as BABYLON.Mesh;
let playerSelf = new BABYLON.Mesh(“Player”,scene);
let carMeshInst = carDefault.createInstance(“Player”);
carMeshInst.rotationQuaternion = new BABYLON.Quaternion();
playerSelf.addChild(carMeshInst);
let boxCollider = BABYLON.Mesh.CreateBox("Collider",1,scene);
boxCollider.position.y = 5;
boxCollider.position.z = -9;
boxCollider.scaling = new BABYLON.Vector3(15,8,38);
playerSelf.addChild(boxCollider);
// Enable physics on colliders first then physics root of the mesh
boxCollider.physicsImpostor = new BABYLON.PhysicsImpostor(boxCollider,BABYLON.PhysicsImpostor.BoxImpostor,{mass:0},scene);
playerSelf.physicsImpostor = new BABYLON.PhysicsImpostor(playerSelf,BABYLON.PhysicsImpostor.NoImpostor,{mass:5},scene);
boxCollider.isVisible = false;
playerSelf.position = MathUtil.CloneVector3(playerPos);
playerSelf.rotationQuaternion = BABYLON.Quaternion.FromEulerVector(playerRot);
playerSelf.isPickable = false;
}
