Physics2D: Add support for SegmentCollider2D
This commit is contained in:
parent
950068a60d
commit
ac2193e0c2
|
|
@ -125,6 +125,33 @@ namespace Nz
|
|||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
};
|
||||
|
||||
class SegmentCollider2D;
|
||||
|
||||
using SegmentCollider2DConstRef = ObjectRef<const SegmentCollider2D>;
|
||||
using SegmentCollider2DRef = ObjectRef<SegmentCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API SegmentCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
inline SegmentCollider2D(const Vector2f& first, const Vector2f& second, float thickness = 1.f);
|
||||
|
||||
float ComputeInertialMatrix(float mass) const override;
|
||||
|
||||
inline const Vector2f& GetFirstPoint() const;
|
||||
inline float GetLength() const;
|
||||
inline const Vector2f& GetSecondPoint() const;
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static SegmentCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
|
||||
Vector2f m_first;
|
||||
Vector2f m_second;
|
||||
float m_thickness;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Physics2D/Collider2D.inl>
|
||||
|
|
|
|||
|
|
@ -49,6 +49,36 @@ namespace Nz
|
|||
return object.release();
|
||||
}
|
||||
|
||||
SegmentCollider2D::SegmentCollider2D(const Vector2f& first, const Vector2f& second, float thickness) :
|
||||
m_first(first),
|
||||
m_second(second),
|
||||
m_thickness(thickness)
|
||||
{
|
||||
}
|
||||
|
||||
inline const Vector2f& SegmentCollider2D::GetFirstPoint() const
|
||||
{
|
||||
return m_first;
|
||||
}
|
||||
|
||||
inline float SegmentCollider2D::GetLength() const
|
||||
{
|
||||
return m_first.Distance(m_second);
|
||||
}
|
||||
|
||||
inline const Vector2f& SegmentCollider2D::GetSecondPoint() const
|
||||
{
|
||||
return m_second;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
SegmentCollider2DRef SegmentCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<SegmentCollider2D> object(new SegmentCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Physics2D/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
namespace Nz
|
||||
{
|
||||
Collider2D::~Collider2D() = default;
|
||||
|
||||
|
||||
/******************************** BoxCollider2D *********************************/
|
||||
|
||||
BoxCollider2D::BoxCollider2D(const Vector2f& size, float radius) :
|
||||
|
|
@ -84,4 +84,25 @@ namespace Nz
|
|||
{
|
||||
return std::vector<cpShape*>();
|
||||
}
|
||||
|
||||
/******************************** SegmentCollider2D *********************************/
|
||||
|
||||
float SegmentCollider2D::ComputeInertialMatrix(float mass) const
|
||||
{
|
||||
return static_cast<float>(cpMomentForSegment(mass, cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness));
|
||||
}
|
||||
|
||||
ColliderType2D SegmentCollider2D::GetType() const
|
||||
{
|
||||
return ColliderType2D_Segment;
|
||||
}
|
||||
|
||||
std::vector<cpShape*> SegmentCollider2D::CreateShapes(RigidBody2D* body) const
|
||||
{
|
||||
std::vector<cpShape*> shapes;
|
||||
shapes.push_back(cpSegmentShapeNew(body->GetHandle(), cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness));
|
||||
|
||||
return shapes;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue