Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
- added 1 bot (movement is not correct currently)
- added the ability to shoot
- frictionforce is now dependent on the velocity (slower acceleration with higher speed)
- with X the player can now "brake"
- the speedlabel now also shows negative values
- added info2 with blue color to the Logger
- camera now rotates around player if the spaceship is rotating
- camera now changes the distance to the spaceship depending on the velocity
  • Loading branch information
Christian Koehlke committed Apr 14, 2021
1 parent 82805ec commit 1b180b3
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 80 deletions.
16 changes: 13 additions & 3 deletions Game/Bullet.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Bullet.h"
#include "Game.h"

Bullet::Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 direction) : Object(shader, "arrow.bmf")
Bullet::Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 direction) : Object(shader, "laser.bmf")
{
this->setType(ObjectType::Object_Bullet);
this->position = position;
Expand All @@ -11,7 +11,17 @@ Bullet::Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3
this->setCollisionBoxType(CollisionBoxType::cube);
}

Bullet::Bullet(Shader* shader) : Object(shader, "arrow.bmf")
Bullet::Bullet(Shader* shader, glm::vec3 position, glm::quat rotation, glm::vec3 direction) : Object(shader, "laser.bmf")
{
this->setType(ObjectType::Object_Bullet);
this->position = position;
this->setRotationQuat(rotation);
this->movement = glm::normalize(direction) * speed;// *Game::getDelta(); //todo : apply Game::getDelta() per frame
this->name = "Bullet";
this->setCollisionBoxType(CollisionBoxType::cube);
}

Bullet::Bullet(Shader* shader) : Object(shader, "laser.bmf")
{
this->setType(ObjectType::Object_Bullet);
this->name = "Bullet";
Expand All @@ -23,7 +33,7 @@ void Bullet::fall()
this->Object::fall();

if (movement == glm::vec3(0, 0, 0)) return;
if (position.y < 0.5) { movement=glm::vec3(0, 0, 0); return; }
//if (position.y < 0.5) { movement=glm::vec3(0, 0, 0); return; }


float gegk = movement.y;
Expand Down
4 changes: 3 additions & 1 deletion Game/Bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Bullet : public Object
public:
Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 direction);

Bullet(Shader* shader, glm::vec3 position, glm::quat rotation, glm::vec3 direction);

Bullet(Shader* shader);

//void move();
Expand All @@ -21,7 +23,7 @@ class Bullet : public Object

private:

float speed = 70;
float speed = 200;

};

90 changes: 52 additions & 38 deletions Game/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <cmath>

