Guest User

Untitled

a guest
Nov 19th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1.  
  2. bool AABBsOverlap(Entity* collider, Entity* other, float dt) {
  3. 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)) {
  4. if (/*!IsLayer(collider->layer, other->layer) ||*/ collider->bIsTrigger || other->bIsTrigger) return false;
  5. #if !PHYSICS_DISABLED
  6. const float colMult = ((float)other->bIsStatic + 1.0f) / 2.0f * (float)!collider->bIsStatic, otherMult = ((float)collider->bIsStatic + 1.0f) / 2.0f * (float)!other->bIsStatic;
  7. const Vector2 fromTo = FromToVector(collider->midPoint, other->midPoint);
  8. #define checkDot(dir) DotProduct(fromTo, dir)
  9. float right = checkDot(rightVector), up = checkDot(upVector);
  10. float maxDot = -10.0f;
  11. ushort dir;
  12. #define checkMax(_dir, direction) if (_dir > maxDot) {\
  13. dir = direction;\
  14. maxDot = _dir;\
  15. }
  16. checkMax(right, RIGHT);
  17. checkMax(up, UP);
  18. checkMax(-right, LEFT);
  19. checkMax(-up, DOWN);
  20. bool isDirX = dir == RIGHT;
  21. bool isDirY = dir == UP;
  22. Vector2 relativeVel = TakeVectors(other->velocity, collider->velocity);
  23. bool changedY = collider->transform.pastPosition.y != collider->transform.position.y || other->transform.pastPosition.y != other->transform.position.y;
  24. 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
  25. Vector2 normalizedNormal = NormalizedVector(normal);
  26. Vector2 minRelVel = relativeVel;
  27. minRelVel = VectorMax(AbsVector(relativeVel), fallBackVelocity);
  28. minRelVel.x *= (relativeVel.x > .0f) * 2.0f - 1.0f;
  29. minRelVel.y *= (relativeVel.y > .0f) * 2.0f - 1.0f;
  30. float dot = DotProduct(normalizedNormal, minRelVel);
  31. if (dot > .0f) return true;
  32. float e = FMin(collider->restitutionCoef, other->restitutionCoef);
  33. Vector2 jn = VectorByScalar(normalizedNormal, (-1.0f -e) * dot / (collider->invMass + other->invMass) );
  34. jn.y *= changedY;
  35. //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);
  36. TakeVectorsAssign(&collider->velocity, VectorByScalar(jn, collider->invMass));
  37. AddVectorsAssign(&other->velocity, VectorByScalar(jn, other->invMass));
  38. //printf("new other velocity.x: %f, new collider velocity.x: %f. ", other->velocity.x, collider->velocity.x);
  39. #endif
  40. return true;
  41. }
  42. return false;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment