mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-06 20:24:33 +00:00
211 lines
5.5 KiB
C++
211 lines
5.5 KiB
C++
#ifndef _T3D_JOLT_PLUGIN_H_
|
|
#define _T3D_JOLT_PLUGIN_H_
|
|
|
|
#ifndef _T3D_PHYSICS_PHYSICSPLUGIN_H_
|
|
#include "T3D/physics/physicsPlugin.h"
|
|
#endif
|
|
|
|
#ifndef _SCENERENDERSTATE_H_
|
|
#include "scene/sceneRenderState.h"
|
|
#endif
|
|
|
|
#if defined(TORQUE_TOOLS) && defined(TORQUE_OS_WIN)
|
|
#ifndef JPH_DEBUG_RENDERER
|
|
#define JPH_DEBUG_RENDERER
|
|
#endif
|
|
#endif
|
|
|
|
// Save and undefine the macro if it exists
|
|
#ifdef Offset
|
|
#pragma push_macro("Offset")
|
|
#undef Offset
|
|
#endif
|
|
|
|
#include <Jolt/Jolt.h>
|
|
#include <Jolt/Physics/PhysicsSystem.h>
|
|
#include <Jolt/RegisterTypes.h>
|
|
#include <Jolt/Core/TempAllocator.h>
|
|
#include <Jolt/Core/StreamWrapper.h>
|
|
#include <Jolt/Physics/Collision/RayCast.h>
|
|
#include <Jolt/Physics/Collision/CastResult.h>
|
|
#ifdef JPH_DEBUG_RENDERER
|
|
#include <Jolt/Renderer/DebugRenderer.h>
|
|
#endif
|
|
#include <Jolt/Core/JobSystemThreadPool.h>
|
|
#include <Jolt/Physics/PhysicsSettings.h>
|
|
#include <Jolt/Physics/StateRecorderImpl.h>
|
|
#include <Jolt/Physics/Collision/Shape/BoxShape.h>
|
|
#include <Jolt/Physics/Collision/Shape/CylinderShape.h>
|
|
#include <Jolt/Physics/Collision/Shape/SphereShape.h>
|
|
#include <Jolt/Physics/Collision/Shape/MeshShape.h>
|
|
#include <Jolt/Physics/Body/BodyCreationSettings.h>
|
|
#include <Jolt/Physics/Constraints/SixDOFConstraint.h>
|
|
#include <Jolt/Physics/Body/BodyActivationListener.h>
|
|
|
|
#ifdef Offset
|
|
// Restore the original macro after includes
|
|
#pragma pop_macro("Offset")
|
|
#endif
|
|
|
|
inline Point3F joltCast(const JPH::Vec3& vec)
|
|
{
|
|
return Point3F(vec.GetX(), vec.GetY(), vec.GetZ());
|
|
}
|
|
|
|
inline JPH::Vec3 joltCast(const Point3F& point)
|
|
{
|
|
return JPH::Vec3(point.x, point.y, point.z);
|
|
}
|
|
|
|
inline QuatF joltCast(const JPH::Quat& quat)
|
|
{
|
|
/// The Torque quat has the opposite winding order.
|
|
return QuatF(-quat.GetX(), -quat.GetY(), -quat.GetZ(), quat.GetW());
|
|
}
|
|
|
|
inline JPH::Quat joltCast(const QuatF& quat)
|
|
{
|
|
/// The Torque quat has the opposite winding order.
|
|
return JPH::Quat(-quat.x, -quat.y, -quat.z, quat.w);
|
|
}
|
|
|
|
inline JPH::RMat44 joltCast(const MatrixF& m)
|
|
{
|
|
QuatF ang(m);
|
|
return JPH::Mat44::sRotationTranslation(joltCast(ang), joltCast(m.getPosition()));
|
|
}
|
|
|
|
inline MatrixF joltCast(const JPH::Mat44& xfm)
|
|
{
|
|
MatrixF out;
|
|
|
|
QuatF ang = joltCast(xfm.GetQuaternion());
|
|
ang.setMatrix(&out);
|
|
out.setPosition(joltCast(xfm.GetTranslation()));
|
|
|
|
return out;
|
|
}
|
|
|
|
inline void toJolt(const MatrixF& mat, JPH::RVec3& pos, JPH::Quat& rot)
|
|
{
|
|
Point3F p;
|
|
p = mat.getPosition();
|
|
|
|
pos = joltCast(p);
|
|
|
|
QuatF q(mat);
|
|
|
|
rot = joltCast(q);
|
|
}
|
|
|
|
inline JPH::AABox toJolt(const Box3F& box)
|
|
{
|
|
return JPH::AABox(
|
|
joltCast(box.minExtents),
|
|
joltCast(box.maxExtents)
|
|
);
|
|
}
|
|
|
|
inline Box3F fromJolt(const JPH::AABox& box)
|
|
{
|
|
return Box3F(
|
|
joltCast(box.mMin),
|
|
joltCast(box.mMax)
|
|
);
|
|
}
|
|
|
|
#ifndef _COLOR_H_
|
|
#include "core/color.h"
|
|
#endif
|
|
|
|
inline JPH::Color toJolt(const ColorI& c)
|
|
{
|
|
return JPH::Color(c.red, c.green, c.blue, c.alpha);
|
|
}
|
|
|
|
|
|
|
|
inline ColorI fromJolt(const JPH::Color& c)
|
|
{
|
|
return ColorI(c.r, c.g, c.b, c.a);
|
|
}
|
|
|
|
#ifdef JPH_DEBUG_RENDERER
|
|
struct DebugRenderFrameState
|
|
{
|
|
Frustum frustum;
|
|
JPH::Vec3 cameraPos;
|
|
};
|
|
|
|
class JoltDebugRenderer final : public JPH::DebugRenderer
|
|
{
|
|
public:
|
|
JPH_OVERRIDE_NEW_DELETE
|
|
explicit JoltDebugRenderer();
|
|
virtual void DrawLine(JPH::RVec3Arg inFrom, JPH::RVec3Arg inTo, JPH::ColorArg inColor) override;
|
|
virtual void DrawTriangle(JPH::RVec3Arg inV1, JPH::RVec3Arg inV2, JPH::RVec3Arg inV3, JPH::ColorArg inColor, ECastShadow inCastShadow) override;
|
|
virtual JPH::DebugRenderer::Batch CreateTriangleBatch(const Triangle* inTriangles, int inTriangleCount) override;
|
|
virtual JPH::DebugRenderer::Batch CreateTriangleBatch(const Vertex* inVertices, int inVertexCount, const JPH::uint32* inIndices, int inIndexCount) override;
|
|
virtual void DrawGeometry(JPH::RMat44Arg inModelMatrix, const JPH::AABox& inWorldSpaceBounds, float inLODScaleSq, JPH::ColorArg inModelColor, const GeometryRef& inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) override;
|
|
virtual void DrawText3D(JPH::RVec3Arg inPosition, const JPH::string_view& inString, JPH::ColorArg inColor, float inHeight) override {};
|
|
|
|
|
|
void SetFrameState(const DebugRenderFrameState& state)
|
|
{
|
|
mFrameState = state;
|
|
}
|
|
|
|
/// Implementation specific batch object
|
|
class BatchImpl : public JPH::RefTargetVirtual
|
|
{
|
|
public:
|
|
JPH_OVERRIDE_NEW_DELETE
|
|
|
|
virtual void AddRef() override
|
|
{
|
|
++mRefCount;
|
|
}
|
|
virtual void Release() override
|
|
{
|
|
if (--mRefCount == 0)
|
|
delete this;
|
|
}
|
|
|
|
JPH::Array<JPH::DebugRenderer::Triangle> mTriangles;
|
|
|
|
private:
|
|
JPH::atomic<U32> mRefCount = 0;
|
|
};
|
|
|
|
private:
|
|
DebugRenderFrameState mFrameState;
|
|
};
|
|
#endif
|
|
|
|
class JoltPlugin : public PhysicsPlugin
|
|
{
|
|
public:
|
|
JoltPlugin();
|
|
~JoltPlugin();
|
|
|
|
/// Create function for factory.
|
|
static PhysicsPlugin* create();
|
|
|
|
// PhysicsPlugin
|
|
virtual void destroyPlugin();
|
|
virtual void reset();
|
|
virtual PhysicsCollision* createCollision();
|
|
virtual PhysicsBody* createBody();
|
|
virtual PhysicsPlayer* createPlayer();
|
|
virtual bool isSimulationEnabled() const;
|
|
virtual void enableSimulation(const String& worldName, bool enable);
|
|
virtual void setTimeScale(const F32 timeScale);
|
|
virtual const F32 getTimeScale() const;
|
|
virtual bool createWorld(const String& worldName);
|
|
virtual void destroyWorld(const String& worldName);
|
|
virtual PhysicsWorld* getWorld(const String& worldName) const;
|
|
virtual PhysicsWorld* getWorld() const;
|
|
virtual U32 getWorldCount() const;
|
|
};
|
|
|
|
#endif // !_T3D_JOLT_PLUGIN_H_
|