这与 Three.js + Socket IO + CANNON.js 有关。我的服务器上有一个 CANNON.RigidBody 对象,我无法按原样发送。所以我把它改成这样:
// Create a sphere
var mass = 5,
radius = 1.8;
var sphereShape = new CANNON.Sphere(radius);
var physicsMaterial = new CANNON.Material("slipperyMaterial");
var sphereBody = new CANNON.RigidBody(mass, sphereShape, physicsMaterial);
sphereBody.position.set(0, 10, 0);
sphereBody.linearDamping = 0.9;
cannonWorld.add(sphereBody);
cannonEntities[(numBodies++).toString()] = sphereBody;
player = {
type: "player",
data: {
id:((numBodies++).toString()),
position: sphereBody.position,
quaternion: sphereBody.quaternion,
velocity: sphereBody.velocity,
radius:radius,
mass:mass
} }
并将其发送给我的客户......
broadcastJsonEvent(JSON.stringify(player))
在我的客户端中,我将这个“玩家”对象传递给我的控件,通常不是玩家一会传递 CANNON.RigidBody 对象。
controls = new PointerLockControls(camera, player);
scene.add(controls.getObject());
因为我无法发送完整的对象,它是....一个 CANNON.RigidBody,我将它作为播放器对象发送,而在 PointerLockControls 中,最后一行看起来像这样......这就是所有失败的地方。
cannonBody.position.copy(yawObject.position);
现在我的播放器对象没有复制方法。我能做些什么?请帮忙。这是我尝试过的。记住 cannonBody = 玩家!!即使我得到了复制方法,它仍然不起作用。
var copyBody = new CANNON.Vec3(cannonBody.position.x,cannonBody.position.y,cannonBody.position.z);
cannonBody = $.extend(cannonBody,new CANNON.RigidBody(cannonBody.mass,new CANNON.Sphere(1.8),new CANNON.Material("slipperyMaterial")));
cannonBody.position = $.extend(copyBody);
cannonBody.position.copy(yawObject.position);
即使我得到了复制方法,它仍然不起作用。:(