Jump to content

P2 Physics - can't control offset of shapes?


Gan_HOPE326
 Share

Recommended Posts

Hi all,

 

I've been playing around with Phaser's P2 physics for a game set in space... I have a very simple starship to which I give a triangular shape with addPolygon and then planets with circle shapes (setCircle). This is an example of the code:

//For the playerplayer.body.addPolygon({'skipSimpleCheck': true}, [[this.width/2.0, 0], [0, this.height], [this.width, this.height]]);  // A simple triangular shape//For the planetplanet.body.setCircle(radius);    

After doing a clearShapes() for both. However, the collision seems to be shifted on the player - when it hits on the top, it hits while it's still a few pixels from the surface, while on the bottom, it overlaps a bit before hitting. I suppose this is the consequence of P2 calculating the center of mass of the shape and then overlapping it to the middle of the sprite. Is there any way to avoid/fix this? I tried manually changing player.body.offset but it didn't work. Thanks!

 

EDIT: Ok, I don't know how to tag this as solved but I did find a solution for this. From what I see, yes, P2 finds the center of mass and then automatically overlaps it to the .5, .5 point of the sprite - my solution was to simply increase the sprite's size, giving it some more empty space to shift the center downwards and have it coincide with the place where I wanted the center of mass to actually be. It's a bit rough but I could afford it given that the sprite was very small anyway. Would be nice to be able to avoid this though.

Link to comment
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...
 Share

  • Recently Browsing   0 members

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