Skip to content

Commit

Permalink
Add collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Dec 14, 2023
1 parent 49134b7 commit 003afff
Show file tree
Hide file tree
Showing 35 changed files with 8,655 additions and 4,862 deletions.
10,109 changes: 5,273 additions & 4,836 deletions AimGL/resources/Models/ak47/ak47.obj

Large diffs are not rendered by default.

2,488 changes: 2,488 additions & 0 deletions AimGL/resources/Models/sphere.obj

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions AimGL/resources/Shaders/Graphics/Physics/AABB.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 330 core
out vec4 FragColor;

uniform vec3 aabbColor = vec3(0,0.8,0);

void main() {
FragColor = vec4(aabbColor, 1.0);
}
10 changes: 10 additions & 0 deletions AimGL/resources/Shaders/Graphics/Physics/AABB.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 330 core
layout (location = 0) in vec3 aPos;

uniform mat4 model = mat4(1.0);
uniform mat4 view;
uniform mat4 projection;

void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
2 changes: 1 addition & 1 deletion AimGL/resources/Shaders/Graphics/Physics/Ray.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#version 330 core
out vec4 FragColor;

uniform vec3 rayColor = vec3(0,1,0);
uniform vec3 rayColor = vec3(0,0.8,0);

void main() {
FragColor = vec4(rayColor, 1.0);
Expand Down
Binary file added AimGL/resources/Sounds/target-shot.wav
Binary file not shown.
11 changes: 10 additions & 1 deletion AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ set(PROJECT_SOURCES
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/Scene/GameObjects/Rifle.cpp
World/Physics/Ray.cpp
World/Scene/GameObjects/PreviewTarget.cpp
World/Physics/Drawable/AABB.cpp
World/Physics/Drawable/Ray.cpp
World/Physics/Drawable/DrawableCollider.cpp
World/Physics/Collider.cpp
World/Physics/CollisionRegister.cpp
World/Physics/RayCollider.cpp
World/Physics/RectangleCollider.cpp
World/Physics/SphereCollider.cpp
World/Physics/Collisions.cpp
Utils/Mouse.cpp
)
4 changes: 2 additions & 2 deletions AimGL/src/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "Utils/Lerp.h"
#include "pch.h"

Player::Player(WindowToRender& window)
Player::Player(WindowToRender& window, CollisionRegister& collisionRegister)
: mCamera(window)
, mCrosshairTexture("resources/Textures/crosshair.png")
, mCrosshair(mCrosshairTexture)
, mRifle(mCamera)
, mRifle(mCamera, collisionRegister)
{
mCrosshair.setPosition({window.getSize().x / 2.f, window.getSize().y / 2.f},
Sprite2D::Origin::Center);
Expand Down
3 changes: 2 additions & 1 deletion AimGL/src/Player/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class Player
/**
* \brief Player class constructor
* \param window The window into which the player's eye view is rendered
* \param collisionRegister Register in which all collisions on the scene should be located
*/
explicit Player(WindowToRender& window);
Player(WindowToRender& window, CollisionRegister& collisionRegister);

static constexpr auto PLAYER_HEGIHT = 0.8f;
static constexpr auto PLAYER_MAX_HORIZONTAL_SPEED = 5.f;
Expand Down
21 changes: 20 additions & 1 deletion AimGL/src/States/CustomStates/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
GameState::GameState(StateStack& stack, WindowToRender& window)
: State(stack)
, mWindow(window)
, mPlayer(window)
, mPlayer(window, mCollisionRegister)
, mRenderer(mWindow)
, mLogoTexture("resources/Textures/logo_background.png")
, mLogo(mLogoTexture)
Expand All @@ -28,6 +28,15 @@ GameState::GameState(StateStack& stack, WindowToRender& window)
mGameBackground.setPosition({0, 0});
mPhaseInLogoColor.setPosition({0, 0});
mPhaseInClock.restart();

std::vector<glm::vec3> samplePreviewTargetsPositons = {
{2, 2, 2}, {2, 1, 2}, {2, 3, 2}, {2.5, 2, 2.6}, {3, 3, 3},
{3, 2, 1}, {1, 2, 3}, {0, 3, 2}, {0, 1, 1}, {0, 2, 4}};
for (auto& position: samplePreviewTargetsPositons)
{
auto test = std::make_unique<PreviewTarget>(mCollisionRegister, position);
mPreviewTargets.push_back(std::move(test));
}
}

void GameState::draw(sf::Window& target) const
Expand All @@ -38,19 +47,29 @@ void GameState::draw(sf::Window& target) const
mTree.draw(mRenderer, mPlayer.camera());
mLogo.draw(mRenderer, mPlayer.camera());
mPhaseInLogoColor.draw(mRenderer);

for (auto& previewTarget: mPreviewTargets)
{
previewTarget->draw(mRenderer, mPlayer.camera());
}
mPlayer.draw(mRenderer);
}

bool GameState::fixedUpdate(const float& deltaTime)
{
MTR_SCOPE("GameState", "GameState::fixedUpdate");
mPlayer.fixedUpdate(deltaTime);
mCollisionRegister.updateAllCollisions();
return true;
}

bool GameState::update(const float& deltaTime)
{
MTR_SCOPE("GameState", "GameState::update");
for (auto& previewTarget: mPreviewTargets)
{
previewTarget->update(deltaTime);
}
mPlayer.update(deltaTime);

if (mPhaseInLogoColor.opacity() > 0)
Expand Down
4 changes: 4 additions & 0 deletions AimGL/src/States/CustomStates/GameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <Player/Player.h>
#include <Renderer/Graphics/3D/Model.h>
#include <World/Physics/CollisionRegister.h>
#include <World/Scene/GameObjects/PreviewTarget.h>

class StateStack;

Expand Down Expand Up @@ -53,6 +55,7 @@ class GameState : public State

private:
WindowToRender& mWindow;
CollisionRegister mCollisionRegister;
Player mPlayer;
Renderer mRenderer;
Texture mLogoTexture;
Expand All @@ -62,4 +65,5 @@ class GameState : public State
Rectangle2D mPhaseInLogoColor;
InfiniteGridFloor mInfiniteGridFloor;
Model mTree;
std::vector<std::unique_ptr<PreviewTarget>> mPreviewTargets;
};
5 changes: 2 additions & 3 deletions AimGL/src/World/Camera.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#pragma once
#include "Game.h"

#include <Renderer/Graphics/3D/Utils/Rotation3D.h>
#include "Renderer/Graphics/3D/Utils/Rotation3D.h"
#include <glm/matrix.hpp>
#include <glm/vec3.hpp>


