Jump to content

Problems with physics p2


Milani
 Share

Recommended Posts

Good Morning. I repeat one method every second, using "game.time.events.repeat". In this method I break down a molecule and re-join the atoms in other molecules, here's my problem. When I remove a constraint, the molecule undoes, but when creating the new molecule, "createDistanceConstraint" and / or "createLockConstraint" fail, as if it lost the context. I would like your help.

//        arrControle.push({
//            _n_a: obj1._n_a,
//            _h1: obj2._h_a,
//            _h2: obj3._h_a,
//            _h3: obj4._h_a,
//            _c1: constr_1,
//            _c2: constr_2,
//            _c3: constr_3,
//            tipo: 'A'
//        });

    var obj1 = getMolecula_inArrayControle('A');
    var obj2 = getMolecula_inArrayControle('A');

    this.game.physics.arcade.moveToXY(obj1._n_a, obj2._n_a.x, obj2._n_a.y, 0, 900);

    this.game.physics.p2.removeConstraint(obj1._c1);
    this.game.physics.p2.removeConstraint(obj1._c2);
    this.game.physics.p2.removeConstraint(obj1._c3);

    this.game.physics.p2.removeConstraint(obj2._c1);
    this.game.physics.p2.removeConstraint(obj2._c2);
    this.game.physics.p2.removeConstraint(obj2._c3);

    this.game.physics.p2.createDistanceConstraint(obj1._n_a, obj2._n_a, 5); <-- Here it fails
    this.game.physics.p2.createDistanceConstraint(obj1._h1, obj2._h1, 5); <-- Here it fails
    this.game.physics.p2.createDistanceConstraint(obj1._h2, obj2._h2, 5); <-- Here it fails
    this.game.physics.p2.createDistanceConstraint(obj1._h3, obj2._h3, 5); <-- Here it fails

 

Link to comment
Share on other sites

Did you assign the constraint to obj1._c1 and obj2._c1 etc...  to maintain the reference?:

//somewhere in  create
obj1._c1=game.physics.p2.createDistanceConstraint(obj1._n_a, obj2._h_a, 5);

//in your event loop
game.physics.p2.removeConstraint(obj1._c1);
//reassign it
obj1._c1=game.physics.p2.createDistanceConstraint(obj1._n_a, obj2._h_a, 5);

If so, in what way is it failing? Is there any error?

Link to comment
Share on other sites

Thanks for answering samid737. Creating the constraint simply fails. Most likely by the loss of reference, as you have shown. However, I have tried other ways, used groups and did not succeed. Would there be a way to access objects directly in the world without losing their reference? I do not have much experience, but if you can give me a suggestion, I'll be grateful.

Link to comment
Share on other sites

Maybe you can use addConstraint instead of creating a new one:

https://phaser.io/docs/2.2.2/Phaser.Physics.P2.html#addConstraint

Then if you have any constraints set as variables, you can add to the world and remove them  from the world whenever you like. Also if you want to further inspect what is going on, this might give you more details:

https://phaser.io/docs/2.2.2/Phaser.Physics.P2.html#getConstraints

console.log(game.physics.p2.getConstraints());

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...