-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameObject.cpp
126 lines (101 loc) · 3.23 KB
/
GameObject.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "GameObject.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include "config.h"
int GameObject::nextID = 0;
GameObject::GameObject(Mesh* p_mesh, Shape p_shapeType,
glm::vec3 p_translation, glm::quat p_orientation, glm::vec3 p_scale,
glm::vec4 p_color, GLuint p_texID, float p_mass, bool p_collidable,
Path p_motion)
: mesh(p_mesh), color(p_color), trueColor(p_color), textureID(p_texID),
mass(p_mass)
{
ID = nextID++;
motion = p_motion;
SetShape(p_translation, p_orientation, p_scale, p_shapeType);
rigidbody->setFriction(RB_FRICTION);
SetCollidable(p_collidable);
if(mass != 0)
rigidbody->setActivationState(DISABLE_DEACTIVATION);
////http://bulletphysics.org/mediawiki-1.5.8/index.php/Constraints
//btGeneric6DofSpring2Constraint constraint =
// new btGeneric6DofSpring2Constraint(*rigidbody,
// btTransform::getIdentity());
//constraint->setLinearLowerLimit(btVector3(0., 0., 0.));
//constraint->setLinearUpperLimit(btVector3(0., 0., 0.));
//constraint->enableSpring(0, true);
//constraint->setStiffness(0, 100);
//constraint->getTranslationalLimitMotor()->m_enableMotor[0] = true;
//constraint->getTranslationalLimitMotor()->m_targetVelocity[0] = -5.0f
//constraint->setEquilibriumPoint(0, 0);
//Physics::dynamicsWorld->addConstraint(constraint);
//rigidbody->setUserPointer(this);
rigidbody->setUserPointer(this);
}
GameObject::~GameObject()
{
delete rigidbody;
}
glm::mat4 GameObject::GetModelMatrix()
{
return glm::translate(glm::mat4(1), GetTranslation())
* glm::toMat4(GetOrientation())
* glm::scale(glm::mat4(1), GetScale());
}
void GameObject::Render()
{
if(drawable)
mesh->Draw();
}
void GameObject::Update(float deltaTime)
{
if (!motion.Points.empty())
{
if (motion.Enabled)
motion.Step();
rigidbody->applyCentralForce(convert(
Physics::InverseDynamics(GetTranslation(),
motion.GetPosition(),
convert(rigidbody->getLinearVelocity()),
mass, deltaTime)));
}
}
void GameObject::SetShape(Shape p_shapeType)
{
glm::vec3 translation = GetTranslation();
glm::quat orientation = GetOrientation();
glm::vec3 scale = GetScale();
//TODO: if not in dynamics world
Physics::dynamicsWorld->removeRigidBody(rigidbody);
delete rigidbody;
SetShape(translation, orientation, scale,
p_shapeType);
}
void GameObject::SetShape(glm::vec3 translation, glm::quat orientation,
glm::vec3 scale, Shape p_shapeType)
{
btCollisionShape* shape;
if (p_shapeType == Shape::Box)
shape = new btBoxShape(btVector3(1, 1, 1));
else
shape = new btCylinderShape(btVector3(1, 1, 1));
shape->setLocalScaling(convert(scale));
btDefaultMotionState* transform =
new btDefaultMotionState(btTransform(
btQuaternion(convert(orientation)),
btVector3(convert(translation))));
btVector3 inertia;
shape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo
groundRigidBodyCI(mass, transform, shape, inertia);
rigidbody = new btRigidBody(groundRigidBodyCI);
if (motion.Points.size() > 0)
rigidbody->setMassProps(mass, btVector3(0, 0, 0));
else
rigidbody->setMassProps(mass, inertia);
rigidbody->setUserPointer(this);
Physics::dynamicsWorld->addRigidBody(rigidbody);
shapeType = p_shapeType;
}