-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysicsEngine.cpp
102 lines (69 loc) · 2.03 KB
/
PhysicsEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "src/PhysicsEngine.h"
namespace physics{
/***********************************
* PHYSICS ENTITY IMPLEMENTATION
*************************************/
PhysicsEntity::PhysicsEntity()
: _position(Vector3f(0.0f, 0.0f, 0.0f)),_velocity(Vector3f(0.0f, 0.0f, 0.0f)),_force(Vector3f(0.0f, 0.0f, 0.0f))
{
}
PhysicsEntity::PhysicsEntity(Vector3f pos)
: _position(pos), _velocity(Vector3f(0.0f, 0.0f, 0.0f)),_force(Vector3f(0.0f, 0.0f, 0.0f))
{
}
void PhysicsEntity::update()
{
//x = x0 + velocity * time
_position = _position+ _velocity * 0.5f;
}
/***********************************
* STATIC ENTITY IMPLEMENTATION
*************************************/
StaticEntity::StaticEntity()
{
}
StaticEntity::StaticEntity(float radius, Vector3f position, Vector3f force)
{
_radius = radius;
_position = position;
_velocity = Vector3f(0,0,0);
_force = force;
}
/***********************************
* PHYSICS ENGINE IMPLEMENTATION
*************************************/
PhysicsEngine::PhysicsEngine(){}
PhysicsEngine& PhysicsEngine::get()
{
static PhysicsEngine instance;
return instance;
}
bool PhysicsEngine::spheresphere(Vector3<float>& c1,float _radius1,Vector3<float>& c2,float _radius2)
{
Vector3<float> temp = c1;
if(temp.x > 10)
temp.x / 10;
float dist=pointdistacesquare(temp,c2);
if(std::abs(dist)<=(_radius1+_radius2)*(_radius1+_radius2))
{
return 1;
}
return 0;
}
float PhysicsEngine::pointdistacesquare(Vector3<float> p1,Vector3<float> p2)
{
Vector3<float> vec(p2.x-p1.x,p2.y-p1.y,p2.z-p1.z);
return (vec.x*vec.x+vec.y*vec.y+vec.z*vec.z);
}
void PhysicsEngine::resolveCollision(PhysicsEntity* a, PhysicsEntity* b)
{ //!! b can't pass a !!
//Resolve Collision
//Seeking Knock back effect
//Assume B has triple the mass of a and it will be the enemy
a->_velocity = (b->_velocity + b->_velocity)*2.0f;
b->_velocity = -(b->_velocity + b->_velocity)*5.0f;
a->update();
b->update();
}
void PhysicsEngine::resolveCollisionWall(PhysicsEntity * b){}
}//namespace physics