Jump to content

How to make p2js deterministic


jerry_afr
 Share

Recommended Posts

Hi,

I'm using p2js for physics for my game.

The issue is p2js is having different behaviors in the same situations.

I'm adding 4 units with the following shape to the scene to the same x and y in a loop each time I click on the page.

  "shape": [ 10,0, -10,0, 0,-20 ] 

As you can see in the screenshot, the first and second group of 4 units have different alignments while they are being added with the same function with no difference.

It seems that p2js is using a random function in some cases to determine how to move the objects.

Is there any way to make p2js deterministic? Since I'm making a multiplayer online game, it is a critical factor for me.

 

Thanks

screenshot.jpg

Link to comment
Share on other sites

for (var i=0;i<4;i++) {
	var sprite = game.add.sprite(80+4*90, y, 'units');
	sprite.anchor.setTo(0.5,0.5);
	sprite.animations.add(type, Phaser.Animation.generateFrameNames(type+'-'+d+'-', 1, 8, '.png'), 8, true);
	sprite.animations.play(type);
	sprite.scale.setTo(0.5);
	
	game.physics.p2.enable([ sprite ], false);
	sprite.body.clearShapes();
	var physicsName='unit_down';
	sprite.body.loadPolygon('physicsData', physicsName);
	sprite.body.fixedRotation=true;
	sprite.body.mass = unitInfo.mass;
	sprite.body.setZeroVelocity();
}

Here is how I create troops in my game.

I'm calling this method every time I click on a button and sometimes, the troops align like the left group, sometimes they align like the right group (in the screenshot).

Link to comment
Share on other sites

  • 2 months later...
2 minutes ago, schteppe said:

A wild guess, but maybe it's because they overlap when you create them? Add some more space between them or make each sprite smaller?

They overlap when I create them and the system automatically moves them. But these moves look random. Sometimes it moves them to left, sometimes it moves them to right. If it is deterministic, it should always have the same behavior.

 

Link to comment
Share on other sites

Yes, it will produce the same result if you add the exact same number of objects at the exact same positions AND at the exact same physics time AND in the same order. You also need to use a fixed time step (i hope Phaser does this). My guess is that the constraint solver gets a different error value each time and solves the contacts differently. Or maybe the broadphase reports the collisions in a different order (due to the different positions), which results in different order of the contacts, which makes the solver do something else.

In any case, you should never start a physics simulation with overlaps. If you change the physics settings later, you will have no control of what will happen.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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