But at the first Cut, when everything its allright, all vertices are clear yet, i have the same bug!
I try to post here, every single diference i have to do, to make the code work:
1 - Remake how you get the entry and exit points of collision, to the new raycast method, this is working, because i can draw this, and see this is the perfect point of colision, and Box2D don't have anymore the b2Segment, so i must use the Touch points:
Quote:
Change from This:
Code:
const b2Body* b = shape->GetBody();
const b2XForm xf = b->GetXForm();
if (shape->TestSegment(xf, &lambda, &normal, segment, 1.0f) != e_hitCollide)
return -1;
b2Vec2 entryPoint = (1-lambda)*segment.p1+lambda*segment.p2;
b2Segment reverseSegment;
reverseSegment.p1 = segment.p2;
reverseSegment.p2 = segment.p1;
if (shape->TestSegment(xf, &lambda, &normal, reverseSegment, 1.0f) != e_hitCollide)
return -1;
b2Vec2 exitPoint = (1-lambda)*reverseSegment.p1+lambda*reverseSegment.p2;
b2Vec2 localEntryPoint = b->GetLocalPoint(entryPoint);
b2Vec2 localExitPoint = b->GetLocalPoint(exitPoint);
To this:Code:
CGPoint firstLocation = CGPointFromString([arrTouches objectAtIndex:0]);
CGPoint secondLocation = CGPointFromString([arrTouches objectAtIndex:1]);
firstLocation.x = firstLocation.x/PTM_RATIO;
firstLocation.y = firstLocation.y/PTM_RATIO;
secondLocation.x = secondLocation.x/PTM_RATIO;
secondLocation.y = secondLocation.y/PTM_RATIO;
b2Vec2 firstPoint(firstLocation.x, firstLocation.y);
b2Vec2 secondPoint(secondLocation.x, secondLocation.y);
RayCastMultipleCallback callbackIn;
world->RayCast(&callbackIn ,firstPoint, secondPoint);
RayCastMultipleCallback callbackOut;
world->RayCast(&callbackOut ,secondPoint, firstPoint);
for(int i = 0; i < callbackIn.m_count; i++)
{
b2Body *tryBody = callbackIn.m_body[i];
if(tryBody->GetFixtureList()->GetShape()->GetType() != 1)
{
NSLog(@"This Shape is not a Poly Shape, we can't continue");
continue;
}
b2Fixture *tempFixture = tryBody->GetFixtureList();
b2PolygonShape* bodyShape = (b2PolygonShape *)tryBody->GetFixtureList()->GetShape();
b2PolygonShape pd[2];
b2Vec2 inPointOfCollision = callbackIn.m_points[i];
b2Vec2 outPointOfCollision = callbackOut.m_points[((callbackOut.m_count-1)-i)];
b2Vec2 localEntryPoint = tryBody->GetLocalPoint(inPointOfCollision);
b2Vec2 localExitPoint = tryBody->GetLocalPoint(outPointOfCollision);
2 - In the Function checkPolyShape i can't figure out what is the best way to doing that, but like someone said in this topic, you cant alloc the array like that:
Quote:
Can't do that:
Code:
b2Vec2 m_normals[poly->vertexCount];
So, just to make this work i change to that:
Code:
b2Vec2 m_normals[b2_maxPolygonVertices];
But i don't believe this is the problem, because, even if don't do this verification in checkPolyShape, like @kne do in the python code, the bug still exist
3 - And another modification i have to do is the part where you recreate your body:
Quote:
In original its like that:
Code:
if (SplitShape(polyShape, segment, 0.1, pd) == 0)
{
b->DestroyShape(shapes[i]);
b->CreateShape(&pd[0]);
b->SetMassFromShapes();
b->WakeUp();
b2BodyDef bd;
bd.userData = (void*)1;
bd.position = b->GetPosition();
bd.angle = b->GetAngle();
b2Body* newBody = m_world->CreateBody(&bd);
newBody->CreateShape(&pd[1]);
newBody->SetMassFromShapes();
newBody->SetAngularVelocity(b->GetAngularVelocity());
newBody->SetLinearVelocity(b->GetLinearVelocity());
}
I'm have to change to this:
Code:
if([self SplitBody:tryBody from:inPointOfCollision to:outPointOfCollision andReturnInThisObject:pd] == 0)
{
tryBody->DestroyFixture(tempFixture);
tryBody->CreateFixture(&pd[0], 1.0f);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
//bodyDef.userData = (void *)1;
bodyDef.position = tryBody->GetPosition();
bodyDef.angle = tryBody->GetAngle();
b2Body *newBody = world->CreateBody(&bodyDef);
b2FixtureDef fixtureDef;
fixtureDef.shape = &pd[1];
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
newBody->CreateFixture(&fixtureDef);
newBody->SetAngularVelocity(tryBody->GetAngularVelocity());
newBody->SetLinearVelocity(tryBody->GetLinearVelocity());
}
Looks like it's something when you recreate the bodie, some filter, something i have to do in diferent way in this new box2D version, like the SetMassFromShapes(), i try to rewrote this to another things, but i keep geting this bug! The objects acting like that in this movie:
http://www.youtube.com/watch?v=jkN4Yla_9Ek