From 159bc09c15a61079c8d4f19b00dd100872ef64d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 3 Feb 2022 00:16:34 +0100 Subject: [PATCH] Havok: Add hkpCylinderShape --- lib/hkStubs/CMakeLists.txt | 1 + .../Shape/Convex/Cylinder/hkpCylinderShape.h | 121 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/Cylinder/hkpCylinderShape.h diff --git a/lib/hkStubs/CMakeLists.txt b/lib/hkStubs/CMakeLists.txt index 4a51d07a..86dfef6a 100644 --- a/lib/hkStubs/CMakeLists.txt +++ b/lib/hkStubs/CMakeLists.txt @@ -116,6 +116,7 @@ add_library(hkStubs OBJECT Havok/Physics2012/Collide/Shape/Convex/Box/hkpBoxShape.h Havok/Physics2012/Collide/Shape/Convex/Capsule/hkpCapsuleShape.h Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h + Havok/Physics2012/Collide/Shape/Convex/Cylinder/hkpCylinderShape.h Havok/Physics2012/Collide/Shape/HeightField/hkpHeightFieldShape.h Havok/Physics2012/Collide/Shape/HeightField/hkpSphereRepShape.h Havok/Physics2012/Collide/Shape/HeightField/Plane/hkpPlaneShape.h diff --git a/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/Cylinder/hkpCylinderShape.h b/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/Cylinder/hkpCylinderShape.h new file mode 100644 index 00000000..690be643 --- /dev/null +++ b/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/Cylinder/hkpCylinderShape.h @@ -0,0 +1,121 @@ +#pragma once + +#include +#include + +class hkpCylinderShape : public hkpConvexShape { +public: + HK_DECLARE_CLASS_ALLOCATOR(hkpCylinderShape) + HK_DECLARE_REFLECTION() + HKCD_DECLARE_SHAPE_TYPE(hkcdShapeType::CYLINDER) + + enum VertexIdEncoding { + VERTEX_ID_ENCODING_IS_BASE_A_SHIFT = 7, + VERTEX_ID_ENCODING_SIN_SIGN_SHIFT = 6, + VERTEX_ID_ENCODING_COS_SIGN_SHIFT = 5, + VERTEX_ID_ENCODING_IS_SIN_LESSER_SHIFT = 4, + VERTEX_ID_ENCODING_VALUE_MASK = 0x0f + }; + + HK_FORCE_INLINE hkpCylinderShape() {} + + hkpCylinderShape(const hkVector4& vertexA, const hkVector4& vertexB, hkReal cylinderRadius, + hkReal paddingRadius = hkConvexShapeDefaultRadius); + + explicit hkpCylinderShape(hkFinishLoadedObjectFlag flag); + + void getSupportingVertex(hkVector4Parameter direction, + hkcdVertex& supportingVertexOut) const override; + void convertVertexIdsToVertices(const hkpVertexId* ids, int numIds, + hkcdVertex* verticesOut) const override; + void getCentre(hkVector4& centreOut) const override; + HK_FORCE_INLINE int getNumCollisionSpheres() const override; + const hkSphere* getCollisionSpheres(hkSphere* sphereBuffer) const override; + void getAabb(const hkTransform& localToWorld, hkReal tolerance, hkAabb& out) const override; + hkBool castRay(const hkpShapeRayCastInput& input, + hkpShapeRayCastOutput& results) const override; + + inline const hkVector4* getVertices() const; + + HK_FORCE_INLINE const hkVector4& getVertex(int i) const; + template + HK_FORCE_INLINE const hkVector4& getVertex() const; + + HK_FORCE_INLINE void setVertex(int i, const hkVector4& position); + template + HK_FORCE_INLINE void setVertex(hkVector4Parameter position); + + hkReal getCylinderRadius() const; + void setCylinderRadius(hkReal radius); + + hkReal getCylinderBaseRadiusFactorForHeightfieldCollisions() { + return m_cylBaseRadiusFactorForHeightFieldCollisions; + } + + inline void setCylinderBaseRadiusFactorForHeightfieldCollisions(hkReal radiusFactor) { + m_cylBaseRadiusFactorForHeightFieldCollisions = radiusFactor; + } + + void presetPerpendicularVector(); + + static void setNumberOfVirtualSideSegments(int numSegments); + + void getFirstVertex(hkVector4& v) const override; + + HK_FORCE_INLINE static void initRoundUpThreshold(); + +protected: + hkReal m_cylRadius; + hkReal m_cylBaseRadiusFactorForHeightFieldCollisions; + hkVector4 m_vertexA; + hkVector4 m_vertexB; + hkVector4 m_perpendicular1; + hkVector4 m_perpendicular2; + +private: + void decodeVertexId(hkpVertexId code, hkVector4& result) const; + + static hkReal s_virtualTesselationParameter; + static hkReal s_virtualTesselationParameterInv; + static hkReal s_intRoundUpThreshold; +}; + +inline int hkpCylinderShape::getNumCollisionSpheres() const { + return 16 + 2; +} + +inline const hkVector4* hkpCylinderShape::getVertices() const { + return &m_vertexA; +} + +inline const hkVector4& hkpCylinderShape::getVertex(int i) const { + return getVertices()[i]; +} + +template +inline const hkVector4& hkpCylinderShape::getVertex() const { + static_assert(I == 0 || I == 1, "invalid index"); + return getVertices()[I]; +} + +inline void hkpCylinderShape::setVertex(int i, const hkVector4& position) { + (&m_vertexA)[i] = position; +} + +template +inline void hkpCylinderShape::setVertex(const hkVector4f& position) { + static_assert(I == 0 || I == 1, "invalid index"); + (&m_vertexA)[I] = position; +} + +inline void hkpCylinderShape::initRoundUpThreshold() { + if (s_intRoundUpThreshold < 0.0f) { + for (hkReal value = 0.0f; value < 1.1f; value += 0.01f) { + if (hkMath::hkToIntFast(value) != 0) { + s_intRoundUpThreshold = 1.0f - value; + return; + } + } + s_intRoundUpThreshold = 0.0f; + } +}