Skip to content
Snippets Groups Projects
Commit 5219277b authored by Мартин Трајковски's avatar Мартин Трајковски
Browse files

Fixed Ball-Rectangle Collision

parent ac3d054f
No related branches found
No related tags found
No related merge requests found
......@@ -16,24 +16,24 @@ float clamp(float value, float minn, float maxx) {
}
Collision BallObject::checkCollision(GameObject& obj) {
glm::vec2 center(this->Position + this->Radius);
// calculate AABB info (center, half-extents)
glm::vec2 rectangle_half_extents(obj.Size.x / 2.0f, obj.Size.y / 2.0f);
glm::vec2 rectangle_center(obj.Position.x + rectangle_half_extents.x, obj.Position.y + rectangle_half_extents.y);
glm::vec2 difference = center - rectangle_center;
glm::vec2 clamped = glm::clamp(difference, -rectangle_half_extents, rectangle_half_extents);
// add clamped value to AABB_center and we get the value of box closest to circle
glm::vec2 closest = rectangle_center + clamped;
glm::vec2 center(this->Position);
// calculate AABB info (center, half-extents)
glm::vec2 rectangle_half_extents(obj.Size.x / 2.0f, obj.Size.y / 2.0f);
glm::vec2 rectangle_center(obj.Position.x, obj.Position.y);
glm::vec2 difference = center - rectangle_center;
glm::vec2 clamped = glm::clamp(difference, -rectangle_half_extents, rectangle_half_extents);
// add clamped value to AABB_center and we get the value of box closest to circle
glm::vec2 closest = rectangle_center + clamped;
difference = closest - center;
if (glm::length(difference) <= this->Radius / 2.0f) {
return Collision(true, VectorDirection(difference), difference);
}
else {
return Collision();
}
difference = closest - center;
if (glm::length(difference) <= this->Radius) {
return Collision(true, VectorDirection(difference), difference);
}
else {
return Collision();
}
}
glm::vec3& BallObject::Move(float dt, unsigned int windowWidth, unsigned int windowHeight) {
......
......@@ -65,9 +65,7 @@ void Game::LoadFiles() {
}
glm::vec3 camPosition(0.0f, 0.0f, 0.000001f);
glm::vec3 worldUp(0.0f, 1.0f, 0.0f);
glm::vec3 camFront(0.0f, 0.0f, 0.0f);
void Game::Init()
{
......@@ -77,6 +75,7 @@ void Game::Init()
ResourceManager::GetShader("sprite3D").SetInteger("image", 0, true);
ResourceManager::GetShader("sprite3D").SetMatrix4("projection", glm::ortho(0.0f, (float)this->Width, (float)this->Height, 0.0f, 0.0f, 1.0f));
ResourceManager::GetShader("sprite3D").SetMatrix4("view", glm::mat4(1.0f));
//create game menu
Option option1("GAME START");
......@@ -104,7 +103,7 @@ void Game::Init()
void Game::Update(float dt)
{
//std::cout << camPosition.x << " " << camPosition.y << " " << camPosition.z << std::endl;
ResourceManager::GetShader("sprite3D").SetMatrix4("view", glm::lookAt(camPosition, camPosition * camFront, worldUp), true);
//ResourceManager::GetShader("sprite3D").SetMatrix4("view", glm::lookAt(camPosition, camPosition * camFront, worldUp), true);
if (this->State == GAME_ACTIVE) {
for (auto& object : this->Levels[Level]->Objects) {
object->Move(dt, this->Width, this->Height);
......@@ -157,35 +156,17 @@ void Game::ProcessInput(float dt)
this->KeysProcessed[GLFW_KEY_ENTER] = true;
}
float camVelocity = 20.0f;
if (this->Keys[GLFW_KEY_W]) {
camPosition.z -= camVelocity * dt;
}
if (this->Keys[GLFW_KEY_S]) {
camPosition.z += camVelocity * dt;
}
if (this->Keys[GLFW_KEY_A]) {
camPosition.x -= camVelocity * dt;
}
if (this->Keys[GLFW_KEY_D]) {
camPosition.x += camVelocity * dt;
}
std::string direction = "";
if (this->Keys[GLFW_KEY_LEFT]) {
if (Player->Position.x >= 0) {
if (Player->Position.x >= Player->Size.x / 2.0f) {
Player->Position.x -= Player->Velocity.x * dt;
}
direction = "-left-";
}
if (this->Keys[GLFW_KEY_RIGHT]) {
if (Player->Position.x + Player->Size.x <= this->Width) {
if (Player->Position.x <= this->Width - Player->Size.x / 2.0f) {
Player->Position.x += Player->Velocity.x * dt;
}
direction = "-right-";
......@@ -285,7 +266,7 @@ void Game::Render()
void Game::DoCollisions() {
for (auto& object : this->Levels[this->Level]->Objects) {
if (!object->Destroyed && dynamic_cast<BallObject*>(object) != nullptr) {
Collision& collisionPlayer = Player->checkCollision(*object);
Collision& collisionPlayer = object->checkCollision(*Player);
if (collisionPlayer.collision) {
--this->Lives;
SoundEngine->stopAllSounds();
......@@ -296,7 +277,7 @@ void Game::DoCollisions() {
}
for (auto& Weapon : Player->Weapons) {
Collision& collisionWeapon = Weapon->checkCollision(*object);
Collision& collisionWeapon = object->checkCollision(*Weapon);
if (Weapon->Using && collisionWeapon.collision) {
if (dynamic_cast<BallObject*>(object) != nullptr) {
object->Destroyed = true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment