Commit 627e8549d90219c2949fb1d30bf34809b8e3cb60

Authored by Olav Kjartan Larseng
1 parent eb47d94a

collision implemented, sinus wave on enemy implemented with respect to delta time

SpaceShooterV4/Bullet.cpp
1 1 #include "Bullet.h"
2 2 #include "include/glm/glm.hpp"
3 3 #include "include/glm/gtc/matrix_transform.hpp"
  4 +#include "Collision.h"
  5 +#include "Enemy.h"
  6 +
4 7  
5 8 Bullet::Bullet(const glm::mat4& startMatrix)
6 9 {
7   - this->setMatrix(startMatrix);
  10 + setMatrix(startMatrix);
8 11 }
9 12  
10 13 Bullet::Bullet(const glm::mat4& startMatrix, Trajectory t)
11 14 {
12   - this->setMatrix(startMatrix);
  15 + setMatrix(startMatrix);
13 16 _trajectory = t;
14   -
15 17 }
16 18  
17   -Bullet::Bullet(const glm::mat4& startMatrix, Trajectory t, float trajectoryRate)
  19 +Bullet::Bullet(const glm::mat4& startMatrix, Trajectory t, float speed)
18 20 {
19   - this->setMatrix(startMatrix);
  21 + setMatrix(startMatrix);
20 22 _trajectory = t;
21   - _trajectoryRate = trajectoryRate;
  23 + _speed = speed;
22 24 }
23 25  
24 26 Bullet::~Bullet()
25 27 {
  28 + _collisionObjects.clear();
26 29 }
27 30  
28 31 void Bullet::privateInit()
... ... @@ -45,23 +48,63 @@ void Bullet::privateRender()
45 48  
46 49 void Bullet::privateUpdate()
47 50 {
48   -
49   - if (_trajectory == Trajectory::FORWARD)
  51 + if (_trajectory == Trajectory::FORWARD)
50 52 {
51   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -_trajectoryRate * _deltaTime));
  53 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -_speed * _deltaTime));
52 54 if (glm::vec3(matrix_[3]).z < -3000)
53 55 _markDelete = true;
54 56 }
55   - else if (_trajectory == Trajectory::BACKWARD)
  57 + else if (_trajectory == Trajectory::BACKWARD)
56 58 {
57   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _trajectoryRate * _deltaTime));
  59 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _speed * _deltaTime));
58 60 if (glm::vec3(matrix_[3]).z > 100)
59 61 _markDelete = true;
60 62 }
  63 +
  64 + collide();
61 65  
62 66 }
63 67  
  68 +void Bullet::collide()
  69 +{
  70 + if (!_collisionObjects.empty())
  71 + {
  72 + for (std::vector<std::shared_ptr<SceneObject> >::iterator it = _collisionObjects.begin();
  73 + it != _collisionObjects.end();)
  74 + {
  75 + const auto collObj = (*it);
  76 + if (Collision::detectCollisionSphereVsSphere(glm::vec3(getMatrix()[3]), glm::vec3(collObj->getMatrix()[3])))
  77 + {
  78 + std::shared_ptr<Enemy> enemy = std::dynamic_pointer_cast<Enemy>(collObj);
  79 + if (enemy)
  80 + {
  81 + enemy->hitLife(_bulletPower);
  82 + }
  83 + it = _collisionObjects.erase(it);
  84 + _markDelete = true;
  85 + break;
  86 + }
  87 + else {
  88 + it++;
  89 + }
  90 + }
  91 + }
  92 +}
  93 +
64 94 bool Bullet::markedForDeletion()
65 95 {
66 96 return _markDelete;
67 97 }
  98 +
  99 +void Bullet::addCollisionWith(std::shared_ptr<SceneObject> obj)
  100 +{
  101 + _collisionObjects.push_back(obj);
  102 +}
  103 +
  104 +void Bullet::addCollisionWith(std::vector<std::shared_ptr<SceneObject>> cObj)
  105 +{
  106 + for (auto i : cObj)
  107 + {
  108 + addCollisionWith(i);
  109 + }
  110 +}
... ...
SpaceShooterV4/Bullet.h
... ... @@ -18,16 +18,21 @@ class Bullet : public SceneObject
18 18 ~Bullet();
19 19  
20 20 private:
21   - float _speed;
22   - float _trajectoryRate = 0.05f;
  21 + float _speed = 100.0f;
