Physics2D/Collider2D: Implement CompoundCollider2D and ConvexCollider2D
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <Nazara/Core/ObjectRef.hpp>
|
||||
#include <Nazara/Core/ObjectLibrary.hpp>
|
||||
#include <Nazara/Core/Signal.hpp>
|
||||
#include <Nazara/Core/SparsePtr.hpp>
|
||||
#include <Nazara/Math/Rect.hpp>
|
||||
#include <Nazara/Math/Vector2.hpp>
|
||||
#include <Nazara/Physics2D/Config.hpp>
|
||||
@@ -33,6 +34,7 @@ namespace Nz
|
||||
{
|
||||
friend Collider2DLibrary;
|
||||
friend RigidBody2D;
|
||||
friend class CompoundCollider2D; //< See CompoundCollider2D::CreateShapes
|
||||
|
||||
public:
|
||||
inline Collider2D();
|
||||
@@ -40,7 +42,7 @@ namespace Nz
|
||||
Collider2D(Collider2D&&) = delete;
|
||||
virtual ~Collider2D();
|
||||
|
||||
virtual float ComputeInertialMatrix(float mass) const = 0;
|
||||
virtual float ComputeMomentOfInertia(float mass) const = 0;
|
||||
|
||||
inline Nz::UInt32 GetCategoryMask() const;
|
||||
inline Nz::UInt32 GetCollisionGroup() const;
|
||||
@@ -64,7 +66,7 @@ namespace Nz
|
||||
NazaraSignal(OnColliderRelease, const Collider2D* /*collider*/);
|
||||
|
||||
protected:
|
||||
virtual std::vector<cpShape*> CreateShapes(RigidBody2D* body) const = 0;
|
||||
virtual void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const = 0;
|
||||
|
||||
bool m_trigger;
|
||||
Nz::UInt32 m_categoryMask;
|
||||
@@ -89,7 +91,7 @@ namespace Nz
|
||||
BoxCollider2D(const Vector2f& size, float radius = 0.f);
|
||||
BoxCollider2D(const Rectf& rect, float radius = 0.f);
|
||||
|
||||
float ComputeInertialMatrix(float mass) const override;
|
||||
float ComputeMomentOfInertia(float mass) const override;
|
||||
|
||||
inline const Rectf& GetRect() const;
|
||||
inline Vector2f GetSize() const;
|
||||
@@ -98,7 +100,7 @@ namespace Nz
|
||||
template<typename... Args> static BoxCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
||||
|
||||
Rectf m_rect;
|
||||
float m_radius;
|
||||
@@ -114,7 +116,7 @@ namespace Nz
|
||||
public:
|
||||
CircleCollider2D(float radius, const Vector2f& offset = Vector2f::Zero());
|
||||
|
||||
float ComputeInertialMatrix(float mass) const override;
|
||||
float ComputeMomentOfInertia(float mass) const override;
|
||||
|
||||
inline float GetRadius() const;
|
||||
ColliderType2D GetType() const override;
|
||||
@@ -122,12 +124,58 @@ namespace Nz
|
||||
template<typename... Args> static CircleCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
||||
|
||||
Vector2f m_offset;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class CompoundCollider2D;
|
||||
|
||||
using CompoundCollider2DConstRef = ObjectRef<const CompoundCollider2D>;
|
||||
using CompoundCollider2DRef = ObjectRef<CompoundCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API CompoundCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
CompoundCollider2D(std::vector<Collider2DRef> geoms);
|
||||
|
||||
float ComputeMomentOfInertia(float mass) const override;
|
||||
|
||||
inline const std::vector<Collider2DRef>& GetGeoms() const;
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static CompoundCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
||||
|
||||
std::vector<Collider2DRef> m_geoms;
|
||||
};
|
||||
|
||||
class ConvexCollider2D;
|
||||
|
||||
using ConvexCollider2DConstRef = ObjectRef<const ConvexCollider2D>;
|
||||
using ConvexCollider2DRef = ObjectRef<ConvexCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API ConvexCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
ConvexCollider2D(SparsePtr<const Vector2f> vertices, std::size_t vertexCount, float radius = 0.f);
|
||||
|
||||
float ComputeMomentOfInertia(float mass) const override;
|
||||
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static ConvexCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
||||
|
||||
std::vector<Vector2d> m_vertices;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class NullCollider2D;
|
||||
|
||||
using NullCollider2DConstRef = ObjectRef<const NullCollider2D>;
|
||||
@@ -138,14 +186,14 @@ namespace Nz
|
||||
public:
|
||||
NullCollider2D() = default;
|
||||
|
||||
float ComputeInertialMatrix(float mass) const override;
|
||||
float ComputeMomentOfInertia(float mass) const override;
|
||||
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static NullCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
||||
};
|
||||
|
||||
class SegmentCollider2D;
|
||||
@@ -158,7 +206,7 @@ namespace Nz
|
||||
public:
|
||||
inline SegmentCollider2D(const Vector2f& first, const Vector2f& second, float thickness = 1.f);
|
||||
|
||||
float ComputeInertialMatrix(float mass) const override;
|
||||
float ComputeMomentOfInertia(float mass) const override;
|
||||
|
||||
inline const Vector2f& GetFirstPoint() const;
|
||||
inline float GetLength() const;
|
||||
@@ -168,7 +216,7 @@ namespace Nz
|
||||
template<typename... Args> static SegmentCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
||||
|
||||
Vector2f m_first;
|
||||
Vector2f m_second;
|
||||
|
||||
Reference in New Issue
Block a user