Khaotic Engine Reborn
Loading...
Searching...
No Matches
object.cpp
1#include "object.h"
2
3#include <comdef.h>
4
5#include "application_class.h"
6#include "d_3d_class.h"
7
8object::object(application_class& app) : m_Application(app)// initialize the reference here
9{
10 m_scaleMatrix = XMMatrixIdentity();
11 m_rotateMatrix = XMMatrixIdentity();
12 m_translateMatrix = XMMatrixIdentity();
13 m_srMatrix = XMMatrixIdentity();
14 m_worldMatrix = XMMatrixIdentity();
15 m_previousPosition = XMVectorZero();
16 m_velocity = XMVectorZero();
17 m_acceleration = XMVectorZero();
18 m_mass = NULL;
19 m_isGrounded = false;
20 m_id = NULL;
21 m_boundingRadius = 1.0f;
22}
23
24object::object()
25 : m_Application(*static_cast<application_class*>(nullptr)) // Remplacer suivant votre logique
26{
27 m_scaleMatrix = XMMatrixIdentity();
28 m_rotateMatrix = XMMatrixIdentity();
29 m_translateMatrix = XMMatrixIdentity();
30 m_srMatrix = XMMatrixIdentity();
31 m_worldMatrix = XMMatrixIdentity();
32 m_previousPosition = XMVectorZero();
33 m_velocity = XMVectorZero();
34 m_acceleration = XMVectorZero();
35 m_mass = NULL;
36 m_isGrounded = false;
37 m_id = NULL;
38 m_boundingRadius = 1.0f;
39}
40
41object::~object()
42{
43}
44
45bool object::Initialize(
46 ID3D11Device* device,
47 ID3D11DeviceContext* deviceContext,
48 char* modelFilename,
49 TextureContainer& texturesContainer
50 )
51{
52
53 std::string filename(modelFilename);
54 auto it = g_model_cache.find(filename);
55 if (it != g_model_cache.end())
56 {
57 m_model_ = it->second;
58 }
59 else
60 {
61 auto new_model = std::make_shared<model_class>();
62 if (!new_model->Initialize(device, deviceContext, modelFilename, texturesContainer))
63 {
64 return false;
65 }
66 g_model_cache[filename] = new_model;
67 m_model_ = new_model;
68 }
69
70 return true;
71}
72
73void object::SetScaleMatrix(XMMATRIX scaleMatrix)
74{
75 m_scaleMatrix = scaleMatrix;
76}
77
78void object::SetRotateMatrix(XMMATRIX rotateMatrix)
79{
80 m_rotateMatrix = rotateMatrix;
81}
82
83void object::SetTranslateMatrix(XMMATRIX translateMatrix)
84{
85 m_translateMatrix = translateMatrix;
86}
87
88void object::SetSRMatrix(XMMATRIX srMatrix)
89{
90 m_srMatrix = srMatrix;
91}
92
93void object::SetWorldMatrix(XMMATRIX worldMatrix)
94{
95 m_worldMatrix = worldMatrix;
96}
97
98XMMATRIX object::GetScaleMatrix() const
99{
100 return m_scaleMatrix;
101}
102
103XMMATRIX object::GetRotateMatrix() const
104{
105 return m_rotateMatrix;
106}
107
108XMMATRIX object::GetTranslateMatrix() const
109{
110 return m_translateMatrix;
111}
112
113XMMATRIX object::GetSRMatrix() const
114{
115 return m_srMatrix;
116}
117
118XMMATRIX object::GetWorldMatrix() const
119{
120 return m_worldMatrix;
121}
122
123XMVECTOR object::GetPosition()
124{
125 XMFLOAT4X4 matrix;
126 XMStoreFloat4x4(&matrix, m_translateMatrix);
127 return XMVectorSet(matrix._41, matrix._42, matrix._43, 0.0f);
128}
129
130XMVECTOR object::GetRotation()
131{
132 XMFLOAT4X4 matrix;
133 XMStoreFloat4x4(&matrix, m_rotateMatrix);
134 float rotationX = atan2f(matrix._32, matrix._33);
135 float rotationY = atan2f(-matrix._31, sqrtf(matrix._32 * matrix._32 + matrix._33 * matrix._33));
136 float rotationZ = atan2f(matrix._21, matrix._11);
137 return XMVectorSet(rotationX, rotationY, rotationZ, 0.0f);
138}
139
140XMVECTOR object::GetScale()
141{
142 XMFLOAT4X4 matrix;
143 XMStoreFloat4x4(&matrix, m_scaleMatrix);
144
145 // Utiliser des vecteurs pour les lignes de la matrice
146 XMVECTOR row1 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._11));
147 XMVECTOR row2 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._21));
148 XMVECTOR row3 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._31));
149
150 // Calculer les longueurs des vecteurs
151 XMVECTOR scale = XMVectorSet(
152 XMVectorGetX(XMVector3Length(row1)),
153 XMVectorGetX(XMVector3Length(row2)),
154 XMVectorGetX(XMVector3Length(row3)),
155 0.0f
156 );
157
158 return scale;
159}
160
161
162
163void object::SetPosition(XMVECTOR position)
164{
165 XMFLOAT4X4 matrix;
166 XMStoreFloat4x4(&matrix, m_translateMatrix);
167 matrix._41 = XMVectorGetX(position);
168 matrix._42 = XMVectorGetY(position);
169 matrix._43 = XMVectorGetZ(position);
170 m_translateMatrix = XMLoadFloat4x4(&matrix);
171}
172
173void object::SetRotation(XMVECTOR rotation)
174{
175 XMFLOAT4X4 matrix;
176 XMStoreFloat4x4(&matrix, m_rotateMatrix);
177 XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(XMVectorGetX(rotation), XMVectorGetY(rotation), XMVectorGetZ(rotation));
178 m_rotateMatrix = rotationMatrix;
179}
180
181void object::SetScale(XMVECTOR scale)
182{
183 XMFLOAT4X4 matrix;
184 XMStoreFloat4x4(&matrix, m_scaleMatrix);
185 matrix._11 = XMVectorGetX(scale);
186 matrix._22 = XMVectorGetY(scale);
187 matrix._33 = XMVectorGetZ(scale);
188 m_scaleMatrix = XMLoadFloat4x4(&matrix);
189}
190
191void object::UpdateWorldMatrix()
192{
193 m_worldMatrix = m_scaleMatrix * m_rotateMatrix * m_translateMatrix;
194}
195
196void object::UpdateSRMatrix()
197{
198 m_srMatrix = m_scaleMatrix * m_rotateMatrix;
199}
200
201void object::UpdateTranslateMatrix()
202{
203 m_translateMatrix = XMMatrixTranslationFromVector(GetPosition());
204}
205
206void object::UpdateRotateMatrix()
207{
208 m_rotateMatrix = XMMatrixRotationRollPitchYawFromVector(GetRotation());
209}
210
211void object::UpdateScaleMatrix()
212{
213 m_scaleMatrix = XMMatrixScalingFromVector(GetScale());
214}
215
216void object::Update()
217{
218 UpdateWorldMatrix();
219 UpdateSRMatrix();
220 UpdateTranslateMatrix();
221 UpdateRotateMatrix();
222 UpdateScaleMatrix();
223}
224
225std::string object::GetName()
226{
227 return m_name;
228}
229
230void object::SetName(std::string name)
231{
232 m_name = name;
233}
234
235void object::SetVelocity(XMVECTOR velocity)
236{
237 m_velocity = velocity;
238}
239
240void object::AddVelocity(float deltaTime)
241{
242 m_velocity += m_acceleration * deltaTime;
243}
244
245XMVECTOR object::GetVelocity() const
246{
247 return m_velocity;
248}
249
250void object::SetAcceleration(XMVECTOR acceleration)
251{
252 m_acceleration = acceleration;
253}
254
255XMVECTOR object::GetAcceleration() const
256{
257 return m_acceleration;
258}
259
260void object::SetMass(float mass)
261{
262 m_mass = mass;
263}
264
265float object::GetMass() const
266{
267 return m_mass;
268}
269
270void object::SetGrounded(bool isGrounded)
271{
272 m_isGrounded = isGrounded;
273}
274
275bool object::IsGrounded() const
276{
277 return m_isGrounded;
278}
279
280int object::SetId(int id)
281{
282 return m_id = id;
283}
284
285int object::GetId() const
286{
287 return m_id;
288}
289
290bool object::IsPhysicsEnabled() const
291{
292 return m_isPhysicsEnabled;
293}
294
295void object::SetPhysicsEnabled(bool state)
296{
297 m_isPhysicsEnabled = state;
298}
299
300float object::GetBoundingRadius() const
301{
302 return m_boundingRadius;
303}
304
305void object::UpdatePosition(float deltaTime)
306{
307 XMVECTOR position = GetPosition();
308 position = position + GetVelocity() * deltaTime;
309 SetPosition(position);
310}
311
312void object::SetType(ObjectType type)
313{
314 m_type = type;
315}
316
317std::string object::ObjectTypeToString(ObjectType type) {
318 switch (type) {
319 case ObjectType::Cube: return "Cube";
320 case ObjectType::Sphere: return "Sphere";
321 // Ajoutez d'autres cas si nécessaire
322 default: return "Unknown";
323 }
324}
325
326ObjectType object::StringToObjectType(const std::string& str) {
327 if (str == "Cube") return ObjectType::Cube;
328 if (str == "Sphere") return ObjectType::Sphere;
329 // Add other cases as needed
330 return ObjectType::Unknown;
331}
332
333std::string object::ShaderTypeToString(ShaderType type) {
334 switch (type) {
335 case ShaderType::ALPHA_MAPPING: return "ALPHA_MAPPING";
336 case ShaderType::CEL_SHADING: return "CEL_SHADING";
337 case ShaderType::NORMAL_MAPPING: return "NORMAL_MAPPING";
338 case ShaderType::SPECULAR_MAPPING: return "SPECULAR_MAPPING";
339 case ShaderType::TEXTURE: return "TEXTURE";
340 case ShaderType::LIGHTING: return "LIGHTING";
341 case ShaderType::SUNLIGHT: return "SUNLIGHT";
342 // Ajoutez d'autres cas si nécessaire
343 default: return "Unknown";
344 }
345}
346
347ShaderType object::StringToShaderType(const std::string& str) {
348 if (str == "ALPHA_MAPPING") return ShaderType::ALPHA_MAPPING;
349 if (str == "CEL_SHADING") return ShaderType::CEL_SHADING;
350 if (str == "NORMAL_MAPPING") return ShaderType::NORMAL_MAPPING;
351 if (str == "SPECULAR_MAPPING") return ShaderType::SPECULAR_MAPPING;
352 if (str == "TEXTURE") return ShaderType::TEXTURE;
353 if (str == "LIGHTING") return ShaderType::LIGHTING;
354 if (str == "SUNLIGHT") return ShaderType::SUNLIGHT;
355 // Add other cases as needed
356 return ShaderType::TEXTURE;
357}
358
359void object::LaunchObject()
360{
361 // Constants
362 const float gravity = -9.81f;
363
364 // Convert alpha from degrees to radians if needed
365 float alphaRadians = m_alpha * (XM_PI / 180.0f);
366
367 // Scale factors to make the physics simulation more visible
368 float scaleFactor = 200.0f; // Adjust this based on your world scale
369
370 // Calculate initial velocity magnitude using the same formula as the Python code
371 // v_eject = l1 * sqrt(k/m) * sqrt(1 - (m*g*sin(alpha)/(k*l1))^2)
372 float velocityMagnitude = m_initialStretch * sqrtf(m_springConstant / m_mass) *
373 sqrtf(1.0f - powf((m_mass * gravity * sinf(alphaRadians) / (m_springConstant * m_initialStretch)), 2.0f));
374
375 // Apply scale factor
376 velocityMagnitude *= scaleFactor;
377
378 // Calculate velocity components
379 XMVECTOR velocity = XMVectorSet(
380 velocityMagnitude * cosf(alphaRadians), // vx = v0 * cos(alpha)
381 velocityMagnitude * sinf(alphaRadians), // vy = v0 * sin(alpha)
382 0.0f, // z-component (0 for 2D trajectory)
383 0.0f
384 );
385
386 // Apply velocity to object
387 SetVelocity(velocity);
388
389 // Enable physics for the object to handle the trajectory
390 SetPhysicsEnabled(true);
391
392 // Reset grounded state
393 SetGrounded(false);
394
395 // Debug output
396 char buffer[256];
397 sprintf_s(buffer, "Launch velocity: %f m/s at angle %f degrees", XMVectorGetX(XMVector3Length(velocity)), m_alpha);
398 OutputDebugStringA(buffer);
399}
400
401bool object::LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer,
402 d_3d_class* m_Direct3D)
403{
404
405 HRESULT result;
406
407 int i = 0;
408 TextureType type;
409 for (const auto& texturePath : texturePaths)
410 {
411 ID3D11ShaderResourceView* texture = nullptr;
412 result = DirectX::CreateWICTextureFromFile(m_Direct3D->get_device(), m_Direct3D->get_device_context(), texturePath.c_str(), nullptr, &texture);
413 if (FAILED(result))
414 {
415
416 // Utiliser _com_error pour obtenir des informations détaillées sur l'erreur
417 _com_error err(result);
418 LPCTSTR errMsg = err.ErrorMessage();
419
420 //convertie errMessage en std::wstring
421 std::wstring ws(errMsg);
422 std::string str(ws.begin(), ws.end());
423
424 Logger::Get().Log("Failed to load texture: " + std::string(texturePath.begin(), texturePath.end()) +
425 "\nError: " + std::to_string(result) +
426 "\nDescription: " + str,
427 __FILE__, __LINE__, Logger::LogLevel::Error);
428 return false; // Assurez-vous de retourner false ou de gérer l'erreur de manière appropriée
429 }
430 texturesContainer.AssignTexture(texturesContainer, texture,texturePath , i);
431 i++;
432
433 }
434
435 return true;
436}
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158
ID3D11Device * get_device()
Gets the Direct3D device.
ID3D11DeviceContext * get_device_context()
Gets the Direct3D device context.