Hello everyone, I hope any of you can help me with one little problem.
On the game that I'm working, I have a circle colliding inside of a box:
Normally, each time the circle collides with the walls in a angle, the circle starts rotating (as it should be), and this angular velocity will influence the next collision with the wall (as it should be too) modifying the resulting direction of circle movement.
What I've been trying to achieve is a modification on Box2D in a way that the collisions INFLUENCE the angular velocity, but the Angular Velocity itself DOES NOT INFLUENCE the collision (do not modify the resulting movement direction).
To do so, I'm modifying the SolveVelocityConstraints() function on b2ContactSolver.cpp, by setting the angular speed at 0.0f only when calculating the linear speed.
Here is a snippet of the code:
And this is not working. Can any of you spot what I did wrong, or even better, suggest another way to do this?
Thank you.
On the game that I'm working, I have a circle colliding inside of a box:
Code:
_______
| |
| O |
| |
|______|
| |
| O |
| |
|______|
Normally, each time the circle collides with the walls in a angle, the circle starts rotating (as it should be), and this angular velocity will influence the next collision with the wall (as it should be too) modifying the resulting direction of circle movement.
What I've been trying to achieve is a modification on Box2D in a way that the collisions INFLUENCE the angular velocity, but the Angular Velocity itself DOES NOT INFLUENCE the collision (do not modify the resulting movement direction).
To do so, I'm modifying the SolveVelocityConstraints() function on b2ContactSolver.cpp, by setting the angular speed at 0.0f only when calculating the linear speed.
Here is a snippet of the code:
Code:
// Solve tangent constraints
for (int32 j = 0; j < c->pointCount; ++j)
{
b2ContactConstraintPoint* ccp = c->points + j;
// Relative velocity at contact
b2Vec2 dv = vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA);
b2Vec2 dv2 = vB + b2Cross(0.0f, ccp->rB) - vA - b2Cross(0.0f, ccp->rA);
// Compute tangent force
float32 vt = b2Dot(dv, tangent);
float32 vt2 = b2Dot(dv2, tangent);
float32 lambda = ccp->tangentMass * (-vt);
float32 lambda2 = ccp->tangentMass * (-vt2);
// b2Clamp the accumulated force
float32 maxFriction = friction * ccp->normalImpulse;
float32 newImpulse = b2Clamp(ccp->tangentImpulse + lambda, -maxFriction, maxFriction);
float32 newImpulse2 = b2Clamp(ccp->tangentImpulse + lambda2, -maxFriction, maxFriction);
lambda = newImpulse - ccp->tangentImpulse;
lambda2 = newImpulse2 - ccp->tangentImpulse;
// Apply contact impulse
b2Vec2 P = lambda * tangent;
b2Vec2 P2 = lambda2 * tangent;
vA -= invMassA * P2;
wA -= invIA * b2Cross(ccp->rA, P);
vB += invMassB * P2;
wB += invIB * b2Cross(ccp->rB, P);
ccp->tangentImpulse = newImpulse;
}
// Solve normal constraints
if (c->pointCount == 1)
{
b2ContactConstraintPoint* ccp = c->points + 0;
// Relative velocity at contact
b2Vec2 dv = vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA);
b2Vec2 dv2 = vB + b2Cross(0.0f, ccp->rB) - vA - b2Cross(0.0f, ccp->rA);
// Compute normal impulse
float32 vn = b2Dot(dv, normal);
float32 vn2 = b2Dot(dv2, normal);
float32 lambda = -ccp->normalMass * (vn - ccp->velocityBias);
float32 lambda2 = -ccp->normalMass * (vn2 - ccp->velocityBias);
// b2Clamp the accumulated impulse
float32 newImpulse = b2Max(ccp->normalImpulse + lambda, 0.0f);
float32 newImpulse2 = b2Max(ccp->normalImpulse + lambda2, 0.0f);
lambda = newImpulse - ccp->normalImpulse;
lambda2 = newImpulse2 - ccp->normalImpulse;
// Apply contact impulse
b2Vec2 P = lambda * normal;
b2Vec2 P2 = lambda2 * normal;
vA -= invMassA * P2;
wA -= invIA * b2Cross(ccp->rA, P);
vB += invMassB * P2;
wB += invIB * b2Cross(ccp->rB, P);
ccp->normalImpulse = newImpulse;
for (int32 j = 0; j < c->pointCount; ++j)
{
b2ContactConstraintPoint* ccp = c->points + j;
// Relative velocity at contact
b2Vec2 dv = vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA);
b2Vec2 dv2 = vB + b2Cross(0.0f, ccp->rB) - vA - b2Cross(0.0f, ccp->rA);
// Compute tangent force
float32 vt = b2Dot(dv, tangent);
float32 vt2 = b2Dot(dv2, tangent);
float32 lambda = ccp->tangentMass * (-vt);
float32 lambda2 = ccp->tangentMass * (-vt2);
// b2Clamp the accumulated force
float32 maxFriction = friction * ccp->normalImpulse;
float32 newImpulse = b2Clamp(ccp->tangentImpulse + lambda, -maxFriction, maxFriction);
float32 newImpulse2 = b2Clamp(ccp->tangentImpulse + lambda2, -maxFriction, maxFriction);
lambda = newImpulse - ccp->tangentImpulse;
lambda2 = newImpulse2 - ccp->tangentImpulse;
// Apply contact impulse
b2Vec2 P = lambda * tangent;
b2Vec2 P2 = lambda2 * tangent;
vA -= invMassA * P2;
wA -= invIA * b2Cross(ccp->rA, P);
vB += invMassB * P2;
wB += invIB * b2Cross(ccp->rB, P);
ccp->tangentImpulse = newImpulse;
}
// Solve normal constraints
if (c->pointCount == 1)
{
b2ContactConstraintPoint* ccp = c->points + 0;
// Relative velocity at contact
b2Vec2 dv = vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA);
b2Vec2 dv2 = vB + b2Cross(0.0f, ccp->rB) - vA - b2Cross(0.0f, ccp->rA);
// Compute normal impulse
float32 vn = b2Dot(dv, normal);
float32 vn2 = b2Dot(dv2, normal);
float32 lambda = -ccp->normalMass * (vn - ccp->velocityBias);
float32 lambda2 = -ccp->normalMass * (vn2 - ccp->velocityBias);
// b2Clamp the accumulated impulse
float32 newImpulse = b2Max(ccp->normalImpulse + lambda, 0.0f);
float32 newImpulse2 = b2Max(ccp->normalImpulse + lambda2, 0.0f);
lambda = newImpulse - ccp->normalImpulse;
lambda2 = newImpulse2 - ccp->normalImpulse;
// Apply contact impulse
b2Vec2 P = lambda * normal;
b2Vec2 P2 = lambda2 * normal;
vA -= invMassA * P2;
wA -= invIA * b2Cross(ccp->rA, P);
vB += invMassB * P2;
wB += invIB * b2Cross(ccp->rB, P);
ccp->normalImpulse = newImpulse;
And this is not working. Can any of you spot what I did wrong, or even better, suggest another way to do this?
Thank you.