From 95ab077a7ead089c823b6c0d4d6480b17244c97b Mon Sep 17 00:00:00 2001 From: Nigel Date: Wed, 19 Oct 2022 20:39:20 +0200 Subject: [PATCH] Collisions ball --- src/ballobject.cpp | 42 +++++++++++++++++++++++++++++++++++ src/ballobject.h | 20 +++++++++++++++++ src/game.cpp | 55 +++++++++++++++++++++++++++++++++++----------- src/game.h | 1 + 4 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 src/ballobject.cpp create mode 100644 src/ballobject.h diff --git a/src/ballobject.cpp b/src/ballobject.cpp new file mode 100644 index 0000000..6057fa1 --- /dev/null +++ b/src/ballobject.cpp @@ -0,0 +1,42 @@ +#include "ballobject.h" + +BallObject::BallObject() +: GameObject(), Radius(12.5f), Stuck(true) +{ +} +BallObject::BallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2D sprite) +: GameObject(pos, glm::vec2(radius* 2.0f, radius * 2.0f), sprite, glm::vec3(1.0f), velocity), Radius(radius), Stuck(true) +{ +} + +glm::vec2 BallObject::Move(float dt, unsigned int window_width) +{ + if(!this->Stuck) + { + this->Position += this->Velocity * dt; + if (this->Position.x <= 0.0f) + { + this->Velocity.x = -this->Velocity.x; + this->Position.x = 0.0f; + } + else if(this->Position.x + this->Size.x >= window_width) + { + this->Velocity.x = -this->Velocity.x; + this->Position.x = window_width - this->Size.x; + } + if(this->Position.y <= 0.0f) + { + this->Velocity.y = -this->Velocity.y; + this->Position.y = 0.0f; + } + } + return this->Position; +} + + +void BallObject::Reset(glm::vec2 position, glm::vec2 velocity) +{ + this->Position = position; + this->Velocity = velocity; + this->Stuck = true; +} \ No newline at end of file diff --git a/src/ballobject.h b/src/ballobject.h new file mode 100644 index 0000000..5c78168 --- /dev/null +++ b/src/ballobject.h @@ -0,0 +1,20 @@ +#pragma once +#include "gameobject.h" +#include "texture2d.h" +#include + + +class BallObject : public GameObject +{ + public: + // ball state + float Radius; + bool Stuck; + + BallObject(); + BallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2D sprite); + + glm::vec2 Move(float dt, unsigned int window_width); + void Reset(glm::vec2 position, glm::vec2 velocity); + +}; \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index 2b58d82..958ca7e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -3,25 +3,33 @@ SpriteRenderer* Renderer; - -Game::~Game() -{ - delete Renderer; -} - -Game::Game(unsigned int width, unsigned int height) - : State(GAME_ACTIVE), Keys(), Width(width), Height(height) -{ - -} - // Initial size of the player paddle const glm::vec2 PLAYER_SIZE (100.0f, 20.0f); // Initial velocity of the player paddle const float PLAYER_VELOCITY(500.0f); GameObject* player; +// Initial velocity of the Ball +const glm::vec2 INITIAL_BALL_VELOCITY(100.0f, -350.0f); +// Radius of the ball object +const float BALL_RADIUS = 12.5f; +BallObject* ball; + + + +Game::~Game() +{ + delete Renderer; + delete player; + delete ball; +} + +Game::Game(unsigned int width, unsigned int height) + : State(GAME_ACTIVE), Keys(), Width(width), Height(height) +{ + +} void Game::Init() { @@ -54,6 +62,10 @@ void Game::Init() this->Height - PLAYER_SIZE.y); player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle")); + glm::vec2 ballPos = playerPos + glm::vec2(PLAYER_SIZE.x / 2.0 - BALL_RADIUS, -BALL_RADIUS * 2.0f); + + ball = new BallObject(ballPos, BALL_RADIUS, INITIAL_BALL_VELOCITY, ResourceManager::GetTexture("face")); + } @@ -65,20 +77,36 @@ void Game::ProcessInput(float dt) if( this->Keys[GLFW_KEY_A]) { if(player->Position.x >= 0.0f) + { player->Position.x -= velocity; + if(ball->Stuck) + { + ball->Position.x -= velocity; + } + } + } if( this->Keys[GLFW_KEY_D]) { if(player->Position.x <= this->Width - player->Size.x) + { player->Position.x += velocity; + if(ball->Stuck) + { + ball->Position.x += velocity; + } + } + } + if (this->Keys[GLFW_KEY_SPACE]) + ball->Stuck = false; } } void Game::Update(float dt) { - + ball->Move(dt, this->Width); } void Game::Render() @@ -93,5 +121,6 @@ void Game::Render() this->Levels[this->Level].Draw(*Renderer); player->Draw(*Renderer); + ball->Draw(*Renderer); } } \ No newline at end of file diff --git a/src/game.h b/src/game.h index b7996e3..e3eb190 100644 --- a/src/game.h +++ b/src/game.h @@ -6,6 +6,7 @@ #include "spriterenderer.h" #include "resourcemanager.h" #include "gamelevel.h" +#include "ballobject.h" // Represent the curreent state of the game enum GameState {