Khaotic Engine Reborn
Loading...
Searching...
No Matches
physics Class Reference
Inheritance diagram for physics:
object

Public Member Functions

 physics (const physics &)
 
XMVECTOR GetGravity () const
 
void SetGravity (XMVECTOR gravity)
 
void ApplyGravity (object *, float)
 
void AddForce (object *, XMVECTOR)
 
bool IsColliding (object *, object *)
 
bool CubesOverlap (object *, object *)
 
bool SpheresOverlap (object *, object *)
 
bool SphereCubeOverlap (object *, object *)
 
- Public Member Functions inherited from object
 object (application_class &app)
 
 object (const object &)=delete
 
objectoperator= (const object &)=delete
 
bool Initialize (ID3D11Device *device, ID3D11DeviceContext *deviceContext, char *modelFilename, TextureContainer &texturesContainer)
 
void SetScaleMatrix (XMMATRIX scaleMatrix)
 
void SetRotateMatrix (XMMATRIX rotateMatrix)
 
void SetTranslateMatrix (XMMATRIX translateMatrix)
 
void SetSRMatrix (XMMATRIX srMatrix)
 
void SetWorldMatrix (XMMATRIX worldMatrix)
 
void SetPosition (XMVECTOR position)
 
void SetRotation (XMVECTOR rotation)
 
void SetScale (XMVECTOR scale)
 
XMMATRIX GetScaleMatrix () const
 
XMMATRIX GetRotateMatrix () const
 
XMMATRIX GetTranslateMatrix () const
 
XMMATRIX GetSRMatrix () const
 
XMMATRIX GetWorldMatrix () const
 
XMVECTOR GetPosition ()
 
XMVECTOR GetRotation ()
 
XMVECTOR GetScale ()
 
void SetVelocity (XMVECTOR)
 
void AddVelocity (float deltaTime)
 
XMVECTOR GetVelocity () const
 
void SetAcceleration (XMVECTOR)
 
XMVECTOR GetAcceleration () const
 
void SetMass (float)
 
float GetMass () const
 
void SetGrounded (bool)
 
bool IsGrounded () const
 
bool IsPhysicsEnabled () const
 
void SetPhysicsEnabled (bool state)
 
void SetVisible (bool state)
 
bool IsVisible () const
 
void UpdateWorldMatrix ()
 
void UpdateSRMatrix ()
 
void UpdateScaleMatrix ()
 
void UpdateRotateMatrix ()
 
void UpdateTranslateMatrix ()
 
void UpdatePosition (float deltaTime)
 
void Update ()
 
std::string GetName ()
 
void SetName (std::string name)
 
int SetId (int id)
 
int GetId () const
 
void SetType (ObjectType type)
 
ObjectType GetType () const
 
ShaderType GetActiveShader () const
 
void SetActiveShader (ShaderType activeShader)
 
float GetBoundingRadius () const
 
void SetBoundingRadius (float radius)
 
void SetModelPath (std::wstring &path)
 
std::wstring & GetModelPath ()
 
ShaderType StringToShaderType (const std::string &shaderType)
 
std::string ShaderTypeToString (ShaderType shaderType)
 
ObjectType StringToObjectType (const std::string &objectType)
 
std::string ObjectTypeToString (ObjectType objectType)
 
void LaunchObject ()
 
bool LoadTexturesFromPath (std::vector< std::wstring > &texturePaths, TextureContainer &texturesContainer, d_3d_class *m_Direct3D)
 
bool SetupInstancing (ID3D11Device *device, const std::vector< XMMATRIX > &instanceTransforms)
 
void EnableInstancing (bool enabled)
 
void SetInstanceCount (int count)
 
bool IsInstancingEnabled () const
 
int GetInstanceCount () const
 
ID3D11Buffer * GetInstanceBuffer () const
 
void SetAlpha (float alpha)
 
float GetAlpha () const
 
void SetInitialStretch (float initialStretch)
 
float GetInitialStretch () const
 
void SetSpringConstant (float springConstant)
 
float GetSpringConstant () const
 
bool IsGravityEnabled () const
 
void SetGravityEnabled (bool state)
 
std::shared_ptr< model_classget_model () const
 
void SetModel (std::shared_ptr< model_class > model)
 

Additional Inherited Members

- Public Attributes inherited from object
bool m_demoSpinning = false
 
XMVECTOR m_previousPosition
 
XMVECTOR m_velocity
 
int m_id
 
bool m_gravityEnabled = true
 

Detailed Description

Definition at line 7 of file physics.h.

Constructor & Destructor Documentation

◆ physics() [1/2]

physics::physics ( )

Definition at line 4 of file physics.cpp.

5{
6 m_gravity = XMVectorSet(0.0f, -9.81f, 0.0f, 0.0f); // initialize the gravity vector
7}

◆ physics() [2/2]

physics::physics ( const physics & other)
explicit

Definition at line 9 of file physics.cpp.

10{
11 m_gravity = other.m_gravity; // Copy the gravity value
12}

◆ ~physics()

physics::~physics ( )

Definition at line 14 of file physics.cpp.

15{
16}

Member Function Documentation

◆ AddForce()

void physics::AddForce ( object * object,
XMVECTOR force )

Definition at line 63 of file physics.cpp.

64{
65 if (object == nullptr) // Verify if the object is not null
66 {
67 return;
68 }
69
70 // Get the mass of the object
71 float mass = object->GetMass();
72
73 // Calculate the acceleration caused by the force
74 XMVECTOR acceleration = force / mass;
75
76 // Add the acceleration to the object's current acceleration
77 object->SetAcceleration(object->GetAcceleration() + acceleration);
78}

◆ ApplyGravity()

void physics::ApplyGravity ( object * object,
float dragValue )

Definition at line 31 of file physics.cpp.

32{
33 if (this == nullptr || object == nullptr) // Verify if 'this' and 'object' are not null
34 {
35 return;
36 }
37
38 if (!object->IsGrounded()) // Verify if the object is grounded
39 {
40 // Calculate the acceleration caused by gravity
41 XMVECTOR gravityAcceleration = m_gravity / object->GetMass();
42
43 // Add the gravity acceleration to the object's current acceleration
44 object->SetAcceleration(object->GetAcceleration() + gravityAcceleration);
45
46 // Calculate the acceleration caused by drag
47 XMVECTOR dragAcceleration = -object->GetVelocity() * dragValue / object->GetMass();
48
49 // Add the drag acceleration to the object's current acceleration
50 object->SetAcceleration(object->GetAcceleration() + dragAcceleration);
51
52 // Get the object velocity
53 XMVECTOR velocity = object->GetVelocity();
54
55 // Update the velocity with the object's acceleration
56 velocity += object->GetAcceleration();
57
58 // Set the new velocity
59 object->SetVelocity(velocity);
60 }
61}

◆ CubesOverlap()

bool physics::CubesOverlap ( object * cube1,
object * cube2 )

Definition at line 119 of file physics.cpp.

120{
121 XMVECTOR position1 = cube1->GetPosition();
122 XMVECTOR position2 = cube2->GetPosition();
123
124 XMVECTOR scale1 = cube1->GetScale();
125 XMVECTOR scale2 = cube2->GetScale();
126
127 XMVECTOR min1 = position1 - scale1;
128 XMVECTOR max1 = position1 + scale1;
129 XMVECTOR min2 = position2 - scale2;
130 XMVECTOR max2 = position2 + scale2;
131
132 return (min1.m128_f32[0] <= max2.m128_f32[0] && max1.m128_f32[0] >= min2.m128_f32[0] &&
133 min1.m128_f32[1] <= max2.m128_f32[1] && max1.m128_f32[1] >= min2.m128_f32[1] &&
134 min1.m128_f32[2] <= max2.m128_f32[2] && max1.m128_f32[2] >= min2.m128_f32[2]);
135}

