4

This has to do with Three.js + Socket IO + CANNON.js. I have an CANNON.RigidBody object on my SERVER, that i cannot send as it is. So i transform it like this:

// 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
    } }

and sent it to my CLIENT....

broadcastJsonEvent(JSON.stringify(player))

In my CLIENT i pass this "player" object into my control, Normally instead of player one would pass the CANNON.RigidBody object.

controls = new PointerLockControls(camera, player);
scene.add(controls.getObject());

Because i cannot send the full object which is.... a CANNON.RigidBody i send it as that player object, and in PointerLockControls the last line looks like this... And this is where all fails.

cannonBody.position.copy(yawObject.position);

Now my player object does not have the copy method. What can i do? Please help. This is what i tried. Remember cannonBody = player!! And even if i get the copy method it still does not work.

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);

And even if i get the copy method it still does not work. :(

Thor88
  • 323
  • 3
  • 7
  • what do you extend? `cannonBody.position = $.extend(copyBody);` – Ol Sen May 23 '13 at 20:22
  • I extend a Vector3... x,y,z. So basically on my server i have a CANNON.RigidBody object that i take some properties of and put it into the "player" object and send it to the client. But on the client now i need the origianl CANNON.RigidBody type and the "player" object is of type object and not CANNON.RigidBody. so i try and convert it. – Thor88 May 24 '13 at 08:07

1 Answers1

1

Assuming you only need to do the copy operation, you can do this:

CANNON.Vec3.prototype.copy.call(cannonBody.position,yawObject.position);

This line of code uses the copy method from CANNON.Vec3. You may want to look up Function.prototype.call to understand how this works. Note that you need to load Cannon.js on the client to do this.

Depending on what the rest of your code does, it may be convenient for you to make a local CANNON.RigidBody instance on the client. You can update this body with new states whenever you get updates from the server.

schteppe
  • 1,984
  • 13
  • 15