Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool AABBsOverlap(Entity* collider, Entity* other, float dt) {
- if ((collider->trueAABB[0].x >= other->trueAABB[0].x || collider->trueAABB[1].x >= other->trueAABB[0].x) && (collider->trueAABB[0].x <= other->trueAABB[1].x || collider->trueAABB[1].x <= other->trueAABB[1].x) && (collider->trueAABB[0].y >= other->trueAABB[0].y || collider->trueAABB[1].y >= other->trueAABB[0].y) && (collider->trueAABB[0].y <= other->trueAABB[1].y || collider->trueAABB[1].y <= other->trueAABB[1].y)) {
- if (/*!IsLayer(collider->layer, other->layer) ||*/ collider->bIsTrigger || other->bIsTrigger) return false;
- #if !PHYSICS_DISABLED
- const float colMult = ((float)other->bIsStatic + 1.0f) / 2.0f * (float)!collider->bIsStatic, otherMult = ((float)collider->bIsStatic + 1.0f) / 2.0f * (float)!other->bIsStatic;
- const Vector2 fromTo = FromToVector(collider->midPoint, other->midPoint);
- #define checkDot(dir) DotProduct(fromTo, dir)
- float right = checkDot(rightVector), up = checkDot(upVector);
- float maxDot = -10.0f;
- ushort dir;
- #define checkMax(_dir, direction) if (_dir > maxDot) {\
- dir = direction;\
- maxDot = _dir;\
- }
- checkMax(right, RIGHT);
- checkMax(up, UP);
- checkMax(-right, LEFT);
- checkMax(-up, DOWN);
- bool isDirX = dir == RIGHT;
- bool isDirY = dir == UP;
- Vector2 relativeVel = TakeVectors(other->velocity, collider->velocity);
- bool changedY = collider->transform.pastPosition.y != collider->transform.position.y || other->transform.pastPosition.y != other->transform.position.y;
- Vector2 normal = { (isDirX ? FMin : FMax)(collider->trueAABB[isDirX].x, collider->trueAABB[isDirX].x) - (isDirX ? FMax : FMin)(collider->trueAABB[!isDirX].x, other->trueAABB[!isDirX].x), ((isDirY ? FMax : FMin)(collider->trueAABB[!isDirY].y, other->trueAABB[!isDirY].y) - (isDirY ? FMin : FMax)(collider->trueAABB[isDirY].y, collider->trueAABB[isDirY].y)) * changedY };//positive overlap
- Vector2 normalizedNormal = NormalizedVector(normal);
- Vector2 minRelVel = relativeVel;
- minRelVel = VectorMax(AbsVector(relativeVel), fallBackVelocity);
- minRelVel.x *= (relativeVel.x > .0f) * 2.0f - 1.0f;
- minRelVel.y *= (relativeVel.y > .0f) * 2.0f - 1.0f;
- float dot = DotProduct(normalizedNormal, minRelVel);
- if (dot > .0f) return true;
- float e = FMin(collider->restitutionCoef, other->restitutionCoef);
- Vector2 jn = VectorByScalar(normalizedNormal, (-1.0f -e) * dot / (collider->invMass + other->invMass) );
- jn.y *= changedY;
- //printf("jn: %f, %f, normalized normal: %f, %f, relative velocity: %f, %f, dot: %f, collider velocity.x: %f, other velocity.x: %f, ", jn.x, jn.y, normalizedNormal.x, normalizedNormal.y, relativeVel.x, relativeVel.y, dot, collider->velocity.x, other->velocity.x);
- TakeVectorsAssign(&collider->velocity, VectorByScalar(jn, collider->invMass));
- AddVectorsAssign(&other->velocity, VectorByScalar(jn, other->invMass));
- //printf("new other velocity.x: %f, new collider velocity.x: %f. ", other->velocity.x, collider->velocity.x);
- #endif
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment