|
@@ -16,6 +16,7 @@
|
|
#include <Jolt/Physics/StateRecorderImpl.h>
|
|
#include <Jolt/Physics/StateRecorderImpl.h>
|
|
#include <Jolt/Physics/Body/BodyCreationSettings.h>
|
|
#include <Jolt/Physics/Body/BodyCreationSettings.h>
|
|
#include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
|
|
#include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
|
|
|
|
+#include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
|
|
#include <Jolt/Physics/PhysicsScene.h>
|
|
#include <Jolt/Physics/PhysicsScene.h>
|
|
#include <Jolt/Physics/Collision/RayCast.h>
|
|
#include <Jolt/Physics/Collision/RayCast.h>
|
|
#include <Jolt/Physics/Collision/ShapeCast.h>
|
|
#include <Jolt/Physics/Collision/ShapeCast.h>
|
|
@@ -42,6 +43,7 @@
|
|
#include <Utils/Log.h>
|
|
#include <Utils/Log.h>
|
|
#include <Utils/ShapeCreator.h>
|
|
#include <Utils/ShapeCreator.h>
|
|
#include <Utils/CustomMemoryHook.h>
|
|
#include <Utils/CustomMemoryHook.h>
|
|
|
|
+#include <Utils/SoftBodyCreator.h>
|
|
#include <Renderer/DebugRendererImp.h>
|
|
#include <Renderer/DebugRendererImp.h>
|
|
|
|
|
|
JPH_SUPPRESS_WARNINGS_STD_BEGIN
|
|
JPH_SUPPRESS_WARNINGS_STD_BEGIN
|
|
@@ -515,7 +517,7 @@ SamplesApp::SamplesApp()
|
|
UIElement *shoot_options = mDebugUI->CreateMenu();
|
|
UIElement *shoot_options = mDebugUI->CreateMenu();
|
|
mDebugUI->CreateTextButton(shoot_options, "Shoot Object (B)", [=]() { ShootObject(); });
|
|
mDebugUI->CreateTextButton(shoot_options, "Shoot Object (B)", [=]() { ShootObject(); });
|
|
mDebugUI->CreateSlider(shoot_options, "Initial Velocity", mShootObjectVelocity, 0.0f, 500.0f, 10.0f, [this](float inValue) { mShootObjectVelocity = inValue; });
|
|
mDebugUI->CreateSlider(shoot_options, "Initial Velocity", mShootObjectVelocity, 0.0f, 500.0f, 10.0f, [this](float inValue) { mShootObjectVelocity = inValue; });
|
|
- mDebugUI->CreateComboBox(shoot_options, "Shape", { "Sphere", "ConvexHull", "Thin Bar" }, (int)mShootObjectShape, [=](int inItem) { mShootObjectShape = (EShootObjectShape)inItem; });
|
|
|
|
|
|
+ mDebugUI->CreateComboBox(shoot_options, "Shape", { "Sphere", "ConvexHull", "Thin Bar", "Soft Body Cube" }, (int)mShootObjectShape, [=](int inItem) { mShootObjectShape = (EShootObjectShape)inItem; });
|
|
mDebugUI->CreateComboBox(shoot_options, "Motion Quality", { "Discrete", "LinearCast" }, (int)mShootObjectMotionQuality, [=](int inItem) { mShootObjectMotionQuality = (EMotionQuality)inItem; });
|
|
mDebugUI->CreateComboBox(shoot_options, "Motion Quality", { "Discrete", "LinearCast" }, (int)mShootObjectMotionQuality, [=](int inItem) { mShootObjectMotionQuality = (EMotionQuality)inItem; });
|
|
mDebugUI->CreateSlider(shoot_options, "Friction", mShootObjectFriction, 0.0f, 1.0f, 0.05f, [this](float inValue) { mShootObjectFriction = inValue; });
|
|
mDebugUI->CreateSlider(shoot_options, "Friction", mShootObjectFriction, 0.0f, 1.0f, 0.05f, [this](float inValue) { mShootObjectFriction = inValue; });
|
|
mDebugUI->CreateSlider(shoot_options, "Restitution", mShootObjectRestitution, 0.0f, 1.0f, 0.05f, [this](float inValue) { mShootObjectRestitution = inValue; });
|
|
mDebugUI->CreateSlider(shoot_options, "Restitution", mShootObjectRestitution, 0.0f, 1.0f, 0.05f, [this](float inValue) { mShootObjectRestitution = inValue; });
|
|
@@ -920,6 +922,10 @@ RefConst<Shape> SamplesApp::CreateShootObjectShape()
|
|
case EShootObjectShape::ThinBar:
|
|
case EShootObjectShape::ThinBar:
|
|
shape = BoxShapeSettings(Vec3(0.05f, 0.8f, 0.03f), 0.015f).Create().Get();
|
|
shape = BoxShapeSettings(Vec3(0.05f, 0.8f, 0.03f), 0.015f).Create().Get();
|
|
break;
|
|
break;
|
|
|
|
+
|
|
|
|
+ case EShootObjectShape::SoftBodyCube:
|
|
|
|
+ JPH_ASSERT(false);
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
|
|
|
|
// Scale shape if needed
|
|
// Scale shape if needed
|
|
@@ -931,15 +937,36 @@ RefConst<Shape> SamplesApp::CreateShootObjectShape()
|
|
|
|
|
|
void SamplesApp::ShootObject()
|
|
void SamplesApp::ShootObject()
|
|
{
|
|
{
|
|
- // Configure body
|
|
|
|
- BodyCreationSettings creation_settings(CreateShootObjectShape(), GetCamera().mPos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
|
|
|
|
- creation_settings.mMotionQuality = mShootObjectMotionQuality;
|
|
|
|
- creation_settings.mFriction = mShootObjectFriction;
|
|
|
|
- creation_settings.mRestitution = mShootObjectRestitution;
|
|
|
|
- creation_settings.mLinearVelocity = mShootObjectVelocity * GetCamera().mForward;
|
|
|
|
-
|
|
|
|
- // Create body
|
|
|
|
- mPhysicsSystem->GetBodyInterface().CreateAndAddBody(creation_settings, EActivation::Activate);
|
|
|
|
|
|
+ if (mShootObjectShape != EShootObjectShape::SoftBodyCube)
|
|
|
|
+ {
|
|
|
|
+ // Configure body
|
|
|
|
+ BodyCreationSettings creation_settings(CreateShootObjectShape(), GetCamera().mPos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
|
|
|
|
+ creation_settings.mMotionQuality = mShootObjectMotionQuality;
|
|
|
|
+ creation_settings.mFriction = mShootObjectFriction;
|
|
|
|
+ creation_settings.mRestitution = mShootObjectRestitution;
|
|
|
|
+ creation_settings.mLinearVelocity = mShootObjectVelocity * GetCamera().mForward;
|
|
|
|
+
|
|
|
|
+ // Create body
|
|
|
|
+ mPhysicsSystem->GetBodyInterface().CreateAndAddBody(creation_settings, EActivation::Activate);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ Ref<SoftBodySharedSettings> shared_settings = SoftBodyCreator::CreateCube(5, 0.5f * GetWorldScale());
|
|
|
|
+ for (SoftBodySharedSettings::Vertex &v : shared_settings->mVertices)
|
|
|
|
+ {
|
|
|
|
+ v.mInvMass = 0.025f;
|
|
|
|
+ (mShootObjectVelocity * GetCamera().mForward).StoreFloat3(&v.mVelocity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Confgure soft body
|
|
|
|
+ SoftBodyCreationSettings creation_settings(shared_settings, GetCamera().mPos);
|
|
|
|
+ creation_settings.mObjectLayer = Layers::MOVING;
|
|
|
|
+ creation_settings.mFriction = mShootObjectFriction;
|
|
|
|
+ creation_settings.mRestitution = mShootObjectRestitution;
|
|
|
|
+
|
|
|
|
+ // Create body
|
|
|
|
+ mPhysicsSystem->GetBodyInterface().CreateAndAddSoftBody(creation_settings, EActivation::Activate);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
bool SamplesApp::CastProbe(float inProbeLength, float &outFraction, RVec3 &outPosition, BodyID &outID)
|
|
bool SamplesApp::CastProbe(float inProbeLength, float &outFraction, RVec3 &outPosition, BodyID &outID)
|
|
@@ -1381,7 +1408,7 @@ bool SamplesApp::CastProbe(float inProbeLength, float &outFraction, RVec3 &outPo
|
|
SoftBodyVertex tmp_vertex;
|
|
SoftBodyVertex tmp_vertex;
|
|
tmp_vertex.mInvMass = 1.0f;
|
|
tmp_vertex.mInvMass = 1.0f;
|
|
tmp_vertex.mPosition = fraction * direction;
|
|
tmp_vertex.mPosition = fraction * direction;
|
|
- tmp_vertex.mVelocity = Vec3(0, -9.81f / 60.0f, 0);
|
|
|
|
|
|
+ tmp_vertex.mVelocity = 10.0f * direction;
|
|
tmp_vertex.mCollidingShapeIndex = -1;
|
|
tmp_vertex.mCollidingShapeIndex = -1;
|
|
tmp_vertex.mLargestPenetration = -FLT_MAX;
|
|
tmp_vertex.mLargestPenetration = -FLT_MAX;
|
|
Array<SoftBodyVertex> vertices = { tmp_vertex };
|
|
Array<SoftBodyVertex> vertices = { tmp_vertex };
|
|
@@ -1399,7 +1426,7 @@ bool SamplesApp::CastProbe(float inProbeLength, float &outFraction, RVec3 &outPo
|
|
for (const TransformedShape &ts : collector.mHits)
|
|
for (const TransformedShape &ts : collector.mHits)
|
|
{
|
|
{
|
|
int colliding_shape_index = int(&ts - collector.mHits.data());
|
|
int colliding_shape_index = int(&ts - collector.mHits.data());
|
|
- ts.mShape->CollideSoftBodyVertices((RMat44::sTranslation(-start) * ts.GetCenterOfMassTransform()).ToMat44(), vertices, 1.0f / 60.0f, Vec3(0, -0.5f * 9.81f / Square(60.0f), 0), colliding_shape_index);
|
|
|
|
|
|
+ ts.mShape->CollideSoftBodyVertices((RMat44::sTranslation(-start) * ts.GetCenterOfMassTransform()).ToMat44(), ts.GetShapeScale(), vertices, 1.0f / 60.0f, Vec3::sZero(), colliding_shape_index);
|
|
if (vertices[0].mCollidingShapeIndex == colliding_shape_index)
|
|
if (vertices[0].mCollidingShapeIndex == colliding_shape_index)
|
|
{
|
|
{
|
|
// To draw a plane, we need a point but CollideSoftBodyVertices doesn't provide one, so we use CollideShape with a tiny sphere to get the closest point and then project that onto the plane to draw the plane
|
|
// To draw a plane, we need a point but CollideSoftBodyVertices doesn't provide one, so we use CollideShape with a tiny sphere to get the closest point and then project that onto the plane to draw the plane
|