kripken / ammo.js

Direct port of the Bullet physics engine to JavaScript using Emscripten
Other
4.19k stars 559 forks source link

When the clone model is introduced and added to the AMMO world, the position parameter becomes NaN #403

Open doumuer opened 2 years ago

doumuer commented 2 years ago

Hello, I have one problem when building a multiplayer online game using three.js+ AMmo.js. The problem is as follows. When the second player joining the game, the actual position of the model is changed to NaN duiring the clone model being introduced to add to the ammo physics scene with the correct position parameters. Could you please tell me what is the reason for that? How can I solve this situation? Finally, attaching the code:

websocket.onmessage = function(evt) { let obj = JSON.parse(evt.data); for(var o in obj){ if(o == local_name){

    }else{
        if(!all_player_json.hasOwnProperty(o)){
            let temp_json = {};
            temp_json['x'] = obj[o]['x'];
            temp_json['y'] = obj[o]['y'];
            temp_json['z'] = obj[o]['z'];
            temp_json['mesh'] = mesh_horse.clone();
            pos.set( temp_json['x'],5, temp_json['z'] );
            quat.set( 0, 0, 0, 1 );
            console.log("newMesh:",temp_json['x'], temp_json['y'], temp_json['z']);
            temp_json['bt_body'] = inputWorld(temp_json['mesh'],pos,quat);
            all_player_json[o] = temp_json;
        }else{
            all_player_json[o]['x'] = obj[o]['x'];
            all_player_json[o]['y'] = obj[o]['y'];
            all_player_json[o]['z'] = obj[o]['z'];
            all_player_json[o]['rotation'] = obj[o]['rotation'];
            all_player_json[o]['directionX'] = obj[o]['directionX'];
            all_player_json[o]['directionY'] = obj[o]['directionY'];
            all_player_json[o]['directionZ'] = obj[o]['directionZ'];
            all_player_json[o]['force_Scalar'] = obj[o]['force_Scalar'];
        }
    }
}
for(var o in all_player_json){ 
    if(!obj.hasOwnProperty(o)){
        removeDebris(all_player_json[o]['mesh']);
        delete all_player_json[o];
    }
}

};

function inputWorld(mesh,position,Quaternary){ const copy_geometry = new THREE.BufferGeometry(); copy_geometry.copy(mesh.geometry); copy_geometry.scale(0.01,0.01,0.01); console.log(position); mesh.position.copy(position); console.log("inputWorld",mesh.position); const shape = createConvexHullPhysicsShape(copy_geometry.attributes.position.array); shape.setMargin( margin ); const bt_body = createRigidBody( mesh, shape, 200, position, Quaternary ); bt_body.setDamping(0.8,1); return bt_body; }

function createConvexHullPhysicsShape( coords ) { const shape = new Ammo.btConvexHullShape(); for ( let i = 0, il = coords.length; i < il; i += 3 ) { tempBtVec3_1.setValue( coords[ i ], coords[ i + 1 ], coords[ i + 2 ] ); const lastOne = ( i >= ( il - 3 ) ); shape.addPoint( tempBtVec3_1, lastOne ); } return shape; }

function createRigidBody( object, physicsShape, mass, pos, quat, vel, angVel ) { if ( pos ) { console.log("createRigidBody1",pos); object.position.copy( pos ); console.log("createRigidBody2",object.position); } else { pos = object.position; } if ( quat ) { object.quaternion.copy( quat ); } else { quat = object.quaternion; } const 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 ) ); const motionState = new Ammo.btDefaultMotionState( transform ); const localInertia = new Ammo.btVector3( 0, 0, 0 ); physicsShape.calculateLocalInertia( mass, localInertia ); const rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, physicsShape, localInertia ); const body = new Ammo.btRigidBody( rbInfo ); body.setFriction( 0.5 ); if ( vel ) { body.setLinearVelocity( new Ammo.btVector3( vel.x, vel.y, vel.z ) ); } if ( angVel ) { body.setAngularVelocity( new Ammo.btVector3( angVel.x, angVel.y, angVel.z ) ); } object.userData.physicsBody = body; object.userData.collided = false; scene.add( object ); if ( mass > 0 ) { rigidBodies.push( object ); // Disable deactivation body.setActivationState( 4 ); } physicsWorld.addRigidBody( body ); return body; } 10002ffd66c48bac0aff2be02fac749