Sign in to follow this  
miyaxu

What's the difference between p2.enable/new p2.body ?

Recommended Posts

Hi Guys, I've come across a problem with p2 body.

I have 2 sprite, sprite1 is physics enabled, and sprite2 is create a Phaser.Physics.P2.Body

They will be moving using velocity after 2 seconds,

but sprite2 not working...

I made a JsFiddle to explain my question.
https://jsfiddle.net/pbq93enn/

 

Does anyone have an idea what I'm doing wrong?

Thanks in advance!

 

 

Share this post


Link to post
Share on other sites
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { 
create: create
});

var sprite1;
var sprite2;

function create() {
	game.physics.startSystem(Phaser.Physics.P2JS);
	
	// sprite1
	sprite1 = game.add.sprite(50, 50, createCircle(game, 's1', 10, 'red'));
	game.physics.p2.enable(sprite1, true);
	sprite1.body.static = true;
	
	// sprite2
	sprite2 = game.add.sprite(50, 100, createCircle(game, 's1', 10, 'red'));
	sprite2.body = new Phaser.Physics.P2.Body(game, sprite2, 50, 100, 1);
	sprite2.body.debug = true;
	sprite2.body.static = true;
	
	// run
	setTimeout(function() {
		sprite1.body.velocity.x = 200;
		sprite2.body.velocity.x = 200;
	}, 2000);
}

function createCircle(game, key, r, color) {
	const bmd = new Phaser.BitmapData(game, key, r * 2, r * 2);
	bmd.ctx.translate(r, r);
	bmd.ctx.beginPath();
	bmd.ctx.fillStyle = color;
	bmd.ctx.arc(0, 0, r, 0, Math.PI * 2);
	bmd.ctx.fill();
	game.cache.addBitmapData(key, bmd);
	return game.cache.getBitmapData(key);
}
enable(object, debug, children)

This will create a P2 Physics body on the given game object or array of game objects. 

A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed. 

Note: When the game object is enabled for P2 physics it has its anchor x/y set to 0.5 so it becomes centered.
new Body(game, sprite, x, y, mass)

The Physics Body is typically linked to a single Sprite and defines properties that determine how the physics body is simulated. 

These properties affect how the body reacts to forces, what forces it generates on itself (to simulate friction), and how it reacts to collisions in the scene. 

In most cases, the properties are used to simulate physical effects. Each body also has its own property values that determine exactly how it reacts to forces and collisions in the scene. 

By default a single Rectangle shape is added to the Body that matches the dimensions of the parent Sprite. 

The difference is that .enable will create the body on the game sprite, while new body will link it to the sprite. Doing a new body(), should in theory give you more control over the physics body and the sprite, while enable will give you a sprite with physics.

I hope that makes sense to you.

P.S. I am new to Phaser so it might be that I am totally wrong on their approach and it might just be a bug.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.