Jump to content

P2.js - Resizing body's shapes


ambidex
 Share

Recommended Posts

I've been working with P2.js within Panda.js for a while now and need it for some complex collisions (like concave shapes). While I can make everything work like clockwork, there is one this I'm not able to figure out: how do I resize my body's shapes? I would think body.shapes[0].height would do the trick, but after the body has been added to the World, this will not be used. I'm having the feeling that this should actually be possible. 

Link to comment
Share on other sites

You're right. The p2.Rectangle class constructor instantiates a p2.Convex with 4 vertices, see https://github.com/schteppe/p2.js/blob/master/src/shapes/Rectangle.js

You need to update the vertices instead. Something like:

shape.vertices[0][0] = -width/2;

shape.vertices[0][1] = -height/2;

...etc

Then you need to update some properties of the Convex:

shape.updateTriangles();

shape.updateCenterOfMass();

shape.updateBoundingRadius();

shape.updateArea();

Sorry for the clunky API :P

Link to comment
Share on other sites

Hi Schteppe,

 

Thanks a lot for your reply. Just implemented this into my game, seems to work! Though the Panda.js p2.js debugdraw seems to not be prepared for these shape updates. Might have to look into that one day. 

 

Though, while implementing this I was realizing that this is a really heavy weight solution for my situation. I've got a playground that is to be scaled, in this playground are sprites that have these corresponding p2.js bodies so they have to be resized too. Since the GUI shouldn't be scaled, and I use bodies for those sprites too, I scale the bodies in the playground manually. I'm now thinking of creating separate Worlds for the playground and GUI, so I can simply change the playground's World ratio. Would you agree on that being the right approach? 

 

Edit: I'm starting to have the feeling World.ratio does not exist in p2's World object? I'm not sure, but I also think there is no support for complete World resizing? 

Link to comment
Share on other sites

Hi again,

 

There's no support for "ratio" it in the p2.js World, I don't know about Panda.js. Rigid bodies aren't supposed to change at all over time, it's often better to recreate them. In many cases there's a physics-to-rendering scale ratio that you can set somewhere, probably in your game framework.

 

Stefan

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...