Oimo.js icon indicating copy to clipboard operation
Oimo.js copied to clipboard

Changing the density of a shape doesn't work

Open andresz1 opened this issue 10 years ago • 3 comments

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 avatar Feb 07 '16 19:02 andresz1

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?

andresz1 avatar Feb 07 '16 20:02 andresz1

@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:

ghost avatar Feb 08 '16 12:02 ghost

@xprogram Oh ok, thank you very much!

andresz1 avatar Feb 08 '16 15:02 andresz1