From df2a029f30cf87c25f0e67deab3e7f2a39c2b293 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 1 Jan 2020 17:25:22 +0100 Subject: [PATCH] Physics2D/Collider2D: Add support for neighbors vertices --- ChangeLog.md | 1 + include/Nazara/Physics2D/Collider2D.hpp | 5 +++++ include/Nazara/Physics2D/Collider2D.inl | 17 +++++++++++++++++ src/Nazara/Physics2D/Collider2D.cpp | 5 ++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 656dc699c..8c74a6428 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -213,6 +213,7 @@ Nazara Engine: - Added ENetPeer::GetTotalPacketSent - ⚠ ENetHost::GetTotalReceivedPackets now returns the number of commands received (instead of the number of UDP packets received) - Added EmptyStream class, useful to measure how many bytes some writing operations will take +- SegmentCollider2D: Add support for neighbors (aka "ghost vertices"), allowing to prevent seams collisions Nazara Development Kit: - Added ImageWidget (#139) diff --git a/include/Nazara/Physics2D/Collider2D.hpp b/include/Nazara/Physics2D/Collider2D.hpp index e817457b8..d0f7fd608 100644 --- a/include/Nazara/Physics2D/Collider2D.hpp +++ b/include/Nazara/Physics2D/Collider2D.hpp @@ -230,13 +230,16 @@ namespace Nz { public: inline SegmentCollider2D(const Vector2f& first, const Vector2f& second, float thickness = 1.f); + inline SegmentCollider2D(const Vector2f& first, const Vector2f& firstNeighbor, const Vector2f& second, const Vector2f& secondNeighbor, float thickness = 1.f); Nz::Vector2f ComputeCenterOfMass() const override; float ComputeMomentOfInertia(float mass) const override; inline const Vector2f& GetFirstPoint() const; + inline const Vector2f& GetFirstPointNeighbor() const; inline float GetLength() const; inline const Vector2f& GetSecondPoint() const; + inline const Vector2f& GetSecondPointNeighbor() const; inline float GetThickness() const; ColliderType2D GetType() const override; @@ -246,7 +249,9 @@ namespace Nz std::size_t CreateShapes(RigidBody2D* body, std::vector* shapes) const override; Vector2f m_first; + Vector2f m_firstNeighbor; Vector2f m_second; + Vector2f m_secondNeighbor; float m_thickness; }; } diff --git a/include/Nazara/Physics2D/Collider2D.inl b/include/Nazara/Physics2D/Collider2D.inl index d347ee361..75696ce50 100644 --- a/include/Nazara/Physics2D/Collider2D.inl +++ b/include/Nazara/Physics2D/Collider2D.inl @@ -191,8 +191,15 @@ namespace Nz } SegmentCollider2D::SegmentCollider2D(const Vector2f& first, const Vector2f& second, float thickness) : + SegmentCollider2D(first, first, second, second, thickness) + { + } + + inline SegmentCollider2D::SegmentCollider2D(const Vector2f& first, const Vector2f& firstNeighbor, const Vector2f& second, const Vector2f& secondNeighbor, float thickness) : m_first(first), + m_firstNeighbor(firstNeighbor), m_second(second), + m_secondNeighbor(secondNeighbor), m_thickness(thickness) { } @@ -202,6 +209,11 @@ namespace Nz return m_first; } + inline const Vector2f& SegmentCollider2D::GetFirstPointNeighbor() const + { + return m_firstNeighbor; + } + inline float SegmentCollider2D::GetLength() const { return m_first.Distance(m_second); @@ -212,6 +224,11 @@ namespace Nz return m_second; } + inline const Vector2f& SegmentCollider2D::GetSecondPointNeighbor() const + { + return m_secondNeighbor; + } + inline float SegmentCollider2D::GetThickness() const { return m_thickness; diff --git a/src/Nazara/Physics2D/Collider2D.cpp b/src/Nazara/Physics2D/Collider2D.cpp index 3a67797bd..16587a092 100644 --- a/src/Nazara/Physics2D/Collider2D.cpp +++ b/src/Nazara/Physics2D/Collider2D.cpp @@ -298,7 +298,10 @@ namespace Nz std::size_t SegmentCollider2D::CreateShapes(RigidBody2D* body, std::vector* shapes) const { - shapes->push_back(cpSegmentShapeNew(body->GetHandle(), cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness)); + cpShape* segment = cpSegmentShapeNew(body->GetHandle(), cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness); + cpSegmentShapeSetNeighbors(segment, cpv(m_firstNeighbor.x, m_firstNeighbor.y), cpv(m_secondNeighbor.x, m_secondNeighbor.y)); + + shapes->push_back(segment); return 1; } }