Столкновение не работает при очень специфических обстоятельствах.C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Столкновение не работает при очень специфических обстоятельствах.

Сообщение Anonymous »

Это функция обновления моего плеера:
void update() {
IsOnWall = false;
IsOnFloor = false;
int collide[4] = { collidelist(Rect{pos.x, pos.y + vel.y, hitbox.width, vel.y + 1}, Stillbodies),
collidelist(Rect{pos.x, pos.y + hitbox.height, hitbox.width, vel.y - 1}, Stillbodies),
collidelist(Rect{pos.x + vel.x, pos.y + 1, vel.x - 1, hitbox.height - 1}, Stillbodies),
collidelist(Rect{pos.x + hitbox.width, pos.y + 1, vel.x + 1, hitbox.height - 1}, Stillbodies) };
if (collide[0] >= 0 && vel.y = 0 && vel.y > 0) {
vel.y = 0;
pos.y = Stillbodies[collide[1]].y - Stillbodies[collide[1]].height;
}
if (collide[2] >= 0 && vel.x = 0 && vel.x > 0) {
vel.x = 0;
pos.x = Stillbodies[collide[3]].x - hitbox.width;
IsOnWall = true;
}
IsOnWallOnly = (IsOnWall && !IsOnFloor);
IsOnFloorOnly = (IsOnFloor && !IsOnWall);
pos.x += vel.x;
pos.y += vel.y;
hitbox.x = pos.x;
hitbox.y = pos.y;
}

Часть ifcollide[0] работает большую часть времени, но если скорость моего игрока по оси y точно равна высоте объекта, с которым он сталкивается, зажимает игрока на 1 кадр. По крайней мере, это то, что я нашел до сих пор. Все остальные if работают отлично даже при тех же обстоятельствах.
Вот минимальный воспроизводимый пример:
Код
#include
#include
class Rect {
public:
int x;
int y;
int width;
int height;
Rect(int X, int Y, int WIDTH, int HEIGHT) {
x = X;
y = Y;
width = WIDTH;
height = HEIGHT;
}
};
class Spot {
public:
int x;
int y;
Spot(int x1, int y1) {
x = x1;
y = y1;
}
};
bool colliderect(Rect rect1, Rect rect2) {
return (rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y);
}
int collidelist(Rect rect, std::deque rects) {
for (float rec = 0; rec < rects.size(); rec++) {
if (colliderect(rect, rects[rec])) {
return rec;
}
}
return -1;
}
std::deque Stillbodies = { {0, 0, 50, 50} };
int main() {
Spot pos = Spot(0, 50);
Rect hitbox = Rect(pos.x, pos.y, 50, 50);
Spot vel = Spot(0, 0);
while (true) {
char move;
std::cin >> move;
if (move == 'u') {
vel.y = -50;
}
else if (move == 'd') {
vel.y = 50;
}
else {
vel.y = 0;
}
int collide[4] = { collidelist(Rect{pos.x, pos.y + vel.y, hitbox.width, vel.y + 1}, Stillbodies),
collidelist(Rect{pos.x, pos.y + hitbox.height, hitbox.width, vel.y - 1}, Stillbodies),
collidelist(Rect{pos.x + vel.x, pos.y + 1, vel.x - 1, hitbox.height - 1}, Stillbodies),
collidelist(Rect{pos.x + hitbox.width, pos.y + 1, vel.x + 1, hitbox.height - 1}, Stillbodies) };
//broken collision (other ones that look same work)
if (collide[0] >= 0 && vel.y = 0 && vel.y > 0) {
vel.y = 0;
pos.y = Stillbodies[collide[1]].y - hitbox.height;
}
pos.x += vel.x;
pos.y += vel.y;
hitbox.x = pos.x;
hitbox.y = pos.y;
std::cout

Подробнее здесь: https://stackoverflow.com/questions/792 ... cumstances
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»