◆ GetGravity()

XMVECTOR physics::GetGravity ( ) const

Definition at line 19 of file physics.cpp.

20{
21 return m_gravity;
22}

◆ IsColliding()

bool physics::IsColliding ( object * object1,
object * object2 )

Definition at line 80 of file physics.cpp.

81{
82 ObjectType type1 = object1->GetType();
83 ObjectType type2 = object2->GetType();
84
85 if (type1 == ObjectType::Unknown || type2 == ObjectType::Unknown)
86 {
87 return false;
88 }
89
90 if (type1 == ObjectType::Sphere && type2 == ObjectType::Sphere)
91 {
92 return SpheresOverlap(object1, object2);
93 }
94 if ((type1 == ObjectType::Cube && type2 == ObjectType::Sphere) ||
95 (type1 == ObjectType::Sphere && type2 == ObjectType::Cube))
96 {
97 if (type1 == ObjectType::Cube)
98 {
99 return SphereCubeOverlap(object1, object2);
100 }
101 else if (type1 == ObjectType::Sphere)
102 {
103 return SphereCubeOverlap(object2, object1);
104 }
105 }
106 else
107 {
108 return CubesOverlap(object1, object2);
109 }
110
111 return false;
112}

◆ SetGravity()

void physics::SetGravity ( XMVECTOR gravity)

Definition at line 25 of file physics.cpp.

26{
27 m_gravity = gravity;
28}

◆ SphereCubeOverlap()

bool physics::SphereCubeOverlap ( object * cube,
object * sphere )

Definition at line 157 of file physics.cpp.

158{
159 XMVECTOR position1 = cube->GetPosition();
160 XMVECTOR position2 = sphere->GetPosition();
161
162 XMVECTOR scale1 = cube->GetScale();
163 XMVECTOR scale2 = XMVectorScale(sphere->GetScale(), 0.5f);
164
165 XMVECTOR min1 = position1 - scale1;
166 XMVECTOR max1 = position1 + scale1;
167
168 // Get box closest point to sphere center by clamping
169 float x = max(min1.m128_f32[0], min(position2.m128_f32[0], max1.m128_f32[0]));
170 float y = max(min1.m128_f32[1], min(position2.m128_f32[1], max1.m128_f32[1]));
171 float z = max(min1.m128_f32[2], min(position2.m128_f32[2], max1.m128_f32[2]));
172
173 // This is the same as SpheresOverlap
174 float distance = sqrt(
175 (x - position2.m128_f32[0]) * (x - position2.m128_f32[0]) +
176 (y - position2.m128_f32[1]) * (y - position2.m128_f32[1]) +
177 (z - position2.m128_f32[2]) * (z - position2.m128_f32[2])
178 );
179
180 float radius = XMVectorGetX(XMVector3Length(scale2));
181
182 return distance < radius;
183}

◆ SpheresOverlap()

bool physics::SpheresOverlap ( object * sphere1,
object * sphere2 )

Definition at line 137 of file physics.cpp.

138{
139 XMVECTOR position1 = sphere1->GetPosition();
140 XMVECTOR position2 = sphere2->GetPosition();
141
142 XMVECTOR scale1 = sphere1->GetScale() / 2;
143 XMVECTOR scale2 = sphere2->GetScale() / 2;
144
145 float distance = sqrt(
146 (position1.m128_f32[0] - position2.m128_f32[0]) * (position1.m128_f32[0] - position2.m128_f32[0]) +
147 (position1.m128_f32[1] - position2.m128_f32[1]) * (position1.m128_f32[1] - position2.m128_f32[1]) +
148 (position1.m128_f32[2] - position2.m128_f32[2]) * (position1.m128_f32[2] - position2.m128_f32[2])
149 );
150
151 float radius1 = XMVectorGetX(XMVector3Length(scale1));
152 float radius2 = XMVectorGetX(XMVector3Length(scale2));
153
154 return distance < radius1 + radius2;
155}

The documentation for this class was generated from the following files: