From a44ce0aea06d2a84a5c1037bb93837eda67438b3 Mon Sep 17 00:00:00 2001 From: jalowe13 Date: Sat, 19 Aug 2023 19:46:34 -0500 Subject: [PATCH] 0.25 - Wall phasing fix - Fixed multiple collision issues - Fixed jumping at edge of floor bug - Fixed floor detection at multiple directions This is a good enough build for a stable release. --- TheOneSDL/Application.h | 4 ++-- TheOneSDL/Physics.cpp | 31 ++++++++++++++++++++++--------- TheOneSDL/Player.cpp | 31 +++++++++++++++++++++++-------- TheOneSDL/Player.h | 2 ++ TheOneSDL/Terrain.cpp | 2 +- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/TheOneSDL/Application.h b/TheOneSDL/Application.h index 99d457a..892c8d8 100644 --- a/TheOneSDL/Application.h +++ b/TheOneSDL/Application.h @@ -3,8 +3,8 @@ // Version Number #define VERSION_MAJOR 0 -#define VERSION_MINOR 24 -#define VERSION_PATCH .3 +#define VERSION_MINOR 25 +#define VERSION_PATCH #define STR_HELPER(x) #x // convert to fit window title #define STR(x) STR_HELPER(x) diff --git a/TheOneSDL/Physics.cpp b/TheOneSDL/Physics.cpp index 79dd177..c6ce48f 100644 --- a/TheOneSDL/Physics.cpp +++ b/TheOneSDL/Physics.cpp @@ -37,7 +37,6 @@ float Physics::getTime(){ int Physics::checkRectCollision(SDL_Rect* A, Terrain* terrain) { std::vector* blocks = terrain->getBlockVector(); - for (Block block : *blocks) // Iterate through every block { if (SDL_HasIntersection(A, block.get_Rect())) // Check if its intersecting with another hitbox @@ -45,15 +44,29 @@ int Physics::checkRectCollision(SDL_Rect* A, Terrain* terrain) // Only Check Collision not by direction if(floor == block.name) // If on Floor return Code for on top of block { - char direction = get4Points(A,block.getX(),block.getY()); - if (direction == 'N') + //char direction = get4Points(A,block.getX(),block.getY()); + + if (block.getY() > A->y) // If On top of block { return 1; } - else + if (block.getX() < A->x) // If to the left of a block { + if (block.getY() < (A->y - 20)) // But under another one + { + return 4; + } return 2; } + else if (block.getX() > A->x) // If to the right of a block + { + if (block.getY() < (A->y - 20)) // But under a block + { + return 4; + } + return 3; + } + return 5; // Unknown collision error } } } @@ -68,8 +81,8 @@ char Physics::get4Points(SDL_Rect* A, int centerX, int centerY) float A_centerX, A_centerY; A_centerX = A->x + 16; A_centerY = A->y + 16; - //std::cout << "[" << A_centerX << "," << A_centerY << "]\n"; - //std::cout << " " << A_centerX << "," << A_centerY << " " << centerX << " " << centerY << std::endl; + // std::cout << "[" << A_centerX << "," << A_centerY << "] "; + // std::cout << " " << A_centerX << "," << A_centerY << " " << centerX << " " << centerY << "\r"; // B Points of interest int B_NorthY = centerY; @@ -90,7 +103,7 @@ char Physics::get4Points(SDL_Rect* A, int centerX, int centerY) float dist_E = sqrt(pow(A_centerX - east.first, 2) + pow(A_centerY - east.second, 2)); float dist_W = sqrt(pow(A_centerX - west.first, 2) + pow(A_centerY - west.second, 2)); // std::cout << "Center[" << A_centerX << "," << A_centerY << "]\n"; - //std::cout << "N[" << dist_N << "]S[" << dist_S << "]E[" << dist_E << "]W[" << dist_W << "] "; + // std::cout << "N[" << dist_N << "]S[" << dist_S << "]E[" << dist_E << "]W[" << dist_W << "] "; // Finding minimum distance float mindist = dist_N; @@ -119,9 +132,9 @@ char Physics::get4Points(SDL_Rect* A, int centerX, int centerY) // mindist = (dist_W < mindist) ? dist_W : mindist; // direction = (dist_W < mindist) ? "West" : direction; - //std::cout << "Direction is " << direction; + // std::cout << " Direction is " << direction << " "; - //std::cout << " " << A_centerX << "," << A_centerY << " " << centerX << " " << centerY << std::endl; + // std::cout << "" << A_centerX << "," << A_centerY << " " << centerX << " " << centerY << "\r"; return direction; } diff --git a/TheOneSDL/Player.cpp b/TheOneSDL/Player.cpp index baa1012..7f7d184 100644 --- a/TheOneSDL/Player.cpp +++ b/TheOneSDL/Player.cpp @@ -234,13 +234,26 @@ void Player::checkCollision(int i, Physics* phys_eng) case 0: // Falling phys_eng->incTime(); // Increase time when away from ground yEdit(getY() + phys_eng->getGravity() * phys_eng->getTime()); - //std::cout << "Air Time: " << phys_eng->getTime() << "\n"; + //std::cout << "Air Time: " << phys_eng->getTime() << "\r"; + isColliding = false; break; - case 1: // On Ground + case 1: // Colliding Ground playerFalling = false; phys_eng->resetTime(); // Reset time on ground + isColliding = false; + //std::cout << "~~~Collide~~~\r"; break; - case 2: // No edits + case 2: // Colliding from left + isColliding = true; + xEdit(getX() + getSpeed()+4); + break; + case 3: // Colliding from right + isColliding = true; + xEdit(getX() - getSpeed()-4); + break; + case 4: // Colliding from below + isColliding = true; + yEdit(getY() + getSpeed()+4); break; } @@ -261,7 +274,9 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng) setTexture(textures["run_left"]); inAnimation = true; } - xEdit(getX() - getSpeed()); + // std::cout << "Is colliding " << isColliding << " " << std::endl; + (!isColliding) ? xEdit(getX() - getSpeed()) : xEdit(getX() + getSpeed()); + // xEdit(getX() - getSpeed()); tilemap_x = round(getX()/32); tilemap_y = round(getY()/32); break; @@ -274,7 +289,7 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng) setTexture(textures["run_right"]); inAnimation = true; } - xEdit(getX() + getSpeed()); + (!isColliding) ? xEdit(getX() + getSpeed()) : xEdit(getX() - getSpeed()); tilemap_x = round(getX()/32); tilemap_y = round(getY()/32); break; @@ -302,7 +317,7 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng) { if (!playerFalling) { - yEdit(getY() - getSpeed()); + (!isColliding) ? yEdit(getY() - getSpeed()) : yEdit(getY() + getSpeed()); tilemap_x = round(getX()/32); tilemap_y = round(getY()/32); } @@ -324,7 +339,7 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng) } - // If the Player is not outside of bounds + //If the Player is not outside of bounds if (!boundsCheck(getX(), getY())) { // Movement @@ -332,7 +347,7 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng) { case Right: { - xEdit(getX() + getSpeed()); + xEdit(getX() - getSpeed()); break; } case Left: diff --git a/TheOneSDL/Player.h b/TheOneSDL/Player.h index 137ba45..6609ce7 100644 --- a/TheOneSDL/Player.h +++ b/TheOneSDL/Player.h @@ -73,6 +73,8 @@ class Player //Hitbox Methods bool hitboxCheck(); + // Is Colliding + bool isColliding = false; private: diff --git a/TheOneSDL/Terrain.cpp b/TheOneSDL/Terrain.cpp index 75a4bf6..6600f72 100644 --- a/TheOneSDL/Terrain.cpp +++ b/TheOneSDL/Terrain.cpp @@ -232,7 +232,7 @@ void Terrain::loadLevel(std::string level) std::string level_name = lvl_data[i]["name"].asString(); // Grab level name if (level_name == level) { - std::cout << "Load" << level_name << std::endl; + std::cout << "Load: " << level_name << std::endl; for (int map_i = 0; map_i < 2; map_i++) // Load background and tilemap { switch(map_i)