Mirror a Box2D-Body?

Hi!

Is it possible to mirror a body from Box2D like I would mirror a Sprite with scaleX = -1? Does Box2D have similar methods for its bodies? Or do I have to destroy the body and create a new one manually?

Thank you!
well that question you have depends on various things.
assuming you want the physics shape to flip too, at the point in the code where you are building the shape.

assuming you are drawing the points of the shape point by point.

you can use the Transform Matrix in flash to calculate scaleX scaleY even Skew and Rotation, and return the new location post transformation.

as an example
you have a rectangle. that started as a 30px x 30px square before transformations.
and 30 px/Meter, so .5 meters is 15 pixels, and the square is centered so it goes 15px each direction

Code:
_shapeDef.vertexCount = 4;
         
var tl:Point = _shapeMC.transform.matrix.deltaTransformPoint(new Point(-.5,-.5));
var tr:Point = _shapeMC.transform.matrix.deltaTransformPoint(new Point(.5,-.5));
var bl:Point = _shapeMC.transform.matrix.deltaTransformPoint(new Point(-.5,.5));
var br:Point = _shapeMC.transform.matrix.deltaTransformPoint(new Point(.5,.5));
         
_shapeDef.vertices[0].Set(tl.x, tl.y);
_shapeDef.vertices[1].Set(tr.x, tr.y);
_shapeDef.vertices[2].Set(br.x, br.y);
_shapeDef.vertices[3].Set(bl.x, bl.y);


hope this helps.

i believe you can also find Transform Matrix references in WCK by MayoButter
Thanks for your answer. I use a MatrixTransformation to link my graphics with my bodies (like mentioned in this wiki: http://www.box2d.org/wiki/index.php?tit ... _Box2D_AS3). Is that what you meant? (Sorry, for this question, but my english is not that good.)

What I want is to mirror the physical shapes of my bodies in runtime. For example if I press the left arrow key, I want my body to look to the left and if I press the right arrow key I want my body to look to the right. And it would be nice, if the MatrixTransformation would recognize that, too. So the graphics would mirrored simultaneously with the body.

Is that possibley without destroying the body and creating a new one?
Funny, I was actually asking myself this same question yesterday. I was thinking about writing a custom function to handle all of my flipping and mirroring, but it seemed like it was creating too much data to maintain for myself.

If it's literally just a mirror toggle, use two body structures - one facing left and one facing right. Then when the left or right arrows are pressed, make it toggle the filtering bits on the shapes so that only one is active ay a time.

It's the best I've found. Bodies cannot be scaled or mirrored at runtime - the best solution short of destroying and re-creating the body (which is very reliable by the way) is to 'hide' them from the engine.
patadox wrote:
......
It's the best I've found. Bodies cannot be scaled or mirrored at runtime - the best solution short of destroying and re-creating the body (which is very reliable by the way) is to 'hide' them from the engine.


yes destroy and recreate is eazy to implement. but 'hide' them... as a better solution. how can i do this?
Hi!

I finally tried to mirror a b2Body, but my mirrored body isn't solid anymore. Here is my code:

Code:
//creates body
private function shapeComplex(shape:Array):b2PolygonShape
{
   //code that creates bodyDef
   
   var shape:b2PolygonShape;
   if (!reflection)
   {
      shape = Creator.shapeComplex([
         [ -15, 15],   //lower left corner
         [15, -15],   //uper right corner
         [15, 15]   //lower right corner
      ]);
   }
   else
   {
      shape = Creator.shapeComplex([
         [15, 15],   //lower right corner
         [ -15, -15],   //uper left corner
         [ -15, 15]   //lower left corner
      ]);
   }
   
   //code that creates fixtureDef and b2Body
}

//[ [point1.x, point1.y], [point2.x, point2.y], ... [pointN.x, pointN.y] ]
private function shapeComplex(shape:Array):b2PolygonShape
{
   var polygonShape:b2PolygonShape = new b2PolygonShape();
   var vertexCount:int = shape.length;
   var vertices:Array = new Array();
   for (var i:int = 0; i < shape.length; i++)
   {
      vertices[i] = new b2Vec2();
      vertices[i].Set(shape[i][0] * World.PIXEL_IN_METER, shape[i][1] * World.PIXEL_IN_METER);
   }
   polygonShape.SetAsArray(vertices, vertexCount);
   return polygonShape;
}


The functions create a simple triangle. The triangles created with reflection=false are solid, but the mirrored triangle aren't. Why?

Attachment:
mirror.jpg
mirror.jpg [ 61.31 KiB | Viewed 1812 times ]


Greetings,
Pipo
Box2D has requires a winding order for polygons. To mirror a polygon, mirror every vertex, then reverse the order of the vertices. I'm already considering adding a method for this.

Original thread: https://box2d.org/forum/viewtopic.php?t=4508