-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameobject.cpp
More file actions
179 lines (155 loc) · 4.91 KB
/
Gameobject.cpp
File metadata and controls
179 lines (155 loc) · 4.91 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "GameObject.h"
#include <Windows.h>
game_object::game_object(HWND hwnd) : m_initializationFailed(false),
m_hwnd(hwnd) , m_velocity(nullptr),
m_rigidBody(nullptr), m_collider(nullptr), m_radius(0)
{
}
game_object::~game_object()
{
if (m_hwnd)
{
//Don't delete as it only stores a reference
m_hwnd = nullptr;
}
if (m_collider)
{
delete m_collider;
m_collider = nullptr;
}
if (m_rigidBody)
{
delete m_rigidBody;
m_rigidBody = nullptr;
}
if (m_object)
{
delete m_object;
m_object = nullptr;
}
if (m_transform)
{
delete m_transform;
m_transform = nullptr;
}
}
void game_object::add_velocity_component()
{
m_velocity = new velocity_component();
}
void game_object::add_velocity_component(const XMFLOAT3& velocity)
{
m_velocity = new velocity_component(velocity);
}
void game_object::add_velocity_component(const float x, const float y, const float z)
{
m_velocity = new velocity_component(x, y, z);
}
//Inertia tensor is based off the model type, if the model isn't initialised before the rigidbody then it will try the colliders type, else it throws an error stating this
void game_object::add_rigid_body_component(const bool useGravity, const float mass, const float drag, const float angularDrag, const XMFLOAT3 position, const XMFLOAT4 rotation, const XMFLOAT3 velocity, const XMFLOAT3 angularVelocity) {
auto inertiaTensor = XMFLOAT3X3();
auto scale = XMVECTOR();
if (m_object)
{
{
scale = m_transform->get_scale();
}
const auto scaleX = XMVectorGetX(scale);
const auto scaleY = XMVectorGetY(scale);
const auto scaleZ = XMVectorGetZ(scale);
switch (m_object->get_object_type())
{
case object::object_type::sphere:
inertiaTensor = XMFLOAT3X3(0.4f * (mass * (scaleX * scaleX)), 0.0f, 0.0f,
0.0f, 0.4f * (mass * (scaleX * scaleX)), 0.0f,
0.0f, 0.0f, 0.4f * (mass * (scaleX * scaleX)));
break;
case object::object_type::cube:
inertiaTensor = XMFLOAT3X3(1.0f / 12.0f * (mass * (scaleY * scaleY + scaleZ * scaleZ)), 0.0f, 0.0f,
0.0f, 1.0f / 12.0f * (mass * (scaleX * scaleX + scaleZ * scaleZ)), 0.0f,
0.0f, 0.0f, 1.0f / 12.0f * (mass * (scaleX * scaleX + scaleY * scaleY)));
break;
case object::object_type::plane:
//No inertia tensor needed
break;
case object::object_type::peg:
inertiaTensor = XMFLOAT3X3(((1.0f / 12.0f) * (mass * (scaleY * scaleY))) + 0.25f * (mass * (scaleX * scaleX)), 0.0f, 0.0f,
0.0f, 0.5f * (mass * (scaleX * scaleX)), 0.0f,
0.0f, 0.0f, ((1.0f / 12.0f) * (mass * (scaleY * scaleY))) + 0.25f * (mass * (scaleX * scaleX)));
break;
default:
m_initializationFailed = true;
return;
}
}
else
{
MessageBox(m_hwnd, "You need to define a model component before the rigidbody!", "Error: Missing Component", MB_OK);
m_initializationFailed = true;
return;
}
m_rigidBody = new rigid_body_component(useGravity, mass, drag, angularDrag, position, rotation, velocity, angularVelocity, inertiaTensor);
}
void game_object::add_collider_component(const collider_component::collider_type colliderType) {
switch (colliderType)
{
case collider_component::collider_type::sphere:
m_collider = new sphere_collider();
break;
case collider_component::collider_type::aabb_cube:
m_collider = new aabb_cube_collider();
break;
case collider_component::collider_type::obb_cube:
m_collider = new obb_cube_collider();
break;
case collider_component::collider_type::plane:
m_collider = new plane_collider();
break;
case collider_component::collider_type::peg:
m_collider = new cylinder_collider();
break;
default:
m_initializationFailed = true;
break;
}
}
void game_object::set_plane_collider_data(const XMFLOAT3& centre, const XMFLOAT3& pointOne, const XMFLOAT3& pointTwo, const float offset) const
{
auto* planeCollider = dynamic_cast<plane_collider*>(m_collider);
if (!planeCollider)
{
MessageBox(m_hwnd, "You tried setting data to a collider type that's isn't a plane collider!", "Error: Collider Conversion", MB_OK);
return;
}
planeCollider->set_normal(centre, pointOne, pointTwo);
planeCollider->set_offset(offset);
planeCollider = nullptr;
}
void game_object::add_transform_component(const XMFLOAT3& position, const XMFLOAT4& rotation, const XMFLOAT3& scale)
{
m_transform = new transform_component(position, rotation, scale);
}
void game_object::add_transform_component(const XMFLOAT3& scale)
{
m_transform = new transform_component(scale);
}
bool game_object::render() {
auto position = m_rigidBody->get_position();
auto rotation = m_rigidBody->get_rotation();
auto scale = get_scale();
if (XMVectorGetZ(rotation) > 0.f)
{
rotation = XMVectorSet(0, 0, 1, 0);
}
else if (XMVectorGetZ(rotation) < 0.f)
{
rotation = XMVectorSet(0, 0, -1, 0);
}
m_object->render(position, rotation, scale/*,color*/);
auto result = true;
return result;
}
void game_object::add_object_type(const object::object_type obj)
{
m_object = new object(obj);
}