# Khaotic Engine Reborn: physics Class Reference

Loading...
Searching...
No Matches
physics Class Reference
Inheritance diagram for physics:

## 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
object & | operator= (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_class > | get_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.

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

## physics() [2/2]

physics::physics | ( | const physics & | other | ) |

explicit

Definition at line 9 of file physics.cpp.

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

## ~physics()

physics::~physics | ( |  | ) |

Definition at line 14 of file physics.cpp.

{
}

## Member Function Documentation

## AddForce()

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

Definition at line 63 of file physics.cpp.

{
if (object == nullptr) // Verify if the object is not null
{
return;
}
// Get the mass of the object
float mass = object->GetMass();
// Calculate the acceleration caused by the force
XMVECTOR acceleration = force / mass;
// Add the acceleration to the object's current acceleration
object->SetAcceleration(object->GetAcceleration() + acceleration);
}
object
Definition object.h:30

## ApplyGravity()

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

Definition at line 31 of file physics.cpp.

{
if (this == nullptr || object == nullptr) // Verify if 'this' and 'object' are not null
{
return;
}
if (!object->IsGrounded()) // Verify if the object is grounded
{
// Calculate the acceleration caused by gravity
XMVECTOR gravityAcceleration = m_gravity / object->GetMass();
// Add the gravity acceleration to the object's current acceleration
object->SetAcceleration(object->GetAcceleration() + gravityAcceleration);
// Calculate the acceleration caused by drag
XMVECTOR dragAcceleration = -object->GetVelocity() * dragValue / object->GetMass();
// Add the drag acceleration to the object's current acceleration
object->SetAcceleration(object->GetAcceleration() + dragAcceleration);
// Get the object velocity
XMVECTOR velocity = object->GetVelocity();
// Update the velocity with the object's acceleration
velocity += object->GetAcceleration();
// Set the new velocity
object->SetVelocity(velocity);
}
}

## CubesOverlap()

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

Definition at line 119 of file physics.cpp.

{
XMVECTOR position1 = cube1->GetPosition();
XMVECTOR position2 = cube2->GetPosition();
XMVECTOR scale1 = cube1->GetScale();
XMVECTOR scale2 = cube2->GetScale();
XMVECTOR min1 = position1 - scale1;
XMVECTOR max1 = position1 + scale1;
XMVECTOR min2 = position2 - scale2;
XMVECTOR max2 = position2 + scale2;
return (min1.m128_f32[0] <= max2.m128_f32[0] && max1.m128_f32[0] >= min2.m128_f32[0] &&
min1.m128_f32[1] <= max2.m128_f32[1] && max1.m128_f32[1] >= min2.m128_f32[1] &&
min1.m128_f32[2] <= max2.m128_f32[2] && max1.m128_f32[2] >= min2.m128_f32[2]);
}

## GetGravity()

XMVECTOR physics::GetGravity | ( |  | ) | const

Definition at line 19 of file physics.cpp.

{
return m_gravity;
}

## IsColliding()

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

Definition at line 80 of file physics.cpp.

{
ObjectType type1 = object1->GetType();
ObjectType type2 = object2->GetType();
if (type1 == ObjectType::Unknown || type2 == ObjectType::Unknown)
{
return false;
}
if (type1 == ObjectType::Sphere && type2 == ObjectType::Sphere)
{
return SpheresOverlap(object1, object2);
}
if ((type1 == ObjectType::Cube && type2 == ObjectType::Sphere) ||
(type1 == ObjectType::Sphere && type2 == ObjectType::Cube))
{
if (type1 == ObjectType::Cube)
{
return SphereCubeOverlap(object1, object2);
}
else if (type1 == ObjectType::Sphere)
{
return SphereCubeOverlap(object2, object1);
}
}
else
{
return CubesOverlap(object1, object2);
}
return false;
}

## SetGravity()

void physics::SetGravity | ( | XMVECTOR | gravity | ) |

Definition at line 25 of file physics.cpp.

{
m_gravity = gravity;
}

## SphereCubeOverlap()

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

Definition at line 157 of file physics.cpp.

{
XMVECTOR position1 = cube->GetPosition();
XMVECTOR position2 = sphere->GetPosition();
XMVECTOR scale1 = cube->GetScale();
XMVECTOR scale2 = XMVectorScale(sphere->GetScale(), 0.5f);
XMVECTOR min1 = position1 - scale1;
XMVECTOR max1 = position1 + scale1;
// Get box closest point to sphere center by clamping
float x = max(min1.m128_f32[0], min(position2.m128_f32[0], max1.m128_f32[0]));
float y = max(min1.m128_f32[1], min(position2.m128_f32[1], max1.m128_f32[1]));
float z = max(min1.m128_f32[2], min(position2.m128_f32[2], max1.m128_f32[2]));
// This is the same as SpheresOverlap
float distance = sqrt(
(x - position2.m128_f32[0]) * (x - position2.m128_f32[0]) +
(y - position2.m128_f32[1]) * (y - position2.m128_f32[1]) +
(z - position2.m128_f32[2]) * (z - position2.m128_f32[2])
);
float radius = XMVectorGetX(XMVector3Length(scale2));
return distance < radius;
}

## SpheresOverlap()

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

Definition at line 137 of file physics.cpp.

{
XMVECTOR position1 = sphere1->GetPosition();
XMVECTOR position2 = sphere2->GetPosition();
XMVECTOR scale1 = sphere1->GetScale() / 2;
XMVECTOR scale2 = sphere2->GetScale() / 2;
float distance = sqrt(
(position1.m128_f32[0] - position2.m128_f32[0]) * (position1.m128_f32[0] - position2.m128_f32[0]) +
(position1.m128_f32[1] - position2.m128_f32[1]) * (position1.m128_f32[1] - position2.m128_f32[1]) +
(position1.m128_f32[2] - position2.m128_f32[2]) * (position1.m128_f32[2] - position2.m128_f32[2])
);
float radius1 = XMVectorGetX(XMVector3Length(scale1));
float radius2 = XMVectorGetX(XMVector3Length(scale2));
return distance < radius1 + radius2;
}

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

- enginecustom/src/inc/system/physics.h
- enginecustom/src/src/system/physics.cpp