Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Dec 1, 2023
1 parent b6e7aec commit 752cf45
Show file tree
Hide file tree
Showing 20 changed files with 336 additions and 42 deletions.
Binary file added AimGL/resources/Textures/muzzle_flash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ set(PROJECT_SOURCES
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/GameObjects/Target.cpp
World/GameObjects/Rifle.cpp
Utils/Mouse.cpp
)
2 changes: 1 addition & 1 deletion AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Game::Game()
mAppStack.saveState<GameState>(State_ID::GameState, *mGameWindow);

// Initial state of the statestack is TitleState
mAppStack.push(State_ID::LogoState);
mAppStack.push(State_ID::GameState);
}

void Game::run()
Expand Down
29 changes: 5 additions & 24 deletions AimGL/src/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,31 @@

Player::Player(WindowToRender& window)
: mCamera(window)
, mGun("resources/Models/ak47/ak47.obj",
{{"resources/Models/ak47/ak47-alternative.png", Texture::Type::Diffuse},
{"resources/Models/ak47/ak47-alternative-specular.png", Texture::Type::Specular}})
, mCrosshairTexture("resources/Textures/crosshair.png")
, mCrosshair(mCrosshairTexture)
, mRifle(mCamera)
{
mCrosshair.setPosition({window.getSize().x / 2.f, window.getSize().y / 2.f},
Sprite2D::Origin::Center);
mCrosshair.setOpacity(0.8f);
mCamera.cameraPosition({mPosition.x, mPosition.y + PLAYER_HEGIHT, mPosition.z});
mGun.setPosition(mCamera.cameraPosition(), Model::Origin::Center);
//mGun.setPosition(mCamera.cameraPosition(), Model::Origin::Center);
}

void Player::draw(const Renderer& target) const
{
glDepthRange(0.0, 0.01);
mGun.draw(target, mCamera);
mRifle.draw(target, mCamera);
glDepthRange(0.0, 1.0);

mCrosshair.draw(target);
}

void Player::updateGunPosition(const float& deltaTime)
{
const glm::vec3 rotationOrigin = {0.3f, -0.2f, -0.35f};
const glm::vec3 targetPosition = {mPosition.x + rotationOrigin.x,
mPosition.y + PLAYER_HEGIHT * 5 / 8,
mPosition.z + rotationOrigin.z};

const auto gunPosition = lerp(mGun.position(), targetPosition, deltaTime * 30.f);
mGun.setPosition(gunPosition, Model::Origin::Center);
mGun.setRotationOrigin(rotationOrigin);

const auto gunTargetPitch =
lerp(mGun.rotation().pitch, mCamera.rotation().pitch, deltaTime * 20.f);
const auto gunTargetYaw =
lerp(mGun.rotation().yaw, -(mCamera.rotation().yaw + 90), deltaTime * 20.f);
mGun.setRotation({gunTargetYaw, gunTargetPitch, 0});
}

void Player::update(const float& deltaTime)
{
mCamera.cameraPosition({mPosition.x, mPosition.y + PLAYER_HEGIHT, mPosition.z});
mCamera.update(deltaTime);
updateGunPosition(deltaTime);
mRifle.updateAttachToCamera(mCamera, deltaTime);
}

void Player::updatePhysics(float deltaTime)
Expand Down Expand Up @@ -152,6 +132,7 @@ void Player::handleEvent(const sf::Event& event)
{
case sf::Keyboard::Space: tryJump(); break;
}
mRifle.handleEvent(event);
}

Camera& Player::camera()
Expand Down
10 changes: 2 additions & 8 deletions AimGL/src/Player/Player.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <Renderer/Graphics/2D/Sprite2D.h>
#include <Renderer/Graphics/3D/Model.h>
#include <World/Camera.h>
#include <World/GameObjects/Rifle.h>

class Renderer;
class Camera;
Expand Down Expand Up @@ -113,17 +113,11 @@ class Player
*/
bool isOnGround() const;

/**
* \brief Updates the position of the player's weapon
* \param deltaTime the time that has passed since the game was last updated.
*/
void updateGunPosition(const float& deltaTime);

private:
Camera mCamera;
glm::vec3 mPosition{0, 0, 0};
glm::vec3 mVelocity{0, 0, 0};
Model mGun;
Rifle mRifle;
Texture mCrosshairTexture;
Sprite2D mCrosshair;
};
69 changes: 69 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
#include "Rotation3D.h"
#include "pch.h"

Rotation3D::Rotation3D()
: mRotation(0,0,0)
{
}

Rotation3D::Rotation3D(float yaw, float pitch, float roll)
: mRotation(yaw, pitch, roll)
{
}

Rotation3D::Rotation3D(const Rotation3D& rotation)
: mRotation(rotation.mRotation)
{
}

Rotation3D& Rotation3D::operator=(const Rotation3D& rotation)
{
mRotation = rotation.mRotation;
return *this;
}

glm::mat4 Rotation3D::rotate(glm::mat4 model, std::initializer_list<EulerAngle> ordering) const
{
for (const auto axisRotation: ordering)
Expand All @@ -27,3 +48,51 @@ void Rotation3D::imGuiRotationSlider()
ImGui::SliderFloat("Pitch", &pitch, -360, 360.0f);
ImGui::SliderFloat("Yaw", &yaw, -360, 360.0f);
}

bool Rotation3D::operator==(const Rotation3D& rhs) const
{
return mRotation == rhs.mRotation;
}

Rotation3D Rotation3D::operator+(const Rotation3D& rhs) const
{
return Rotation3D(mRotation + rhs.mRotation);
}

Rotation3D Rotation3D::operator-(const Rotation3D& rhs) const
{
return Rotation3D(mRotation - rhs.mRotation);
}

Rotation3D Rotation3D::operator*(const Rotation3D& rhs) const
{
return Rotation3D(mRotation * rhs.mRotation);
}

Rotation3D& Rotation3D::operator+=(const Rotation3D& rhs)
{
mRotation += rhs.mRotation;
return *this;
}

Rotation3D& Rotation3D::operator-=(const Rotation3D& rhs)
{
mRotation -= rhs.mRotation;
return *this;
}

Rotation3D& Rotation3D::operator*=(const Rotation3D& rhs)
{
mRotation *= rhs.mRotation;
return *this;
}

Rotation3D::operator glm::vec<3, float>()
{
return mRotation;
}

Rotation3D::Rotation3D(const glm::vec3& rotation)
: mRotation(rotation)
{
}
39 changes: 35 additions & 4 deletions AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@
/**
* \brief Represents a 3D rotation using Euler angles.
*/
struct Rotation3D
class Rotation3D
{
glm::vec3 mRotation{0,0,0};

public:
Rotation3D();
Rotation3D(float yaw, float pitch, float roll);
Rotation3D(const Rotation3D& rotation);
Rotation3D& operator=(const Rotation3D& rotation);
bool operator==(const Rotation3D&) const;
Rotation3D operator+(const Rotation3D&) const;
Rotation3D operator-(const Rotation3D&) const;
Rotation3D operator*(const Rotation3D&) const;
Rotation3D& operator+=(const Rotation3D&);
Rotation3D& operator-=(const Rotation3D&);
Rotation3D& operator*=(const Rotation3D&);
friend Rotation3D operator*(const Rotation3D& lhs, float rhs);
operator glm::vec3();

/**
* \brief Angles of Euler rotation.
*/
Expand All @@ -15,9 +32,9 @@ struct Rotation3D
YAW
};

float yaw{0};
float pitch{0};
float roll{0};
float& yaw = mRotation.x;
float& pitch = mRotation.y;
float& roll = mRotation.z;

/**
* \brief Applies rotation to a 3D model matrix in a specified order.
Expand All @@ -33,4 +50,18 @@ struct Rotation3D
* \brief Renders an ImGui slider interface for adjusting the rotation.
*/
void imGuiRotationSlider();

private:
explicit Rotation3D(const glm::vec3& rotation);
};


inline Rotation3D operator*(const Rotation3D& lhs, float rhs)
{
return Rotation3D(lhs.mRotation * rhs);
}

inline Rotation3D operator*(float lhs, const Rotation3D& rhs)
{
return rhs * lhs;
}
24 changes: 19 additions & 5 deletions AimGL/src/World/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Utils/Mouse.h"
#include <SFML/Window/Keyboard.hpp>
#include <Utils/Lerp.h>

Camera::Camera(const WindowToRender& target)
: mRenderTarget(target)
Expand All @@ -16,6 +17,13 @@ Camera::Camera(const WindowToRender& target)

void Camera::update(const float& deltaTime)
{
auto tolerance = 2.f;
mCurrentShakeValues = lerp(mCurrentShakeValues, mTargetShakeValues, deltaTime * 6.f);
auto difference = glm::abs(static_cast<glm::vec3>(mCurrentShakeValues - mTargetShakeValues));
if(difference.x <= tolerance && difference.y <= tolerance && difference.z <= tolerance)
{
mTargetShakeValues = {0,0,0};
}
handleMouseInputs(deltaTime);

auto width = static_cast<float>(mRenderTarget.getSize().x);
Expand All @@ -41,15 +49,16 @@ void Camera::handleMouseInputs(const float& deltaTime)

void Camera::calculateCameraDirectionVector()
{
auto rotation = mRotation + mCurrentShakeValues;
glm::vec3 direction;
direction.x = cos(glm::radians(mRotation.yaw)) * cos(glm::radians(mRotation.pitch));
direction.y = sin(glm::radians(mRotation.pitch));
direction.z = sin(glm::radians(mRotation.yaw)) * cos(glm::radians(mRotation.pitch));
direction.x = cos(glm::radians(rotation.yaw)) * cos(glm::radians(rotation.pitch));
direction.y = sin(glm::radians(rotation.pitch));
direction.z = sin(glm::radians(rotation.yaw)) * cos(glm::radians(rotation.pitch));
mCameraFront = glm::normalize(direction);

direction.x = cos(glm::radians(mRotation.yaw));
direction.x = cos(glm::radians(rotation.yaw));
direction.y = 0;
direction.z = sin(glm::radians(mRotation.yaw));
direction.z = sin(glm::radians(rotation.yaw));
mCameraFrontWithoutPitch = glm::normalize(direction);
}

Expand Down Expand Up @@ -157,6 +166,11 @@ Rotation3D Camera::rotation() const
return mRotation;
}

void Camera::shake()
{
mTargetShakeValues = {0.f, 4.f, 0.f};
}

glm::vec3 Camera::direction() const
{
return mCameraFront;
Expand Down
5 changes: 5 additions & 0 deletions AimGL/src/World/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class Camera
*/
Rotation3D rotation() const;

void shake();

private:
/**
* Handle keyboard behavior such as moving the camera inside the game
Expand Down Expand Up @@ -162,6 +164,9 @@ class Camera
glm::vec3 mCameraFrontWithoutPitch = glm::vec3(0.0f, 0.0f, -1.0f);
Rotation3D mRotation{-90, 0, 0.f};

Rotation3D mTargetShakeValues{0,0,0};
Rotation3D mCurrentShakeValues{0,0,0};

float mCameraSpeed = 5.f;
float mCameraSensitivity = 4.f;
float mFovCamera = 90.0f;
Expand Down
56 changes: 56 additions & 0 deletions AimGL/src/World/GameObjects/Rifle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "Rifle.h"
#include "pch.h"

#include <Utils/Lerp.h>
#include <World/Camera.h>

Rifle::Rifle(Camera& camera)
: mCamera(camera)
, mGun("resources/Models/ak47/ak47.obj",
{{"resources/Models/ak47/ak47-alternative.png", Texture::Type::Diffuse},
{"resources/Models/ak47/ak47-alternative-specular.png", Texture::Type::Specular}})
{

}

void Rifle::draw(const Renderer& target, const Camera& camera) const
{
mGun.draw(target, camera);
}

void Rifle::updateAttachToCamera(const Camera& camera, const float& deltaTime)
{
auto backward = -camera.direction();
auto recoilOffset = backward * mCurrentRecoil;

const glm::vec3 rotationOrigin = {0.3f, -0.2f, -0.35f};
const auto cameraPosition = camera.cameraPosition();
glm::vec3 targetPosition = {cameraPosition.x + rotationOrigin.x,
cameraPosition.y - 0.3f,
cameraPosition.z + rotationOrigin.z};

targetPosition += recoilOffset;

const auto gunPosition = lerp(mGun.position(), targetPosition, deltaTime * 30.f);
mGun.setPosition(gunPosition, Model::Origin::Center);
mGun.setRotationOrigin(rotationOrigin);

const auto gunTargetPitch =
lerp(mGun.rotation().pitch, camera.rotation().pitch, deltaTime * 20.f);
const auto gunTargetYaw =
lerp(mGun.rotation().yaw, -(camera.rotation().yaw + 90), deltaTime * 20.f);
mGun.setRotation({gunTargetYaw, gunTargetPitch, 0});

mCurrentRecoil -= deltaTime;
mCurrentRecoil = std::max(mCurrentRecoil, 0.0f);
}

void Rifle::handleEvent(const sf::Event& event)
{
if (event.type == sf::Event::MouseButtonPressed && event.key.code == sf::Mouse::Left)
{
mCurrentRecoil += RECOIL_OFFSET_INCREMENT;
mCurrentRecoil = std::min(mCurrentRecoil, RECOIL_OFFSET_MAX);
mCamera.shake();
}
}
Loading

0 comments on commit 752cf45

Please sign in to comment.