Patch update - V9.2.13
Physics update
This commit is contained in:
@@ -67,6 +67,7 @@ private:
|
||||
bool showLogWindow;
|
||||
|
||||
bool m_isPhyiscsEnabled = false;
|
||||
bool m_isGravityEnabled = false;
|
||||
|
||||
ImGuiIO* io;
|
||||
|
||||
|
@@ -95,11 +95,23 @@ public:
|
||||
ObjectType StringToObjectType(const std::string& objectType);
|
||||
std::string ObjectTypeToString(ObjectType objectType);
|
||||
|
||||
void LaunchObject();
|
||||
void SetAlpha(float alpha) { m_alpha = alpha; }
|
||||
float GetAlpha() const { return m_alpha; }
|
||||
void SetInitialStretch(float initialStretch) { m_initialStretch = initialStretch; }
|
||||
float GetInitialStretch() const { return m_initialStretch; }
|
||||
void SetSpringConstant(float springConstant) { m_springConstant = springConstant; }
|
||||
float GetSpringConstant() const { return m_springConstant; }
|
||||
|
||||
bool IsGravityEnabled() const { return m_gravityEnabled; }
|
||||
void SetGravityEnabled(bool state) { m_gravityEnabled = state; }
|
||||
|
||||
public :
|
||||
bool m_demoSpinning = false;
|
||||
XMVECTOR m_previousPosition;
|
||||
XMVECTOR m_velocity;
|
||||
int m_id;
|
||||
bool m_gravityEnabled = true;
|
||||
|
||||
private:
|
||||
XMMATRIX m_scaleMatrix;
|
||||
@@ -121,4 +133,9 @@ private:
|
||||
|
||||
float m_boundingRadius;
|
||||
std::wstring m_modelPath;
|
||||
float m_alpha = 0.0f;
|
||||
float m_initialStretch = 0.0f;
|
||||
float m_springConstant = 10.0f;
|
||||
|
||||
|
||||
};
|
||||
|
@@ -2150,7 +2150,9 @@ bool ApplicationClass::RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bo
|
||||
object->SetVelocity(velocity);
|
||||
}
|
||||
|
||||
m_Physics->ApplyGravity(object, deltaTime);
|
||||
if (object->m_gravityEnabled) {
|
||||
m_Physics->ApplyGravity(object, deltaTime);
|
||||
}
|
||||
|
||||
if (XMVectorGetY(object->GetPosition()) < -30.0f) {
|
||||
XMVECTOR currentPosition = object->GetPosition();
|
||||
|
@@ -401,6 +401,14 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app)
|
||||
object->SetPhysicsEnabled(m_isPhyiscsEnabled);
|
||||
}
|
||||
|
||||
// Gravity Enabled checkbox
|
||||
std::string gravityLabel = "Gravity##" + std::to_string(index);
|
||||
if (ImGui::Checkbox(gravityLabel.c_str(), &object->m_gravityEnabled))
|
||||
{
|
||||
object->SetGravityEnabled(object->m_gravityEnabled);
|
||||
}
|
||||
|
||||
|
||||
// 3 radio button on the same line to set the ObjectType
|
||||
std::string typeLabel = "Type##" + std::to_string(index);
|
||||
ObjectType type = object->GetType();
|
||||
@@ -419,6 +427,40 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app)
|
||||
object->SetType(ObjectType::Sphere);
|
||||
}
|
||||
|
||||
// button to launch the object
|
||||
std::string launchLabel = "Launch##" + std::to_string(index);
|
||||
|
||||
// paraeter to set the alpha, initial stretch and spring constant
|
||||
float alpha = object->GetAlpha();
|
||||
float initialStretch = object->GetInitialStretch();
|
||||
float springConstant = object->GetSpringConstant();
|
||||
if (ImGui::DragFloat("Alpha##" , &alpha, 0.01f, 0.0f, 1.0f))
|
||||
{
|
||||
object->SetAlpha(alpha);
|
||||
}
|
||||
if (ImGui::DragFloat("Initial Stretch##", &initialStretch, 0.01f, 0.0f, 1.0f))
|
||||
{
|
||||
object->SetInitialStretch(initialStretch);
|
||||
}
|
||||
if (ImGui::DragFloat("Spring Constant##", &springConstant, 0.01f, 0.0f, 100.0f))
|
||||
{
|
||||
object->SetSpringConstant(springConstant);
|
||||
}
|
||||
|
||||
if (ImGui::Button(launchLabel.c_str()))
|
||||
{
|
||||
object->LaunchObject();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
// button to stop the object
|
||||
std::string stopLabel = "Stop##" + std::to_string(index);
|
||||
if (ImGui::Button(stopLabel.c_str()))
|
||||
{
|
||||
object->SetVelocity(XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
object->SetPosition(XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ImGui::Separator();
|
||||
|
@@ -306,4 +306,46 @@ ShaderType Object::StringToShaderType(const std::string& str) {
|
||||
if (str == "SUNLIGHT") return ShaderType::SUNLIGHT;
|
||||
// Add other cases as needed
|
||||
return ShaderType::TEXTURE;
|
||||
}
|
||||
|
||||
void Object::LaunchObject()
|
||||
{
|
||||
// Constants
|
||||
const float gravity = -9.81f;
|
||||
|
||||
// Convert alpha from degrees to radians if needed
|
||||
float alphaRadians = m_alpha * (XM_PI / 180.0f);
|
||||
|
||||
// Scale factors to make the physics simulation more visible
|
||||
float scaleFactor = 200.0f; // Adjust this based on your world scale
|
||||
|
||||
// Calculate initial velocity magnitude using the same formula as the Python code
|
||||
// v_eject = l1 * sqrt(k/m) * sqrt(1 - (m*g*sin(alpha)/(k*l1))^2)
|
||||
float velocityMagnitude = m_initialStretch * sqrtf(m_springConstant / m_mass) *
|
||||
sqrtf(1.0f - powf((m_mass * gravity * sinf(alphaRadians) / (m_springConstant * m_initialStretch)), 2.0f));
|
||||
|
||||
// Apply scale factor
|
||||
velocityMagnitude *= scaleFactor;
|
||||
|
||||
// Calculate velocity components
|
||||
XMVECTOR velocity = XMVectorSet(
|
||||
velocityMagnitude * cosf(alphaRadians), // vx = v0 * cos(alpha)
|
||||
velocityMagnitude * sinf(alphaRadians), // vy = v0 * sin(alpha)
|
||||
0.0f, // z-component (0 for 2D trajectory)
|
||||
0.0f
|
||||
);
|
||||
|
||||
// Apply velocity to object
|
||||
SetVelocity(velocity);
|
||||
|
||||
// Enable physics for the object to handle the trajectory
|
||||
SetPhysicsEnabled(true);
|
||||
|
||||
// Reset grounded state
|
||||
SetGrounded(false);
|
||||
|
||||
// Debug output
|
||||
char buffer[256];
|
||||
sprintf_s(buffer, "Launch velocity: %f m/s at angle %f degrees", XMVectorGetX(XMVector3Length(velocity)), m_alpha);
|
||||
OutputDebugStringA(buffer);
|
||||
}
|
Reference in New Issue
Block a user