182 glm::vec2 delta = pos -
lastPos;
183 const float BLOCK_SIZE = 1.f;
187 glm::ivec2 mapSize = {100,100};
190 (pos.y < -dimensions.y)
191 || (pos.x < -dimensions.x)
192 || (pos.y > mapSize.x * BLOCK_SIZE)
193 || (pos.x > mapSize.y * BLOCK_SIZE)
200 upTouch, downTouch, leftTouch, rightTouch);
201 pos =
performCollision(map, {newPos.x, pos.y}, {dimensions.x, dimensions.y}, {0, delta.y},
202 upTouch, downTouch, leftTouch, rightTouch);
206glm::vec2
Player::performCollision(
Block *map, glm::vec2 pos, glm::vec2 size, glm::vec2 delta,
bool &upTouch,
bool &downTouch,
bool &leftTouch,
bool &rightTouch)
208 glm::ivec2 mapSize = {100,100};
212 int maxX = mapSize.x;
213 int maxY = mapSize.y;
217 const float BLOCK_SIZE = 1.f;
219 minX = (pos.x - abs(delta.x) - BLOCK_SIZE) / BLOCK_SIZE;
220 maxX = ceil((pos.x + abs(delta.x) + BLOCK_SIZE + size.x) / BLOCK_SIZE);
222 minY = (pos.y - abs(delta.y) - BLOCK_SIZE) / BLOCK_SIZE;
223 maxY = ceil((pos.y + abs(delta.y) + BLOCK_SIZE + size.y) / BLOCK_SIZE);
225 minX = std::max(0, minX);
226 minY = std::max(0, minY);
227 maxX = std::min(mapSize.x, maxX);
228 maxY = std::min(mapSize.y, maxY);
230 auto getMapBlockUnsafe = [&](
int x,
int y) ->
Block *
232 return &map[x + y * mapSize.x];
235 for (
int y = minY; y < maxY; y++)
236 for (
int x = minX; x < maxX; x++)
238 if (getMapBlockUnsafe(x, y)->isCollidable())
240 if (
aabb({pos,dimensions}, {x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE}, 0.0001f))
247 pos.x = x * BLOCK_SIZE + BLOCK_SIZE;
253 pos.x = x * BLOCK_SIZE - dimensions.x;
257 else if (delta.y != 0)
262 pos.y = y * BLOCK_SIZE + BLOCK_SIZE;
268 pos.y = y * BLOCK_SIZE - dimensions.y;
285 glm::ivec2 mapSize = {100,100};
294 float distance = glm::length(
lastPos - pos);
295 const float BLOCK_SIZE = 1.f;
297 if (distance < BLOCK_SIZE)
311 glm::vec2 delta = pos -
lastPos;
312 delta = glm::normalize(delta);
313 delta *= 0.9 * BLOCK_SIZE;
318 glm::vec2 posTest = newPos;
327 if (newPos != posTest)
333 }
while (glm::length((newPos + delta) - pos) > 1.0f * BLOCK_SIZE);
347 if (pos.x < 0) { pos.x = 0; leftTouch =
true; }
350 pos.x = ((mapSize.x) * BLOCK_SIZE) -
position.
size.x; rightTouch =
true;
glm::vec2 performCollision(Block *map, glm::vec2 pos, glm::vec2 size, glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
void checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos, Block *map, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)