summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-03-08 13:01:05 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-03-08 13:01:05 -0500
commit1c995f2e6c589df9da28c5534c7128e1da64bb89 (patch)
tree192d5e1c8d7ae3a25c719fe514cc8f2244142856
parenteda9b238962ca9a7db14bafefce914265040ecf8 (diff)
downloadtanetane-1c995f2e6c589df9da28c5534c7128e1da64bb89.tar.gz
tanetane-1c995f2e6c589df9da28c5534c7128e1da64bb89.tar.bz2
tanetane-1c995f2e6c589df9da28c5534c7128e1da64bb89.zip
Split collision detection into two phases, horizontal and vertical
The whole function should probably be refactored at some point, possibly like therapy5's but that may be too complicated.

#14
-rw-r--r--src/transform_system.cpp25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/transform_system.cpp b/src/transform_system.cpp index 71b3a4f..055f546 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp
@@ -57,9 +57,12 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i curLoc, vec2
57 57
58 vec2i mapBounds = map.getMapSize() * map.getTileSize(); 58 vec2i mapBounds = map.getMapSize() * map.getTileSize();
59 59
60 // First check horizontal movement only.
61 vec2i horizMovement = { newLoc.x(), curLoc.y() };
62
60 vec2i oldColUL = curLoc + sprite.collisionOffset; 63 vec2i oldColUL = curLoc + sprite.collisionOffset;
61 vec2i oldColDR = oldColUL + sprite.collisionSize; 64 vec2i oldColDR = oldColUL + sprite.collisionSize;
62 vec2i newColUL = newLoc + sprite.collisionOffset; 65 vec2i newColUL = horizMovement + sprite.collisionOffset;
63 vec2i newColDR = newColUL + sprite.collisionSize; 66 vec2i newColDR = newColUL + sprite.collisionSize;
64 67
65 vec2i oldTileUL = oldColUL / map.getTileSize(); 68 vec2i oldTileUL = oldColUL / map.getTileSize();
@@ -154,6 +157,26 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i curLoc, vec2
154 } 157 }
155 } 158 }
156 159
160 // For vertical movement, assume that the horizontal movement has already
161 // taken place (as long as it was not in fact blocked).
162 vec2i effectiveCurLoc = curLoc;
163 vec2i vertMovement = newLoc;
164 if (result.horiz.blocked) {
165 vertMovement.x() = curLoc.x();
166 } else {
167 effectiveCurLoc.x() = newLoc.x();
168 }
169
170 oldColUL = effectiveCurLoc + sprite.collisionOffset;
171 oldColDR = oldColUL + sprite.collisionSize;
172 newColUL = vertMovement + sprite.collisionOffset;
173 newColDR = newColUL + sprite.collisionSize;
174
175 oldTileUL = oldColUL / map.getTileSize();
176 newTileUL = newColUL / map.getTileSize();
177 oldTileDR = oldColDR / map.getTileSize();
178 newTileDR = newColDR / map.getTileSize();
179
157 if (dirHasDir(dir, Direction::down)) { 180 if (dirHasDir(dir, Direction::down)) {
158 if (newTileDR.y() > oldTileDR.y() && 181 if (newTileDR.y() > oldTileDR.y() &&
159 newColDR.y() < mapBounds.h()) { 182 newColDR.y() < mapBounds.h()) {