Unable to correctly align the x, y and z positioning from a gltf import, i’m trying to add physics to the scene by using box geometry from blender, then looping through the boxes and converting to Ammo.btBoxShape. Can anyone help me with this, it looks terrible.
The rotation and scale seem ok, but the position is far off.
What i need
This my import statement, from GLTFLoader
// Load a glTF resource
this.gltfLoader.load(
// resource URL
'basic_map_ground.gltf',
// called when the resource is loaded
function ( gltf ) {
let scene = gltf.scene;
this.scene.add( scene );
this.scene.updateMatrixWorld(true);
this.scene.updateMatrix();
for(let i = 0; i < gltf.scene.children.length; i++){
let platform = gltf.scene.children[i];
platform.geometry = (new THREE.Geometry()).fromBufferGeometry(platform.geometry);
var box = new THREE.Box3(platform.geometry.boundingBox.min, platform.geometry.boundingBox.max);
const center = box.getCenter(new THREE.Vector3());
let size = box.getSize(new THREE.Vector3());
let pos = {x: platform.position.x , y: platform.position.y, z: platform.position.z };
let scale = {x: size.x, y: size.y, z: size.z};
let quat = {x: platform.quaternion.x, y: platform.quaternion.y, z: platform.quaternion.z, w: platform.quaternion.w}; // make flat within three.js
let mass = 0; // 0 = static position
let planeGeometry = new THREE.BoxBufferGeometry();
let planeMaterial = new THREE.MeshLambertMaterial({color: 0x9ff0f0});
let plane = new THREE.Mesh(planeGeometry, planeMaterial, 0 );
plane.receiveShadow = true;
plane.castShadow = true;
var quaternion = new THREE.Quaternion(quat.x, quat.y, quat.z, quat.w);
plane.applyQuaternion(quaternion);
plane.position.set(pos.x, pos.y, pos.z);
plane.scale.set(scale.x, scale.y, scale.z);
plane.__dirtyRotation = true;
plane.__dirtyPosition = true;
// physics
let transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) );
let motionState = new Ammo.btDefaultMotionState( transform );
let colShape = new Ammo.btBoxShape( new Ammo.btVector3( scale.x * 0.5, scale.y * 0.5, scale.z * 0.5 ) );
colShape.setMargin( 0.05 );
let localInertia = new Ammo.btVector3( 0, 0, 0 );
colShape.calculateLocalInertia( mass, localInertia );
let rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, colShape, localInertia );
let body = new Ammo.btRigidBody( rbInfo );
this.scene.add( plane )
this.physicsWorld.addRigidBody( body, this.colGroupPlane, this.colGroupRedBall );
}
}.bind(this),
// called while loading is progressing
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened', error );
}
);