, как вы можете видеть, по какой -то причине, по какой -то причине, хотя и не так, как и все еще. Вверху ... либо подразумевает левую, справа и внизу.
Код: Выделить всё
float sweptAABB(const Collider& other, Vector2& velocity, Vector2& normal) const {
float xInvEntry, yInvEntry;
float xInvExit, yInvExit;
// Find object distances
if (velocity.x > 0.0f) {
xInvEntry = other.getLeft() - getRight();
xInvExit = other.getRight() - getLeft();
}
else {
xInvEntry = other.getRight() - getLeft();
xInvExit = other.getLeft() - getRight();
}
if (velocity.y > 0.0f) {
yInvEntry = other.getTop() - getBottom();
yInvExit = other.getBottom() - getTop();
}
else {
yInvEntry = other.getBottom() - getTop();
yInvExit = other.getTop() - getBottom();
}
// Time of collision and leaving
float xEntry, yEntry;
float xExit, yExit;
if (velocity.x == 0.0f) {
xEntry = -std::numeric_limits::infinity();
xExit = std::numeric_limits::infinity();
}
else {
xEntry = xInvEntry / velocity.x;
xExit = xInvExit / velocity.x;
}
if (velocity.y == 0.0f) {
yEntry = -std::numeric_limits::infinity();
yExit = std::numeric_limits::infinity();
}
else {
yEntry = yInvEntry / velocity.y;
yExit = yInvExit / velocity.y;
}
// Earliest / latest times of collision
float entryTime = std::max(xEntry, yEntry);
float exitTime = std::min(xExit, yExit);
// If there was no collision
if (entryTime > exitTime || (xEntry < 0.0f && yEntry < 0.0f) || xEntry > 1.0f || yEntry > 1.0f) {
normal = { 0.0f, 0.0f };
return 1.0f;
}
// If there WAS collision
else {
// Calculate normal
if (xEntry > yEntry) {
normal = { xInvEntry < 0.0f ? 1.0f : -1.0f, 0 };
}
else {
normal = { 0, yInvEntry < 0.0f ? 1.0f : -1.0f };
}
return entryTime;
}
}
< /code>
и вот ответ на столкновение < /p>
float toi;
Vector2 normal;
// Currently it checks colliders for collision by brute force,
// but I will change it in the nearest future
for (const Collider& otherCollider : collidersToCheck)
{
toi = collider.sweptAABB(otherCollider, velocity, normal);
}
position.x += velocity.x * toi;
position.y += velocity.y * toi;
< /code>
моя система столкновения работает на фиксированном шаге времени (60 кадров в секунду) < /p>
physicsAccum += GetFrameTime();
while (physicsAccum >= FIXED_DELTA_TIME) {
player->physicsUpdate(FIXED_DELTA_TIME);
physicsAccum -= FIXED_DELTA_TIME;
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... -detection