Character::Character(Shader* shader) : Object(shader, "character.bmf")
Character::Character(Shader* shader) : Object(shader, "spaceship.bmf")
{
this->shader = shader;
vecFront = glm::vec3(1.0f, 0.0f, 0.0f);
Expand Down Expand Up @@ -57,43 +57,28 @@ void Character::interactWithObject()

void Character::resetVerticalMovement()
{
////movement = movement * glm::vec3(0, 0, 0);
float brakevalue = 0.02f;
//
Logger::logVector(movement,"Move",2,"\033[34m");

//if (movement.x > brakevalue) movement.x -= brakevalue;
//if (movement.x < -brakevalue) movement.x += brakevalue;
//if (movement.x > -brakevalue && movement.x < brakevalue) movement.x = 0;

//if (movement.y > brakevalue) movement.y -= brakevalue;
//if (movement.y < -brakevalue) movement.y += brakevalue;
//if (movement.y > -brakevalue && movement.y < brakevalue) movement.y = 0;

//if (movement.z > brakevalue) movement.z -= brakevalue;
//if (movement.z < -brakevalue) movement.z += brakevalue;
//if (movement.z > -brakevalue && movement.z < brakevalue) movement.z = 0;

/*if (!Game::isKeyPressed(PlayerAction::moveForward)) {
glm::vec3 moveFront = movement * vecFront;
if (glm::length(moveFront) > brakevalue) {
if (glm::dot(movement, vecFront) > 0) {
Logger::warn("moving forward");
movement += glm::normalize(moveFront) * brakevalue;
}
else {
Logger::warn("moving backward");
movement -= glm::normalize(moveFront) * brakevalue;
}
}
}*/
float brakevalue = 0.001f;
brakevalue *= glm::length(movement);

//Logger::logVector(movement,"Move",2,"\033[34m");


glm::vec3 moveVec = glm::normalize(movement);
actualSpeed = glm::length(movement);
actualSpeedAbs = glm::length(movement);

if (glm::dot(movement, vecFront) > 0) {
actualSpeed = actualSpeedAbs;
}
else {
actualSpeed = -actualSpeedAbs;
}

if (actualSpeed > 0) {
if (actualSpeedAbs > brakevalue) {
movement -= moveVec * brakevalue;
}
else if (actualSpeedAbs > 0) {
movement = glm::vec3(0);
}
}

void Character::moveForward() {
Expand Down Expand Up @@ -176,6 +161,30 @@ void Character::rollRight()

}

void Character::brake()
{
float brakevalue = backwardSidewayAccel;

//Logger::logVector(movement,"Move",2,"\033[34m");

glm::vec3 moveVec = glm::normalize(movement);
actualSpeedAbs = glm::length(movement);

if (glm::dot(movement, vecFront) > 0) {
actualSpeed = actualSpeedAbs;
}
else {
actualSpeed = -actualSpeedAbs;
}

if (actualSpeedAbs > brakevalue) {
movement -= moveVec * brakevalue;
}
else if (actualSpeedAbs > 0) {
movement = glm::vec3(0);
}
}

void Character::jump()
{
if (canJump)
Expand Down Expand Up @@ -251,6 +260,8 @@ std::shared_ptr<Bullet> Character::shoot()
{
float maxScatteringAngle = 1;

//Logger::info2("shoot");

float scatteringAngleX = std::rand() * maxScatteringAngle / RAND_MAX;
scatteringAngleX -= maxScatteringAngle / 2;

Expand All @@ -259,20 +270,23 @@ std::shared_ptr<Bullet> Character::shoot()

std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> notshotDuration = now - lastTimeShot;
if (notshotDuration.count() > 1000)
if (notshotDuration.count() > shootInterval)
{
glm::vec3 bulletCreationPosition = position + glm::vec3(0, 2, 0) +glm::vec3(1, 1, 1) * getVecFront();
glm::vec3 bulletCreationRotation = glm::vec3(0, rotation.y - 90 + scatteringAngleY, -rotation.x - 90 + scatteringAngleX);
glm::vec3 bulletCreationDirection = getVecFront();
glm::vec3 bulletCreationPosition = position + glm::vec3(7) * vecFront;
//glm::vec3 bulletCreationRotation = glm::vec3(0, rotation.y - 90 + scatteringAngleY, -rotation.x - 90 + scatteringAngleX);
glm::vec3 bulletCreationDirection = vecFront;

glm::quat bulletRotationquat = this->getRotationQuat();


//rotate direction around Y
bulletCreationDirection = glm::rotate(bulletCreationDirection, glm::radians(scatteringAngleY), glm::vec3(0, 1, 0));

//rotate direction around z
bulletCreationDirection = glm::rotate(bulletCreationDirection, glm::radians(scatteringAngleX), glm::vec3(0, 0, 1));


std::shared_ptr<Bullet> newBullet = std::make_shared<Bullet>(shader, bulletCreationPosition, bulletCreationRotation, bulletCreationDirection);
std::shared_ptr<Bullet> newBullet = std::make_shared<Bullet>(shader, bulletCreationPosition, bulletRotationquat, bulletCreationDirection);
newBullet->setNumber(Game::objects.size());

Game::bullets.push_back(newBullet);
Expand Down
9 changes: 6 additions & 3 deletions Game/Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class Character : public Object

void rollRight();

void brake();

void jump();

void activateJumping();
Expand Down Expand Up @@ -88,17 +90,18 @@ class Character : public Object


float32 maxSpeed = 60;
float32 forwardAccel = 0.1f; //per second
float32 backwardSidewayAccel = 0.04f; //per second
float32 forwardAccel = 0.05f; //per second
float32 backwardSidewayAccel = 0.03f; //per second
float32 upwardAccel = 0.04f; //per second
float32 rollSpeed = 0.1f;

float32 actualSpeed = 0;
float32 actualSpeedAbs = 0;

const float32 heigth = 4;

std::chrono::system_clock::time_point lastTimeShot = std::chrono::system_clock::now() - std::chrono::hours(1);

float32 shootInterval = 100;


int team = 0;
Expand Down
11 changes: 9 additions & 2 deletions Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ void Game::processInput()
AudioManager::updateAudioListener();
}
}

}
if (isKeyPressed(PlayerAction::rollRight))
{
Expand All @@ -500,7 +499,15 @@ void Game::processInput()
AudioManager::updateAudioListener();
}
}

}
if (isKeyPressed(PlayerAction::brake))
{
if (gameState == GameState::GAME_ACTIVE || gameState == GameState::GAME_GAME_OVER)
{
if (players.size() > 0) {
players[0]->brake();
}
}
}
if (isKeyPressed(PlayerAction::jump))
{
Expand Down
4 changes: 3 additions & 1 deletion Game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ enum class PlayerAction {
moveDown,
rollLeft,
rollRight,
brake,
jump,
sprint,
crouch,
Expand Down Expand Up @@ -136,7 +137,8 @@ std::unordered_map<SDL_Keycode, PlayerAction> const keybindings =
{SDLK_LCTRL, PlayerAction::moveDown},
{SDLK_q, PlayerAction::rollLeft},
{SDLK_e, PlayerAction::rollRight},
{SDLK_x, PlayerAction::jump},
{SDLK_x, PlayerAction::brake},
{SDLK_y, PlayerAction::jump},
{SDLK_LSHIFT, PlayerAction::sprint},
{SDLK_c, PlayerAction::crouch},
{SDLK_f, PlayerAction::interact},
Expand Down
5 changes: 5 additions & 0 deletions Game/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ void Logger::info(std::string message)
std::cout << getTime() << message << std::endl;
}

void Logger::info2(std::string message)
{
std::cout << "\033[34m" << getTime() << message << "\033[0m" << std::endl;
}

void Logger::warn(std::string message)
{
std::cout << "\033[33m" << getTime() << message << "\033[0m" << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions Game/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ static class Logger

static void info(std::string message);

static void info2(std::string message);

static void warn(std::string message);

static void error(std::string message);
Expand Down
8 changes: 8 additions & 0 deletions Game/NPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ void NPC::followNavPoints()
vecFront = targetPosition - myPosition;
vecFront = glm::normalize(vecFront);

vecUp = glm::vec3(1, 0, 0);



float pitch = glm::degrees(asin(-vecFront.y));
float yaw = glm::degrees(atan2(vecFront.x, vecFront.z));
setRotation(glm::vec3(0, yaw, 0));

glm::quat newRotQuat = glm::quat(glm::vec3(pitch,yaw,0));

setRotationQuat(newRotQuat);

float distance = glm::length(targetPosition - myPosition);

if (distance < 0.5)
Expand Down
4 changes: 2 additions & 2 deletions Game/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ class Object

virtual void reactToCollision(CollisionResult collisionResult);

void calculationBeforeFrame();
virtual void calculationBeforeFrame();

void calculationAfterFrame();
virtual void calculationAfterFrame();

glm::vec3 calculateDimensions();

Expand Down
Loading

0 comments on commit 1b180b3

Please sign in to comment.