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)
|
||||
{
|
||||
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 frameTime;
|
||||
@ -673,11 +673,11 @@ bool ApplicationClass::Frame(InputClass* Input)
|
||||
m_Position->SetFrameTime(m_Timer->GetTime());
|
||||
|
||||
// Check if the left or right arrow key has been pressed, if so rotate the camera accordingly.
|
||||
keyDown = Input->IsLeftArrowPressed();
|
||||
m_Position->TurnLeft(keyDown);
|
||||
//keyDown = Input->IsLeftArrowPressed();
|
||||
//m_Position->TurnLeft(keyDown);
|
||||
|
||||
keyDown = Input->IsRightArrowPressed();
|
||||
m_Position->TurnRight(keyDown);
|
||||
//keyDown = Input->IsRightArrowPressed();
|
||||
//m_Position->TurnRight(keyDown);
|
||||
|
||||
m_Position->TurnMouse(deltaX, deltaY, rightMouseDown);
|
||||
|
||||
@ -728,27 +728,66 @@ bool ApplicationClass::Frame(InputClass* Input)
|
||||
//// Update the z position variable each frame.
|
||||
//z -= 0.0174532925f * 0.2f;
|
||||
|
||||
|
||||
keyLeft = Input->IsLeftArrowPressed();
|
||||
keyRight = Input->IsRightArrowPressed();
|
||||
keyUp = Input->IsUpArrowPressed();
|
||||
keyDown = Input->IsDownArrowPressed();
|
||||
|
||||
for (auto object : m_object)
|
||||
{
|
||||
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)
|
||||
{
|
||||
XMVECTOR currentPosition = object->GetPosition(); // Obtain the current 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Render the scene to a render texture.
|
||||
@ -1266,6 +1305,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath)
|
||||
|
||||
Object* newObject = new Object();
|
||||
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->SetName(filename);
|
||||
|
@ -3,11 +3,11 @@ Pos=60,60
|
||||
Size=400,400
|
||||
|
||||
[Window][Khaotic Engine]
|
||||
Pos=587,47
|
||||
Pos=596,31
|
||||
Size=694,210
|
||||
|
||||
[Window][Objects]
|
||||
Pos=91,47
|
||||
Pos=87,45
|
||||
Size=492,353
|
||||
|
||||
[Window][Terrain]
|
||||
|
@ -276,6 +276,27 @@ bool InputClass::IsRightArrowPressed()
|
||||
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 //
|
||||
///////////////////////////////////////////////////
|
||||
|
@ -39,6 +39,8 @@ public:
|
||||
void KeyUp(unsigned int);
|
||||
bool IsLeftArrowPressed();
|
||||
bool IsRightArrowPressed();
|
||||
bool IsUpArrowPressed();
|
||||
bool IsDownArrowPressed();
|
||||
bool IsAPressed();
|
||||
bool IsDPressed();
|
||||
bool IsWPressed();
|
||||
|
@ -9,6 +9,8 @@ Object::Object() : ModelClass()
|
||||
m_worldMatrix = XMMatrixIdentity();
|
||||
m_previousPosition = XMVectorZero();
|
||||
m_velocity = XMVectorZero();
|
||||
m_acceleration = XMVectorZero();
|
||||
m_mass = 1.0f;
|
||||
}
|
||||
|
||||
Object::~Object()
|
||||
@ -173,4 +175,24 @@ void Object::SetVelocity(XMVECTOR velocity)
|
||||
XMVECTOR Object::GetVelocity()
|
||||
{
|
||||
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);
|
||||
XMVECTOR GetVelocity();
|
||||
void SetAcceleration(XMVECTOR);
|
||||
XMVECTOR GetAcceleration();
|
||||
void SetMass(float);
|
||||
float GetMass();
|
||||
|
||||
void UpdateWorldMatrix();
|
||||
void UpdateSRMatrix();
|
||||
@ -53,5 +57,8 @@ private:
|
||||
XMMATRIX m_srMatrix;
|
||||
XMMATRIX m_worldMatrix;
|
||||
|
||||
XMVECTOR m_acceleration;
|
||||
float m_mass;
|
||||
|
||||
std::string m_name;
|
||||
};
|
||||
|
@ -29,34 +29,63 @@ void Physics::SetGravity(XMVECTOR gravity)
|
||||
// Apply gravity to an object
|
||||
void Physics::ApplyGravity(Object* object, float frameTime)
|
||||
{
|
||||
if (object == nullptr) // Verify if the object is not null
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (object == nullptr) // Verify if the object is not null
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the object velocity
|
||||
XMVECTOR velocity = object->GetVelocity();
|
||||
// Calculate the acceleration caused by gravity
|
||||
XMVECTOR gravityAcceleration = m_gravity / object->GetMass();
|
||||
|
||||
// Update the velocity with gravity
|
||||
velocity += m_gravity * frameTime;
|
||||
// Add the gravity acceleration to the object's current acceleration
|
||||
object->SetAcceleration(object->GetAcceleration() + gravityAcceleration);
|
||||
|
||||
// Set the new velocity
|
||||
object->SetVelocity(velocity);
|
||||
// Get the object 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)
|
||||
{
|
||||
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
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the velocity of the object
|
||||
XMVECTOR velocity = object->GetVelocity();
|
||||
// Get the mass of the object
|
||||
float mass = object->GetMass();
|
||||
|
||||
// Calculate the new velocity
|
||||
XMVECTOR newVelocity = velocity - (velocity * dragValue * frameTime);
|
||||
// Calculate the acceleration caused by the force
|
||||
XMVECTOR acceleration = force / mass;
|
||||
|
||||
// Update the velocity of the object
|
||||
object->SetVelocity(newVelocity);
|
||||
// Add the acceleration to the object's current acceleration
|
||||
object->SetAcceleration(object->GetAcceleration() + acceleration);
|
||||
}
|
@ -14,6 +14,7 @@ public:
|
||||
void SetGravity(XMVECTOR gravity); // Define the gravity value
|
||||
void ApplyGravity(Object*, float); // Apply gravity to an object
|
||||
void ApplyDrag(Object*, float, float);
|
||||
void ApplyForce(Object*, XMVECTOR);
|
||||
|
||||
private:
|
||||
XMVECTOR m_gravity;
|
||||
|
Loading…
x
Reference in New Issue
Block a user