Buron
Buron

Reputation: 1233

How to restore b2Body state after collision?

I have 2 bodies. After they collide second body disappears, and the first one must go on it's move in the in the same way as before the collision.

How it looks now:

1) I detect collision in contactListener::BeginContact(..) { };

2) Save the second body to delete , and the first to restore it's velocity and angle.

3)

-(void) update: (ccTime) dt {
int32 velocityIterations = 8;
int32 positionIterations = 1;
_world->Step(dt, velocityIterations, positionIterations);
...
world->DestroyBody(secondBody);
firstBody->SetLinearVelocity(linearVelocityBeforeTouching);
firstBody->SetTransform(firstBody->GetPosition(), angleBeforeTouching );
...
}

As a result the first body moves in the same direction, but it to rotate as after collision

Upvotes: 0

Views: 113

Answers (1)

iforce2d
iforce2d

Reputation: 8262

When the two bodies collide you could do contact->SetEnabled(false) in the PreSolve of the collision listener to cancel the default collision response.

Upvotes: 1

Related Questions