feat: acceleration, mass, mouvement cube
implementation de l'acceleration, de la masse (je sais pas si c'est une bonne facon de faire mais on va dire oui) mouvements avec un code tres sale dans application class frame
This commit is contained in:
parent
56ed2d1d5f
commit
13729b62fc
@ -628,7 +628,7 @@ void ApplicationClass::Shutdown()
|
|||||||
bool ApplicationClass::Frame(InputClass* Input)
|
bool ApplicationClass::Frame(InputClass* Input)
|
||||||
{
|
{
|
||||||
int mouseX, mouseY, currentMouseX, currentMouseY;
|
int mouseX, mouseY, currentMouseX, currentMouseY;
|
||||||
bool result, leftMouseDown, rightMouseDown, keyDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE;
|
bool result, leftMouseDown, rightMouseDown, keyLeft, keyRight, keyUp, keyDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE;
|
||||||
float rotationY, rotationX, positionX, positionY, positionZ;
|
float rotationY, rotationX, positionX, positionY, positionZ;
|
||||||
|
|
||||||
float frameTime;
|
float frameTime;
|
||||||
@ -673,11 +673,11 @@ bool ApplicationClass::Frame(InputClass* Input)
|
|||||||
m_Position->SetFrameTime(m_Timer->GetTime());
|
m_Position->SetFrameTime(m_Timer->GetTime());
|
||||||
|
|
||||||
// Check if the left or right arrow key has been pressed, if so rotate the camera accordingly.
|
// Check if the left or right arrow key has been pressed, if so rotate the camera accordingly.
|
||||||
keyDown = Input->IsLeftArrowPressed();
|
//keyDown = Input->IsLeftArrowPressed();
|
||||||
m_Position->TurnLeft(keyDown);
|
//m_Position->TurnLeft(keyDown);
|
||||||
|
|
||||||
keyDown = Input->IsRightArrowPressed();
|
//keyDown = Input->IsRightArrowPressed();
|
||||||
m_Position->TurnRight(keyDown);
|
//m_Position->TurnRight(keyDown);
|
||||||
|
|
||||||
m_Position->TurnMouse(deltaX, deltaY, rightMouseDown);
|
m_Position->TurnMouse(deltaX, deltaY, rightMouseDown);
|
||||||
|
|
||||||
@ -728,27 +728,66 @@ bool ApplicationClass::Frame(InputClass* Input)
|
|||||||
//// Update the z position variable each frame.
|
//// Update the z position variable each frame.
|
||||||
//z -= 0.0174532925f * 0.2f;
|
//z -= 0.0174532925f * 0.2f;
|
||||||
|
|
||||||
|
keyLeft = Input->IsLeftArrowPressed();
|
||||||
|
keyRight = Input->IsRightArrowPressed();
|
||||||
|
keyUp = Input->IsUpArrowPressed();
|
||||||
|
keyDown = Input->IsDownArrowPressed();
|
||||||
|
|
||||||
for (auto object : m_object)
|
for (auto object : m_object)
|
||||||
{
|
{
|
||||||
if (object != nullptr) // Check if the object is not null
|
if (object != nullptr) // Check if the object is not null
|
||||||
{
|
{
|
||||||
|
// Reset acceleration for the new frame
|
||||||
|
object->SetAcceleration(XMVectorZero());
|
||||||
|
|
||||||
|
// Apply forces
|
||||||
|
m_Physics->ApplyGravity(object, frameTime);
|
||||||
|
m_Physics->ApplyDrag(object, 1.0f, frameTime);
|
||||||
|
|
||||||
|
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
|
||||||
|
|
||||||
|
if (keyLeft)
|
||||||
|
{
|
||||||
|
forceX = -10.0f;
|
||||||
|
}
|
||||||
|
if (keyRight)
|
||||||
|
{
|
||||||
|
forceX = 10.0f;
|
||||||
|
}
|
||||||
|
if (keyUp)
|
||||||
|
{
|
||||||
|
forceY = 40.0f;
|
||||||
|
}
|
||||||
|
if (keyDown)
|
||||||
|
{
|
||||||
|
forceY = -40.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
|
||||||
|
m_Physics->ApplyForce(object, force);
|
||||||
|
|
||||||
|
// Update velocity based on acceleration
|
||||||
|
XMVECTOR velocity = object->GetVelocity();
|
||||||
|
velocity = velocity + object->GetAcceleration() * frameTime;
|
||||||
|
object->SetVelocity(velocity);
|
||||||
|
|
||||||
|
// Update position based on velocity
|
||||||
|
XMVECTOR position = object->GetPosition();
|
||||||
|
position = position + velocity * frameTime;
|
||||||
|
object->SetPosition(position);
|
||||||
|
|
||||||
|
// Check if the object has fallen below the ground
|
||||||
if (XMVectorGetY(object->GetPosition()) < -10.0f)
|
if (XMVectorGetY(object->GetPosition()) < -10.0f)
|
||||||
{
|
{
|
||||||
XMVECTOR currentPosition = object->GetPosition(); // Obtain the current position of the object
|
XMVECTOR currentPosition = object->GetPosition(); // Obtain the current position of the object
|
||||||
object->SetPosition(XMVectorSetY(currentPosition, 20.0f)); // Define the new position of the object
|
object->SetPosition(XMVectorSetY(currentPosition, 20.0f)); // Define the new position of the object
|
||||||
}
|
}
|
||||||
m_Physics->ApplyGravity(object, frameTime);
|
|
||||||
m_Physics->ApplyDrag(object, 1.0f, frameTime);
|
|
||||||
// Update object position based on its velocity
|
|
||||||
XMVECTOR position = object->GetPosition();
|
|
||||||
XMVECTOR velocity = object->GetVelocity();
|
|
||||||
position = position + velocity * frameTime;
|
|
||||||
object->SetPosition(position);
|
|
||||||
object->m_previousPosition = object->GetPosition();
|
object->m_previousPosition = object->GetPosition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Render the scene to a render texture.
|
// Render the scene to a render texture.
|
||||||
@ -1266,6 +1305,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath)
|
|||||||
|
|
||||||
Object* newObject = new Object();
|
Object* newObject = new Object();
|
||||||
newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3);
|
newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3);
|
||||||
|
newObject->SetMass(1.0f);
|
||||||
|
|
||||||
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f));
|
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f));
|
||||||
newObject->SetName(filename);
|
newObject->SetName(filename);
|
||||||
|
@ -3,11 +3,11 @@ Pos=60,60
|
|||||||
Size=400,400
|
Size=400,400
|
||||||
|
|
||||||
[Window][Khaotic Engine]
|
[Window][Khaotic Engine]
|
||||||
Pos=587,47
|
Pos=596,31
|
||||||
Size=694,210
|
Size=694,210
|
||||||
|
|
||||||
[Window][Objects]
|
[Window][Objects]
|
||||||
Pos=91,47
|
Pos=87,45
|
||||||
Size=492,353
|
Size=492,353
|
||||||
|
|
||||||
[Window][Terrain]
|
[Window][Terrain]
|
||||||
|
@ -276,6 +276,27 @@ bool InputClass::IsRightArrowPressed()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool InputClass::IsUpArrowPressed()
|
||||||
|
{
|
||||||
|
if (m_keyboardState[DIK_UP] & 0x80)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool InputClass::IsDownArrowPressed()
|
||||||
|
{
|
||||||
|
if (m_keyboardState[DIK_DOWN] & 0x80)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////
|
///////////////////////////////////////////////////
|
||||||
// Les touches correspondent aux claviers QWERTY //
|
// Les touches correspondent aux claviers QWERTY //
|
||||||
///////////////////////////////////////////////////
|
///////////////////////////////////////////////////
|
||||||
|
@ -39,6 +39,8 @@ public:
|
|||||||
void KeyUp(unsigned int);
|
void KeyUp(unsigned int);
|
||||||
bool IsLeftArrowPressed();
|
bool IsLeftArrowPressed();
|
||||||
bool IsRightArrowPressed();
|
bool IsRightArrowPressed();
|
||||||
|
bool IsUpArrowPressed();
|
||||||
|
bool IsDownArrowPressed();
|
||||||
bool IsAPressed();
|
bool IsAPressed();
|
||||||
bool IsDPressed();
|
bool IsDPressed();
|
||||||
bool IsWPressed();
|
bool IsWPressed();
|
||||||
|
@ -9,6 +9,8 @@ Object::Object() : ModelClass()
|
|||||||
m_worldMatrix = XMMatrixIdentity();
|
m_worldMatrix = XMMatrixIdentity();
|
||||||
m_previousPosition = XMVectorZero();
|
m_previousPosition = XMVectorZero();
|
||||||
m_velocity = XMVectorZero();
|
m_velocity = XMVectorZero();
|
||||||
|
m_acceleration = XMVectorZero();
|
||||||
|
m_mass = 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object::~Object()
|
Object::~Object()
|
||||||
@ -173,4 +175,24 @@ void Object::SetVelocity(XMVECTOR velocity)
|
|||||||
XMVECTOR Object::GetVelocity()
|
XMVECTOR Object::GetVelocity()
|
||||||
{
|
{
|
||||||
return m_velocity;
|
return m_velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Object::SetAcceleration(XMVECTOR acceleration)
|
||||||
|
{
|
||||||
|
m_acceleration = acceleration;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMVECTOR Object::GetAcceleration()
|
||||||
|
{
|
||||||
|
return m_acceleration;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Object::SetMass(float mass)
|
||||||
|
{
|
||||||
|
m_mass = mass;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Object::GetMass()
|
||||||
|
{
|
||||||
|
return m_mass;
|
||||||
}
|
}
|
@ -29,6 +29,10 @@ public:
|
|||||||
|
|
||||||
void SetVelocity(XMVECTOR);
|
void SetVelocity(XMVECTOR);
|
||||||
XMVECTOR GetVelocity();
|
XMVECTOR GetVelocity();
|
||||||
|
void SetAcceleration(XMVECTOR);
|
||||||
|
XMVECTOR GetAcceleration();
|
||||||
|
void SetMass(float);
|
||||||
|
float GetMass();
|
||||||
|
|
||||||
void UpdateWorldMatrix();
|
void UpdateWorldMatrix();
|
||||||
void UpdateSRMatrix();
|
void UpdateSRMatrix();
|
||||||
@ -53,5 +57,8 @@ private:
|
|||||||
XMMATRIX m_srMatrix;
|
XMMATRIX m_srMatrix;
|
||||||
XMMATRIX m_worldMatrix;
|
XMMATRIX m_worldMatrix;
|
||||||
|
|
||||||
|
XMVECTOR m_acceleration;
|
||||||
|
float m_mass;
|
||||||
|
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
};
|
};
|
||||||
|
@ -29,34 +29,63 @@ void Physics::SetGravity(XMVECTOR gravity)
|
|||||||
// Apply gravity to an object
|
// Apply gravity to an object
|
||||||
void Physics::ApplyGravity(Object* object, float frameTime)
|
void Physics::ApplyGravity(Object* object, float frameTime)
|
||||||
{
|
{
|
||||||
if (object == nullptr) // Verify if the object is not null
|
if (object == nullptr) // Verify if the object is not null
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the object velocity
|
// Calculate the acceleration caused by gravity
|
||||||
XMVECTOR velocity = object->GetVelocity();
|
XMVECTOR gravityAcceleration = m_gravity / object->GetMass();
|
||||||
|
|
||||||
// Update the velocity with gravity
|
// Add the gravity acceleration to the object's current acceleration
|
||||||
velocity += m_gravity * frameTime;
|
object->SetAcceleration(object->GetAcceleration() + gravityAcceleration);
|
||||||
|
|
||||||
// Set the new velocity
|
// Get the object velocity
|
||||||
object->SetVelocity(velocity);
|
XMVECTOR velocity = object->GetVelocity();
|
||||||
|
|
||||||
|
// Update the velocity with the object's acceleration
|
||||||
|
velocity += object->GetAcceleration() * frameTime;
|
||||||
|
|
||||||
|
// Set the new velocity
|
||||||
|
object->SetVelocity(velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Physics::ApplyDrag(Object* object, float dragValue, float frameTime)
|
void Physics::ApplyDrag(Object* object, float dragValue, float frameTime)
|
||||||
|
{
|
||||||
|
if (object == nullptr) // Verify if the object is not null
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 velocity of the object
|
||||||
|
XMVECTOR velocity = object->GetVelocity();
|
||||||
|
|
||||||
|
// Update the velocity with the object's acceleration
|
||||||
|
velocity += object->GetAcceleration() * frameTime;
|
||||||
|
|
||||||
|
// Set the new velocity
|
||||||
|
object->SetVelocity(velocity);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Physics::ApplyForce(Object* object, XMVECTOR force)
|
||||||
{
|
{
|
||||||
if (object == nullptr) // Verify if the object is not null
|
if (object == nullptr) // Verify if the object is not null
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the velocity of the object
|
// Get the mass of the object
|
||||||
XMVECTOR velocity = object->GetVelocity();
|
float mass = object->GetMass();
|
||||||
|
|
||||||
// Calculate the new velocity
|
// Calculate the acceleration caused by the force
|
||||||
XMVECTOR newVelocity = velocity - (velocity * dragValue * frameTime);
|
XMVECTOR acceleration = force / mass;
|
||||||
|
|
||||||
// Update the velocity of the object
|
// Add the acceleration to the object's current acceleration
|
||||||
object->SetVelocity(newVelocity);
|
object->SetAcceleration(object->GetAcceleration() + acceleration);
|
||||||
}
|
}
|
@ -14,6 +14,7 @@ public:
|
|||||||
void SetGravity(XMVECTOR gravity); // Define the gravity value
|
void SetGravity(XMVECTOR gravity); // Define the gravity value
|
||||||
void ApplyGravity(Object*, float); // Apply gravity to an object
|
void ApplyGravity(Object*, float); // Apply gravity to an object
|
||||||
void ApplyDrag(Object*, float, float);
|
void ApplyDrag(Object*, float, float);
|
||||||
|
void ApplyForce(Object*, XMVECTOR);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
XMVECTOR m_gravity;
|
XMVECTOR m_gravity;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user