lo-th / Oimo.js

Lightweight 3d physics engine for javascript
http://lo-th.github.io/Oimo.js
MIT License
3.07k stars 303 forks source link

Changing the density of a shape doesn't work #36

Open andresz1 opened 8 years ago

andresz1 commented 8 years ago

Hi I'm trying to do a body wrapper for my WebGL engine but changing the density of the shape doesn't work but changing the restitution does. Can you guys help me please. Here is the class code.

EZ3.Body = function(world, type, position, size, rotation, move) {
  this._body = world.add({
    type: type,
    pos: position,
    size: size,
    rot: rotation,
    move: move,
    config: [
      40,
      1.9,
      0.2
    ]
  });

 this._body.shapes.density = 150;

  console.log(this._body.shapes.density); //150 but in the simulation still 40
};

Object.defineProperty(EZ3.Body.prototype, 'density', {
  get: function() {
    return this._body.shapes.density;
  },
  set: function(density) {
    this._body.shapes.density = density;
  }
});

Object.defineProperty(EZ3.Body.prototype, 'restitution', {
  get: function() {
    return this._body.shapes.restitution;
  },
  set: function(restitution) {
    this._body.shapes.restitution = restitution;
  }
});

Besides that, do you guys think that is necessary to support more than one shape per body ?. I saw Babylon.js code and I think it doesnt.

Greetings and thanks

andresz1 commented 8 years ago
Object.defineProperty(EZ3.Body.prototype, 'density', {
  get: function() {
    return this._body.shapes.density;
  },
  set: function(density) {
    this._body.shapes.density = density;
    this._body.setupMass(0x1, this.move);
  }
});

Fixed, if I change the density, I should update the mass. By the way, do you think I should consider more than one shape per body?

ghost commented 8 years ago

@andresz1 for more than 1 shape per body, you should take advantage because that's defining a compound object, however if you only needed 1 shape per body that is fine. If you took a look at GoblinPhysics or cannon.js you would see that it's about the same concept.

Here are examples for compound shapes:

andresz1 commented 8 years ago

@xprogram Oh ok, thank you very much!