1
0

Collisions ball

This commit is contained in:
2022-10-19 20:39:20 +02:00
parent dbb0db2130
commit 95ab077a7e
4 changed files with 105 additions and 13 deletions

42
src/ballobject.cpp Normal file
View File

@@ -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;
}

20
src/ballobject.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include "gameobject.h"
#include "texture2d.h"
#include <glm/glm.hpp>
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);
};

View File

@@ -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);
}
}

View File

@@ -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 {