Skip to content

Commit

Permalink
0.25 - Wall phasing fix
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jalowe13 committed Aug 20, 2023
1 parent 681547b commit a44ce0a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
4 changes: 2 additions & 2 deletions TheOneSDL/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 22 additions & 9 deletions TheOneSDL/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,36 @@ float Physics::getTime(){
int Physics::checkRectCollision(SDL_Rect* A, Terrain* terrain)
{
std::vector<Block>* 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
{
// 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
}
}
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

}
Expand Down
31 changes: 23 additions & 8 deletions TheOneSDL/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -324,15 +339,15 @@ 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
switch (xPath())
{
case Right:
{
xEdit(getX() + getSpeed());
xEdit(getX() - getSpeed());
break;
}
case Left:
Expand Down
2 changes: 2 additions & 0 deletions TheOneSDL/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Player
//Hitbox Methods
bool hitboxCheck();

// Is Colliding
bool isColliding = false;


private:
Expand Down
2 changes: 1 addition & 1 deletion TheOneSDL/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a44ce0a

Please sign in to comment.