Again, could you post some code, showing you you create these 'concave' (I assume composite) bodies?
Collision works for dynmaic bodies but not for floor bodies
FlashCode:
private function addConcavePolygon(tShape:Sprite, tTriangle:Array):void
{
var tLong:int = tTriangle.length;
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
var tPolyArray:Array = new Array();
for (var i:int=0; i< tLong;i++)
{
var tPoly:b2PolygonDef = new b2PolygonDef();
tPoly.friction = 0.3;
tPoly.density = 1;
tPoly.restitution = 0.2;
tPoly.vertexCount = 3;
if(pDirection){
tPoly.vertices[0].Set(tTriangle[i].p0.x/pPhysScale, tTriangle[i].p0.y/pPhysScale);
tPoly.vertices[1].Set(tTriangle[i].p1.x/pPhysScale, tTriangle[i].p1.y/pPhysScale);
tPoly.vertices[2].Set(tTriangle[i].p2.x/pPhysScale, tTriangle[i].p2.y/pPhysScale);
}else{
tPoly.vertices[2].Set(tTriangle[i].p0.x/pPhysScale, tTriangle[i].p0.y/pPhysScale);
tPoly.vertices[1].Set(tTriangle[i].p1.x/pPhysScale, tTriangle[i].p1.y/pPhysScale);
tPoly.vertices[0].Set(tTriangle[i].p2.x/pPhysScale, tTriangle[i].p2.y/pPhysScale);
}
tPolyArray.push(tPoly);
}
addDynamicShapes({shapeName:"concavePolygon", shape:tShape, bodyDef:tMyBodyDef, type:tPolyArray});
}
{
var tLong:int = tTriangle.length;
var tMyBodyDef:b2BodyDef = new b2BodyDef();
tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
var tPolyArray:Array = new Array();
for (var i:int=0; i< tLong;i++)
{
var tPoly:b2PolygonDef = new b2PolygonDef();
tPoly.friction = 0.3;
tPoly.density = 1;
tPoly.restitution = 0.2;
tPoly.vertexCount = 3;
if(pDirection){
tPoly.vertices[0].Set(tTriangle[i].p0.x/pPhysScale, tTriangle[i].p0.y/pPhysScale);
tPoly.vertices[1].Set(tTriangle[i].p1.x/pPhysScale, tTriangle[i].p1.y/pPhysScale);
tPoly.vertices[2].Set(tTriangle[i].p2.x/pPhysScale, tTriangle[i].p2.y/pPhysScale);
}else{
tPoly.vertices[2].Set(tTriangle[i].p0.x/pPhysScale, tTriangle[i].p0.y/pPhysScale);
tPoly.vertices[1].Set(tTriangle[i].p1.x/pPhysScale, tTriangle[i].p1.y/pPhysScale);
tPoly.vertices[0].Set(tTriangle[i].p2.x/pPhysScale, tTriangle[i].p2.y/pPhysScale);
}
tPolyArray.push(tPoly);
}
addDynamicShapes({shapeName:"concavePolygon", shape:tShape, bodyDef:tMyBodyDef, type:tPolyArray});
}
Code:
private function addDynamicShapes(tData:Object):void
{
var tDynamicBody:b2Body;
tDynamicBody = pWorld.CreateBody(tData.bodyDef);
//tData.type is an array filled with PolygonDef Triangles
for each (var pd:b2PolygonDef in tData.type){
tDynamicBody.CreateShape(pd);
}
tDynamicBody.SetMassFromShapes();
pDynamicActor = new DynamicActor(tDynamicBody, tData.shape);
tDynamicBody.m_userData = pDynamicActor;
pDynamicActors.push(pDynamicActor);
drawArea.addChild(tData.shape);
}
{
var tDynamicBody:b2Body;
tDynamicBody = pWorld.CreateBody(tData.bodyDef);
//tData.type is an array filled with PolygonDef Triangles
for each (var pd:b2PolygonDef in tData.type){
tDynamicBody.CreateShape(pd);
}
tDynamicBody.SetMassFromShapes();
pDynamicActor = new DynamicActor(tDynamicBody, tData.shape);
tDynamicBody.m_userData = pDynamicActor;
pDynamicActors.push(pDynamicActor);
drawArea.addChild(tData.shape);
}
Looks fine to me again..
And you are using a good convex decomposition algorithm?
And you are using a good convex decomposition algorithm?
the objects simulate well... they fall, collide... but dont react to objects which are painted on fly. And I have no idea why?? could it be possible that there is a problem with bodies which has more than one shape?
Do you have a demo? Or a testbed example maybe?
Oha... after some more tests, I think the collision detection works, but I make the wrong collision recognisation.
I get the Collsion Shapes of the ContactListener. That means, I get always two shapes (pShape1 and pShape2) .
now I compare them with all DynamicActors
in the code before I compare the collisiondetectionshapes always with a concave object, but the result is always false.
When I trace this, it seems to be, that the program recognise the collision, but the compare is false anyway.
I get the Collsion Shapes of the ContactListener. That means, I get always two shapes (pShape1 and pShape2) .
Code:
override public function Add(tPoint:b2ContactPoint):void
{
pShape1 = tPoint.shape1;
pShape2 = tPoint.shape2;
}
{
pShape1 = tPoint.shape1;
pShape2 = tPoint.shape2;
}
now I compare them with all DynamicActors
Code:
pDynamicActors[1].body.GetShapeList() == pContactListener.shape2
pDynamicActors[1].body.GetShapeList() == pContactListener.shape1
pDynamicActors[1].body.GetShapeList() == pContactListener.shape1
in the code before I compare the collisiondetectionshapes always with a concave object, but the result is always false.
Code:
trace(pDynamicActors[1].body.GetShapeList());
trace(pContactListener.shape1);
trace(pContactListener.shape2),
trace(pContactListener.shape1);
trace(pContactListener.shape2),
When I trace this, it seems to be, that the program recognise the collision, but the compare is false anyway.
The solution is to compare the bodies and not the shapes.
Code:
pDynamicActors[1].body == pContactListener.shape2.GetBody()
pDynamicActors[1].body == pContactListener.shape1.GetBody()
pDynamicActors[1].body == pContactListener.shape1.GetBody()
Well, yes. If your body has more than one shape, "GetShapeList" will just return the first one, you would then have to cycle through that Linked List, and check each shape. Good job fixing it 🙂.
Original thread: https://box2d.org/forum/viewtopic.php?t=3052