(P2) Attach 2 Physics Bodies Together

Recommended Posts

So I have been searching a solution for this for 2 days now.

I have a character with physics body.

This character has sprites attached to it with addChild(). Arms, sword, cloak, range indicator.

Range indicator also has a body (only used as sensor/trigger ). However as I found out addChild doesn't work on physics bodies. Sprites move together but I can get Range Indicator's body to move with them. It pretty much sits in the corner.

Since addChild() is no go for physics bodies, is there anything else I can use with this? Really trying to avoid writing a custom function to make the body follow me but running out of options here.


EDIT: Found a workaround for weird child behaviour with bodies.

Added another invisible sprite and made it child to my body.

this.rangePositioner = game.add.sprite(0, 0, 'rangeBlack');
this.rangePositioner.pivot.set(0, 40);

Take its world position with ( rangePositioner.position returns 0 ) then set it to my child body.position. I had to dig for some time before I realized returns a point. I would expect it to be named something like worldPoint. Anyways I hope this helps someone.

Share this post

Link to post
Share on other sites

Here is a more detailed explanation.

You have guy,

let guy = game.add.sprite(0,0,'guy')

And you want to give him a sword

let sword = game.add.sprite(0,0,'sword')

You would normally do,


However, if you want sword to have a body, addChild doesn't work. Workaround is;

You create a 'ghost' sprite without a body.

let ghostSprite = game.add.sprite(0,0,'ghostSprite');

Make it invisible so it doesn't block our real sword.

ghostSprite.visible = false;

Add it as a child to our guy


Now, our ghostSprite is perfectly following&rotating with our guy. All we need to do is 'attach' sword to the ghostSprite.

sword.body.x =;
sword.body.y =;
sword.body.rotation = ghostSprite.worldRotation;

We assign rotation & position to the body because it controls sprite pos&rot. If you assign it to the sprite, you will see sprite trying to move but constantly 'pulling back' to the body. So, always assign to the body, not to the sprite. You might also want to set

body.debug = true;

to debug any weird behaviours in the future.

Thats about it. Hit me up if you cant get it to work.

Share this post

Link to post
Share on other sites

I notice one problem is you are always a step behind in the physics simulation which is a problem at high speeds. For example if you move fast enough your sword ends up behind the player. I wonder what a proper solution to this would be.

This happens if you attach the parts in the beginning or end of the update loop.

Share this post

Link to post
Share on other sites

First of all, thanks so much for the info provided in this thread. I was really dismayed to think that there was no way to move a p2 sprite's position to an arbitrary x/y, but it turns out it's super easy. Just change the body.x and body.y in the update function. 

The reason I bother to post on a dead thread is that I want to point out that the "ghost sprite" doesn't seem to be necessary. 

Also, if for example sprite1 collides with sprite2 and becomes attached to it, then it's probably desired to turn off the collision between them. For this there are two options:

1. destroy sprite1 upon collision and make a replacement which is attached to sprite2 and that does not collide with sprite2

2. use the 4th argument of the `collides` function as described here: 


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.

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.


  • Recently Browsing   0 members

    No registered users viewing this page.