/**
* An in-game camera through which the player views gameplay.
*/
class Camera
{
public:
Camera(const WindowToRender& target);
explicit Camera(const WindowToRender& target);

/**
* \brief Updates the camera logic at equal intervals independent of the frame rate.
Expand Down
39 changes: 39 additions & 0 deletions AimGL/src/World/Physics/Collider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "Collider.h"

#include "World/Physics/CollisionRegister.h"
#include "pch.h"
#include <utility>

Collider::Collider(CollisionRegister& collisionRegister)
: mCollisionRegister(collisionRegister)
{
mCollisionRegister.add(*this);
}

Collider::~Collider()
{
mCollisionRegister.remove(*this);
}

void Collider::executeCallback(const Collider& other) const
{
if (mCallback)
{
mCallback(other);
}
}

void Collider::callback(std::function<void(const Collider&)> callbackFunction)
{
mCallback = std::move(callbackFunction);
}

ColliderTag Collider::colliderTag() const
{
return mColliderTag;
}

void Collider::colliderTag(ColliderTag colliderTag)
{
mColliderTag = colliderTag;
}
78 changes: 78 additions & 0 deletions AimGL/src/World/Physics/Collider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once
#include "World/Physics/ColliderTag.h"

class SphereCollider;
class RectangleCollider;
class RayCollider;
class CollisionRegister;

/**
* \brief Base class for collision detection objects.
*/
class Collider
{
public:
/**
* \brief Constructor that registers the collider with a collision management system.
* \param collisionRegister Reference to the collision register for tracking colliders.
*/
Collider(CollisionRegister& collisionRegister);
virtual ~Collider();

/**
* \brief Pure virtual method to check collision with another collider.
* \param other Reference to another collider.
* \return True if collision occurs, false otherwise.
*/
[[nodiscard]] virtual bool checkCollision(const Collider& other) const = 0;

/**
* \brief Checks collision with a SphereCollider.
* \param other SphereCollider to check against.
* \return True if collision occurs, false otherwise.
*/
[[nodiscard]] virtual bool checkCollisionWith(const SphereCollider& other) const = 0;

/**
* \brief Checks collision with a RectangleCollider.
* \param other RectangleCollider to check against.
* \return True if collision occurs, false otherwise.
*/
[[nodiscard]] virtual bool checkCollisionWith(const RectangleCollider& other) const = 0;

/**
* \brief Checks collision with a RayCollider.
* \param other RayCollider to check against.
* \return True if collision occurs, false otherwise.
*/
[[nodiscard]] virtual bool checkCollisionWith(const RayCollider& other) const = 0;

/**
* \brief Executes a callback function upon collision detection.
* \param other Collider that triggered the callback.
*/
void executeCallback(const Collider& other) const;

/**
* \brief Sets the callback function to be executed on collision.
* \param callbackFunction The function to call on collision.
*/
virtual void callback(std::function<void(const Collider&)> callbackFunction);

/**
* \brief Gets the tag identifying the type of collider.
* \return The collider's tag.
*/
[[nodiscard]] ColliderTag colliderTag() const;

/**
* \brief Sets the tag identifying the type of collider.
* \param colliderTag The tag to set.
*/
void colliderTag(ColliderTag colliderTag);

private:
std::function<void(const Collider&)> mCallback;
CollisionRegister& mCollisionRegister;
ColliderTag mColliderTag{ColliderTag::None};
};
11 changes: 11 additions & 0 deletions AimGL/src/World/Physics/ColliderTag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

/**
* \brief Tag identifying the type of collider.
*/
enum class ColliderTag
{
None,
Solid,
GunShot,
};
38 changes: 38 additions & 0 deletions AimGL/src/World/Physics/CollisionRegister.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "CollisionRegister.h"
#include "Collider.h"
#include "pch.h"

void CollisionRegister::add(const Collider& collider)
{
mColliderToAdd.push_back(&collider);
}

void CollisionRegister::remove(const Collider& collider)
{
mColliderToRemove.push_back(&collider);
}

void CollisionRegister::updateAllCollisions()
{
for (auto& collider: mColliderToAdd)
{
mColliders.push_back(collider);
}
mColliderToAdd.clear();
for (auto& collider: mColliderToRemove)
{
mColliders.erase(std::find(mColliders.begin(), mColliders.end(), collider));
}
mColliderToRemove.clear();
for (size_t i = 0; i < mColliders.size(); ++i)
{
for (size_t j = i + 1; j < mColliders.size(); ++j)
{
if (mColliders[i]->checkCollision(*mColliders[j]))
{
mColliders[i]->executeCallback(*mColliders[j]);
mColliders[j]->executeCallback(*mColliders[i]);
}
}
}
}
32 changes: 32 additions & 0 deletions AimGL/src/World/Physics/CollisionRegister.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

class Collider;

/**
* \brief Manages the registration and tracking of colliders for collision detection.
*/
class CollisionRegister
{
public:
/**
* \brief Adds a collider to the registry for collision tracking.
* \param collider Reference to the collider to be added.
*/
void add(const Collider& collider);

/**
* \brief Removes a collider from the registry.
* \param collider Reference to the collider to be removed.
*/
void remove(const Collider& collider);

/**
* \brief Updates and checks for collisions among all registered colliders.
*/
void updateAllCollisions();

private:
std::vector<const Collider*> mColliders;
std::vector<const Collider*> mColliderToRemove;
std::vector<const Collider*> mColliderToAdd;
};
Loading

0 comments on commit 003afff

Please sign in to comment.