23 22 Trajectory _trajectory = Trajectory::FORWARD;
24 23 bool _markDelete = false;
  24 + std::vector<std::shared_ptr<SceneObject>> _collisionObjects;
25 25  
26   - protected:
27 26 void privateInit();
28 27 void privateRender();
29 28 void privateUpdate();
  29 + void collide();
30 30 bool markedForDeletion();
31 31  
  32 + public:
  33 + void addCollisionWith(std::shared_ptr<SceneObject> obj);
  34 + void addCollisionWith(std::vector<std::shared_ptr<SceneObject>> cObj);
  35 + float _bulletPower = 1.0f;
  36 +
32 37 };
33 38  
... ...
SpaceShooterV4/Collision.cpp
... ... @@ -11,16 +11,21 @@ Collision::~Collision()
11 11 {
12 12 }
13 13  
14   -void Collision::detectCollisionSphereVsSphere(glm::vec3 a, glm::vec3 b)
  14 +bool Collision::detectCollisionSphereVsSphere(glm::vec3 a, glm::vec3 b)
15 15 {
16   - float a_radius = 1.0f;
17   - float b_radius = 1.5f;
18   - a = glm::vec3(2.0, 0.0, 0.0);
19   - b = glm::vec3(5.0, 0.0, 0.0);
  16 + float a_radius = 10.0f;
  17 + float b_radius = 10.0f;
  18 + //a = glm::vec3(2.0, 0.0, 0.0);
  19 + //b = glm::vec3(5.0, 0.0, 0.0);
20 20 float dist = glm::length(a - b);
21 21  
22   - if (dist < a_radius + b_radius)
23   - std::cout << "Collision" << std::endl;
24   - else
25   - std::cout << "No Collision" << std::endl;;
  22 + if (dist < a_radius + b_radius)
  23 + {
  24 + //std::cout << "Collision" << std::endl;
  25 + return true;
  26 + }
  27 + else {
  28 + //std::cout << "No Collision" << std::endl;;
  29 + return false;
  30 + }
26 31 }
... ...
SpaceShooterV4/Collision.h
1 1 #pragma once
2 2 #include "include\glm\glm.hpp"
  3 +#include "include\SceneObject.hpp"
3 4  
4 5 class Collision
5 6 {
6 7 enum class Type {
7 8 SphereVsSphere
8 9 };
  10 +
9 11 public:
10 12 Collision();
11 13 ~Collision();
12 14  
13   - void detectCollisionSphereVsSphere(glm::vec3 a, glm::vec3 b);
  15 + static bool detectCollisionSphereVsSphere(glm::vec3 a, glm::vec3 b);
14 16 };
15 17  
  18 +
... ...
SpaceShooterV4/Enemy.cpp
... ... @@ -11,7 +11,7 @@ Enemy::Enemy()
11 11 {
12 12 }
13 13  
14   -Enemy::Enemy(Trajectory t)
  14 +Enemy::Enemy(const Trajectory& t)
15 15 {
16 16 _trajectory = t;
17 17 }
... ... @@ -66,9 +66,11 @@ void Enemy::privateRender()
66 66  
67 67 void Enemy::privateUpdate()
68 68 {
  69 + //if (_counter > 360)
  70 + // _counter = 0;
69 71  
70 72 _counter += (_inc_speed);
71   -
  73 +
72 74 if (_trajectory == Trajectory::FORWARD)
73 75 {
74 76 matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _speed * _deltaTime));
... ... @@ -76,11 +78,11 @@ void Enemy::privateUpdate()
76 78 else if (_trajectory == Trajectory::SINUS)
77 79 {
78 80 // A * sin(2 * pi * f * t + phase)
79   -
  81 + float _period = 2.0f;
  82 + float _phase = glm::sin(_counter / _period);
  83 +
80 84 matrix_ = glm::translate(matrix_, glm::vec3(
81   -
82   - glm::sin(_counter * 2 * (M_PI / 60)),
83   -
  85 + 0.125 * glm::sin(2 * M_PI * _period * _deltaTime + _phase),
84 86 0.0f,
85 87 0.006125f));
86 88 }
... ... @@ -91,3 +93,14 @@ bool Enemy::markedForDeletion()
91 93 {
92 94 return _markDelete;
93 95 }
  96 +
  97 +void Enemy::hitLife(float hitPoint)
  98 +{
  99 + if (_life <= 0)
  100 + {
  101 + _markDelete = true;
  102 + }
  103 + else {
  104 + _life -= hitPoint;
  105 + }
  106 +}
... ...
SpaceShooterV4/Enemy.h
... ... @@ -4,6 +4,7 @@
4 4 #include <GL/glu.h>
5 5 #include "include/SceneObject.hpp"
6 6  
  7 +
7 8 class Enemy : public SceneObject
8 9 {
9 10 public:
... ... @@ -12,29 +13,33 @@ class Enemy : public SceneObject
12 13 SINUS
13 14 };
14 15  
  16 + enum class Type {
  17 + NORMAL
  18 + };
  19 +
15 20 public:
16 21 Enemy();
17   - explicit Enemy(Trajectory t);
  22 + explicit Enemy(const Trajectory& t);
18 23 ~Enemy();
19 24  
20   - protected:
21 25 void privateInit();
22 26 void privateRender();
23 27 void privateUpdate();
24 28 bool markedForDeletion();
25 29  
  30 + void hitLife(float hitPoint);
  31 +
26 32 private:
27   - float _speed = 100.0f;
28   - float _life;
  33 + float _speed = 2.0f;
29 34 float _armor;
30 35  
31 36 bool _markDelete = false;
32   - double _counter = 0;
33   - double _inc_speed = 1;
  37 + float _counter = 0;
  38 + float _inc_speed = 0.01;
34 39 GLuint _texture;
35   - //GLUquadricObj *quadObj = gluNewQuadric();
36 40 Trajectory _trajectory = Trajectory::FORWARD;
37   -
38   -
39   -};
  41 +
  42 + public:
  43 + float _life = 2;
40 44  
  45 +};
41 46 \ No newline at end of file
... ...
SpaceShooterV4/SpaceShooterV4.vcxproj
1   -<?xml version="1.0" encoding="utf-8"?>
  1 +<?xml version="1.0" encoding="utf-8"?>
2 2 <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3 3 <ItemGroup Label="ProjectConfigurations">
4 4 <ProjectConfiguration Include="Debug|Win32">
... ...
SpaceShooterV4/SpaceShooterV4.vcxproj.filters
... ... @@ -56,6 +56,9 @@
56 56 <ClInclude Include="Collision.h">
57 57 <Filter>Header Files</Filter>
58 58 </ClInclude>
  59 + <ClInclude Include="Data.h">
  60 + <Filter>Header Files</Filter>
  61 + </ClInclude>
59 62 </ItemGroup>
60 63 <ItemGroup>
61 64 <ClCompile Include="app\main.cpp">
... ...
SpaceShooterV4/include/Camera.hpp
... ... @@ -27,7 +27,7 @@ class Camera : public SceneObject
27 27 bool markedForDeletion();
28 28  
29 29 private:
30   - float _speed = 25.0f;
  30 + float _speed = 400.0f;
31 31 bool _markDelete = false;
32 32 };
33 33  
... ...
SpaceShooterV4/include/SceneObject.hpp
... ... @@ -4,6 +4,7 @@
4 4 #include "glm/glm.hpp"
5 5 #include <memory>
6 6  
  7 +
7 8 // The SceneObject class is the parent of all objects in the scene graph.
8 9 // To make an object to use in the scene graph, inherit this class and
9 10 // override the methods private_init, private_update and private_render.
... ...
SpaceShooterV4/include/SpaceShip.hpp
... ... @@ -11,18 +11,17 @@ class SpaceShip : public SceneObject
11 11 {
12 12 public:
13 13 SpaceShip();
14   - SpaceShip(float speed);
15   - SpaceShip(float speed, float life);
16   - SpaceShip(float speed, float life, float armor);
  14 + explicit SpaceShip(const float& speed);
  15 + explicit SpaceShip(const float& speed, const float& life);
  16 + explicit SpaceShip(const float& speed, const float& life, const float& armor);
17 17 ~SpaceShip();
18 18  
19   - protected:
  19 + private:
20 20 void privateInit();
21 21 void privateRender();
22 22 void privateUpdate();
23 23 bool markedForDeletion();
24   -
25   - private:
  24 +
26 25 float _speed = 300.0f;
27 26 float _life = 3;
28 27 float _armor = 1;
... ...
SpaceShooterV4/src/Camera.cpp
... ... @@ -38,26 +38,26 @@ bool Camera::markedForDeletion()
38 38  
39 39 void Camera::moveRight()
40 40 {
41   - matrix_ = glm::translate(matrix_, glm::vec3(-_speed, 0.0f, 0.0f));
  41 + matrix_ = glm::translate(matrix_, glm::vec3(-_speed * _deltaTime, 0.0f, 0.0f));
42 42 }
43 43 void Camera::moveLeft()
44 44 {
45   - matrix_ = glm::translate(matrix_, glm::vec3(_speed, 0.0f, 0.0f));
  45 + matrix_ = glm::translate(matrix_, glm::vec3(_speed * _deltaTime, 0.0f, 0.0f));
46 46 }
47 47 void Camera::moveUp()
48 48 {
49   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, -_speed, 0.0f));
  49 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, -_speed * _deltaTime, 0.0f));
