Collisions ball
This commit is contained in:
42
src/ballobject.cpp
Normal file
42
src/ballobject.cpp
Normal 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
20
src/ballobject.h
Normal 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);
|
||||||
|
|
||||||
|
};
|
||||||
55
src/game.cpp
55
src/game.cpp
@@ -3,25 +3,33 @@
|
|||||||
|
|
||||||
SpriteRenderer* Renderer;
|
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
|
// Initial size of the player paddle
|
||||||
const glm::vec2 PLAYER_SIZE (100.0f, 20.0f);
|
const glm::vec2 PLAYER_SIZE (100.0f, 20.0f);
|
||||||
// Initial velocity of the player paddle
|
// Initial velocity of the player paddle
|
||||||
const float PLAYER_VELOCITY(500.0f);
|
const float PLAYER_VELOCITY(500.0f);
|
||||||
GameObject* player;
|
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()
|
void Game::Init()
|
||||||
{
|
{
|
||||||
@@ -54,6 +62,10 @@ void Game::Init()
|
|||||||
this->Height - PLAYER_SIZE.y);
|
this->Height - PLAYER_SIZE.y);
|
||||||
player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle"));
|
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( this->Keys[GLFW_KEY_A])
|
||||||
{
|
{
|
||||||
if(player->Position.x >= 0.0f)
|
if(player->Position.x >= 0.0f)
|
||||||
|
{
|
||||||
player->Position.x -= velocity;
|
player->Position.x -= velocity;
|
||||||
|
if(ball->Stuck)
|
||||||
|
{
|
||||||
|
ball->Position.x -= velocity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if( this->Keys[GLFW_KEY_D])
|
if( this->Keys[GLFW_KEY_D])
|
||||||
{
|
{
|
||||||
if(player->Position.x <= this->Width - player->Size.x)
|
if(player->Position.x <= this->Width - player->Size.x)
|
||||||
|
{
|
||||||
player->Position.x += velocity;
|
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)
|
void Game::Update(float dt)
|
||||||
{
|
{
|
||||||
|
ball->Move(dt, this->Width);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::Render()
|
void Game::Render()
|
||||||
@@ -93,5 +121,6 @@ void Game::Render()
|
|||||||
this->Levels[this->Level].Draw(*Renderer);
|
this->Levels[this->Level].Draw(*Renderer);
|
||||||
|
|
||||||
player->Draw(*Renderer);
|
player->Draw(*Renderer);
|
||||||
|
ball->Draw(*Renderer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "spriterenderer.h"
|
#include "spriterenderer.h"
|
||||||
#include "resourcemanager.h"
|
#include "resourcemanager.h"
|
||||||
#include "gamelevel.h"
|
#include "gamelevel.h"
|
||||||
|
#include "ballobject.h"
|
||||||
|
|
||||||
// Represent the curreent state of the game
|
// Represent the curreent state of the game
|
||||||
enum GameState {
|
enum GameState {
|
||||||
|
|||||||
Reference in New Issue
Block a user