Phaser P2 DebugBody - Visualizing Rotation (possible bug)

Recommended Posts

When adding a rectangle to a p2 body you have the option to specify a rotation in radians for the shape which it appears is just passed directly on to the p2 shape constructor (hence the need for it to be in radians instead of degrees as are used elsewhere in Phaser).

I find that if I use any value other than 0 for a rotation, the shape is properly rotated however drawing of the body via the debug parameter ignores the rotation.

Here is a quick codepen illustrating the bug.  The character should have a long protrusion coming out the bottom achieved with a rotated rectangle.  The physics collision seems to indicate that the rotation has occurred correctly, however the debug shape is not drawn rotated.  Notice that he appears to sit a little ways off the ground due to the un-rotated debug visualization of the shape.

It seems to me this must be a bug (or maybe a limitation of the debugbody drawing system) but I thought I would post here first for thoughts.  I will likely dig in and see if I can find a fix / workaround for this.

Phaser P2 DebugBody rotation bug (codepen)

Share this post

Link to post
Share on other sites

#258 still A bug, the workaround, more specifically, try if this works for drawRectangle :

    drawRectangle: function(g, x, y, angle, w, h, color, fillColor, lineWidth) {

        if (lineWidth === undefined) { lineWidth = 1; }
        if (color === undefined) { color = 0x000000; }
        c = Math.cos(angle);
        s = Math.sin(-angle);

        var oxc = w / 2 * c;
        var oxs = w / 2 * s;

        var oyc = h / 2 * c;
        var oys = h / 2 * s;

        g.lineStyle(lineWidth, color, 1);
        g.moveTo(x + (oxc - oys), y + (-oxs - oyc));
        g.lineTo(x - (- oxc - oys), y - (oxs - oyc));
        g.lineTo(x + (- oxc + oys), y + (oxs + oyc));
        g.lineTo(x - (oxc + oys), y - (- oxs + oyc));


Share this post

Link to post
Share on other sites

I just integrated that into the codepen to test and it seems to work for my case.

Now that it is drawn correctly, I also noticed that the placement of the rotated collision box made the 'dude' sprite rather ... um ... well endowed! :o

Thanks again,

Seth B.

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.