50 50 }
51 51 void Camera::moveDown()
52 52 {
53   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, _speed, 0.0f));
  53 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, _speed * _deltaTime, 0.0f));
54 54 }
55 55 void Camera::moveForward()
56 56 {
57   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _speed));
  57 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _speed * _deltaTime));
58 58 }
59 59 void Camera::moveBackward()
60 60 {
61   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -_speed));
  61 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -_speed * _deltaTime));
62 62 }
63 63  
... ...
SpaceShooterV4/src/GameManager.cpp
... ... @@ -15,7 +15,7 @@ void GameManager::privateInit()
15 15  
16 16 // Adding the camera to the scene
17 17 cam_.reset(new Camera());
18   - // this->addSubObject(cam_);
  18 + this->addSubObject(cam_);
19 19 // matrix_.translate(0.0f, 0.0f, -99.0f);
20 20  
21 21 bf_.reset(new BattleField());
... ... @@ -31,7 +31,6 @@ void GameManager::privateInit()
31 31 _enemy.reset(new Enemy(Enemy::Trajectory::SINUS));
32 32 this->addSubObject(_enemy);
33 33  
34   -
35 34 //_spotlight.reset(new Spotlight(glm::vec3{0,0,200}, 60, 80));
36 35 //this->addSubObject(_spotlight);
37 36 }
... ... @@ -67,6 +66,7 @@ std::shared_ptr&lt;SpaceShip&gt; GameManager::getSpaceShip()
67 66 void GameManager::fireFromSpaceship()
68 67 {
69 68 _bullet.reset(new Bullet(spaceship_->getMatrix(), Bullet::Trajectory::FORWARD, 350.00f));
  69 + _bullet->addCollisionWith(_enemy);
70 70 this->addSubObject(_bullet);
71 71 }
72 72  
... ...
SpaceShooterV4/src/SpaceShip.cpp
... ... @@ -10,18 +10,18 @@ SpaceShip::SpaceShip()
10 10  
11 11 }
12 12  
13   -SpaceShip::SpaceShip(float speed)
  13 +SpaceShip::SpaceShip(const float& speed)
14 14 {
15 15 _speed = speed;
16 16 }
17 17  
18   -SpaceShip::SpaceShip(float speed, float life)
  18 +SpaceShip::SpaceShip(const float& speed, const float& life)
19 19 {
20 20 _speed = speed;
21 21 _life = life;
22 22 }
23 23  
24   -SpaceShip::SpaceShip(float speed, float life, float armor)
  24 +SpaceShip::SpaceShip(const float& speed, const float& life, const float& armor)
25 25 {
26 26 _speed = speed;
27 27 _life = life;
... ...
SpaceShooterV4/x64/Debug/BattleField.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Bullet.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Camera.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Collision.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Enemy.obj
No preview for this file type
SpaceShooterV4/x64/Debug/GameManager.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SceneObject.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SpaceShip.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SpaceShooterV4.log
1 1  Enemy.cpp
  2 +d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\enemy.h(38): warning C4305: 'initializing': truncation from 'double' to 'float'
2 3 SOIL.lib(SOIL.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance
3 4 LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification
4 5 LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog differ
... ...
SpaceShooterV4/x64/Debug/Spotlight.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Text.obj
No preview for this file type
SpaceShooterV4/x64/Debug/main.obj
No preview for this file type
SpaceShooterV4/x64/Debug/vc141.idb
No preview for this file type
SpaceShooterV4/x64/Debug/vc141.pdb
No preview for this file type
x64/Debug/SpaceShooterV4.exe
No preview for this file type
x64/Debug/SpaceShooterV4.pdb
No preview for this file type