mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 12:21:48 +01:00
Added a (temp) collision solver for Axis-Aligned rectangles
This commit is contained in:
@@ -14,11 +14,30 @@ void Player::Draw() const {
|
||||
}
|
||||
|
||||
void Player::Update(float elapsedTime, const WorldLevel& level) {
|
||||
Point2f acc{0, 0};
|
||||
acc.y += m_Gravity.y;
|
||||
// m_Acc.y += m_Gravity.y;
|
||||
// m_Vel.y = std::min(m_Vel.y, m_MaxSpeed);
|
||||
//
|
||||
// Point2f nextPos = m_Position + m_Vel * elapsedTime;
|
||||
// //collision checking
|
||||
m_Vel = Point2f{0, -100};
|
||||
|
||||
Point2f nextPos = m_Position + m_Vel * elapsedTime * acc * elapsedTime * elapsedTime;
|
||||
//collision checking
|
||||
//check for keys
|
||||
if(utils::isKeyDown(SDL_SCANCODE_W)) {
|
||||
m_Vel.y = 100;
|
||||
}
|
||||
if(utils::isKeyDown(SDL_SCANCODE_S)) {
|
||||
m_Vel.y = -100;
|
||||
}
|
||||
if(utils::isKeyDown(SDL_SCANCODE_A)) {
|
||||
m_Vel.x = -100;
|
||||
}
|
||||
if(utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||
m_Vel.x = 100;
|
||||
}
|
||||
|
||||
Point2f nextPos = m_Position + m_Vel * elapsedTime;
|
||||
bool isColliding = false;
|
||||
|
||||
auto tiles = level.GetAllTiles();
|
||||
for (int x{0}; x < WorldLevel::WORLD_WIDTH; ++x) {
|
||||
for (int y{0}; y < WorldLevel::WORLD_HEIGHT; ++y) {
|
||||
@@ -26,20 +45,13 @@ void Player::Update(float elapsedTime, const WorldLevel& level) {
|
||||
if (tile->GetTileType() == GroundTileTypes::Dirt) {
|
||||
Rectf tileRect = Rectf{tile->GetPosition().x, tile->GetPosition().y, WorldLevel::TILE_WIDTH, WorldLevel::TILE_HEIGHT};
|
||||
if (utils::IsOverlapping(nextPos, m_Size, tileRect)) {
|
||||
//collision
|
||||
if (m_Vel.y < 0) {
|
||||
//collision from above
|
||||
m_Position.y = tileRect.bottom;
|
||||
m_Vel.y = 0;
|
||||
} else {
|
||||
m_Position.y = tileRect.bottom + m_Size.y;
|
||||
m_Vel.y = 0;
|
||||
}
|
||||
isColliding = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_Vel = Point2f{m_Vel.x + float(acc.x * elapsedTime), m_Vel.y + float(acc.y * elapsedTime)};
|
||||
// m_Position += m_Vel * elapsedTime;
|
||||
m_Position = Point2f{m_Position.x + m_Vel.x * elapsedTime, m_Position.y + m_Vel.y * elapsedTime};
|
||||
|
||||
if(!isColliding) {
|
||||
m_Position = nextPos;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user