Hello! I found an example where Quake2 models were loaded in three.js. I wanted to do something like a battle royale but I had a little problem. I wanted to use Ammo.js for physics.
I added physics to the ground so that the character can walk on it.
But when I wanted to do the same with the player I got an error.
I use this code for the rigid bodies:
let createMeshRigidBody = function(geometry,mesh,mass) {
// new empty ammo shape
const shape = new Ammo.btConvexHullShape();
//new ammo triangles
let triangle, triangle_mesh = new Ammo.btTriangleMesh;
//new ammo vectors
let vectA = new Ammo.btVector3(0,0,0);
let vectB = new Ammo.btVector3(0,0,0);
let vectC = new Ammo.btVector3(0,0,0);
//retrieve vertices positions from object
let verticesPos = geometry.getAttribute('position').array;
let triangles = [];
for ( let i = 0; i < verticesPos.length; i += 3 ) {
triangles.push({ x:verticesPos[i], y:verticesPos[i+1], z:verticesPos[i+2] })
}
//use triangles data to draw ammo shape
for ( let i = 0; i < triangles.length-3; i += 3 ) {
vectA.setX(triangles[i].x);
vectA.setY(triangles[i].y);
vectA.setZ(triangles[i].z);
shape.addPoint(vectA,true);
vectB.setX(triangles[i+1].x);
vectB.setY(triangles[i+1].y);
vectB.setZ(triangles[i+1].z);
shape.addPoint(vectB,true);
vectC.setX(triangles[i+2].x);
vectC.setY(triangles[i+2].y);
vectC.setZ(triangles[i+2].z);
shape.addPoint(vectC,true);
triangle_mesh.addTriangle( vectA, vectB, vectC, true );
}
let quaternion = mesh.quaternion;
let position = mesh.position;
let transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin( new Ammo.btVector3( position.x, position.y, position.z ) );
transform.setRotation( new Ammo.btQuaternion( quaternion.x, quaternion.y, quaternion.z, quaternion.w ) );
let defaultMotionState = new Ammo.btDefaultMotionState( transform );
let localInertia = new Ammo.btVector3( 0, 0, 0 );
shape.calculateLocalInertia( mass, localInertia );
let RBody_Info = new Ammo.btRigidBodyConstructionInfo( mass, defaultMotionState, shape, localInertia );
let RBody = new Ammo.btRigidBody( RBody_Info );
ammoWorld.addRigidBody( RBody );
mesh.userData.physicsBody = RBody;
rigidBody_List.push(mesh);
};
I call: this.createMeshRigidBody( ground_geom, ground_mesh, 0 );
for the ground.
And this function creates a new player:
createPlayer: function () {
var that = this;
// CHARACTER
player = new THREE.MD2CharacterComplex();
player.scale = 3;
player.controls = controls;
playerConf = Characters[selectedPlayer-1];
var baseCharacter = new THREE.MD2CharacterComplex();
baseCharacter.scale = 3;
baseCharacter.onLoadComplete = function () {
player.shareParts( baseCharacter );
player.enableShadows( true );
player.root.castShadow = true;
if (playerConf.equipWeapon) player.setWeapon( 0 );
player.setSkin( 0 );
player.root.position.x = 0;
player.root.position.z = 0;
scene.add( player.root );
camera.position.set(0,150,-400);
camera2.position.copy(camera.position);
camera.lookAt( player.root.position );
player.root.add( camera );
player.root.add( camera2 );
player.root.add( orbit.target );
gameReady = true;
// physics
var geom = new THREE.Geometry();
// i do this with obj
geom.fromBufferGeometry(player.meshBody);
that.createMeshRigidBody( geom, player.meshBody, 0 ); // i cant get the geometry
that.createControls();
};
baseCharacter.loadParts( playerConf );
}
I use MD2CharacterComplex.js
to create the player. It’s ok if you just want to move it, but i want it to be able to jump, fall, collide with the walls.
please… can someone help me ?.