jerry_afr

How to make p2js deterministic

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

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites
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.

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.