Merge branch 'master' of https://github.com/DigitalPulseSoftware/NazaraEngine
This commit is contained in:
commit
d0e33225b7
|
|
@ -65,7 +65,7 @@ namespace Nz
|
||||||
NazaraSignal(OnColliderRelease, const Collider2D* /*collider*/);
|
NazaraSignal(OnColliderRelease, const Collider2D* /*collider*/);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const = 0;
|
virtual std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const = 0;
|
||||||
|
|
||||||
bool m_trigger;
|
bool m_trigger;
|
||||||
UInt32 m_categoryMask;
|
UInt32 m_categoryMask;
|
||||||
|
|
@ -74,7 +74,7 @@ namespace Nz
|
||||||
UInt32 m_collisionMask;
|
UInt32 m_collisionMask;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual std::vector<cpShape*> GenerateShapes(RigidBody2D* body) const;
|
virtual std::size_t GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const;
|
||||||
|
|
||||||
static Collider2DLibrary::LibraryMap s_library;
|
static Collider2DLibrary::LibraryMap s_library;
|
||||||
};
|
};
|
||||||
|
|
@ -100,7 +100,7 @@ namespace Nz
|
||||||
template<typename... Args> static BoxCollider2DRef New(Args&&... args);
|
template<typename... Args> static BoxCollider2DRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||||
|
|
||||||
Rectf m_rect;
|
Rectf m_rect;
|
||||||
float m_radius;
|
float m_radius;
|
||||||
|
|
@ -125,7 +125,7 @@ namespace Nz
|
||||||
template<typename... Args> static CircleCollider2DRef New(Args&&... args);
|
template<typename... Args> static CircleCollider2DRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||||
|
|
||||||
Vector2f m_offset;
|
Vector2f m_offset;
|
||||||
float m_radius;
|
float m_radius;
|
||||||
|
|
@ -153,8 +153,8 @@ namespace Nz
|
||||||
template<typename... Args> static CompoundCollider2DRef New(Args&&... args);
|
template<typename... Args> static CompoundCollider2DRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||||
std::vector<cpShape*> GenerateShapes(RigidBody2D* body) const override;
|
std::size_t GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||||
|
|
||||||
std::vector<Collider2DRef> m_geoms;
|
std::vector<Collider2DRef> m_geoms;
|
||||||
bool m_doesOverrideCollisionProperties;
|
bool m_doesOverrideCollisionProperties;
|
||||||
|
|
@ -178,7 +178,7 @@ namespace Nz
|
||||||
template<typename... Args> static ConvexCollider2DRef New(Args&&... args);
|
template<typename... Args> static ConvexCollider2DRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||||
|
|
||||||
std::vector<Vector2d> m_vertices;
|
std::vector<Vector2d> m_vertices;
|
||||||
float m_radius;
|
float m_radius;
|
||||||
|
|
@ -201,7 +201,7 @@ namespace Nz
|
||||||
template<typename... Args> static NullCollider2DRef New(Args&&... args);
|
template<typename... Args> static NullCollider2DRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SegmentCollider2D;
|
class SegmentCollider2D;
|
||||||
|
|
@ -225,7 +225,7 @@ namespace Nz
|
||||||
template<typename... Args> static SegmentCollider2DRef New(Args&&... args);
|
template<typename... Args> static SegmentCollider2DRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const override;
|
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||||
|
|
||||||
Vector2f m_first;
|
Vector2f m_first;
|
||||||
Vector2f m_second;
|
Vector2f m_second;
|
||||||
|
|
|
||||||
|
|
@ -11,20 +11,21 @@ namespace Nz
|
||||||
{
|
{
|
||||||
Collider2D::~Collider2D() = default;
|
Collider2D::~Collider2D() = default;
|
||||||
|
|
||||||
std::vector<cpShape*> Collider2D::GenerateShapes(RigidBody2D* body) const
|
std::size_t Collider2D::GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||||
{
|
{
|
||||||
std::vector<cpShape*> shapes;
|
std::size_t shapeCount = CreateShapes(body, shapes);
|
||||||
CreateShapes(body, shapes);
|
|
||||||
|
|
||||||
cpShapeFilter filter = cpShapeFilterNew(m_collisionGroup, m_categoryMask, m_collisionMask);
|
cpShapeFilter filter = cpShapeFilterNew(m_collisionGroup, m_categoryMask, m_collisionMask);
|
||||||
for (cpShape* shape : shapes)
|
for (std::size_t i = shapes->size() - shapeCount; i < shapes->size(); ++i)
|
||||||
{
|
{
|
||||||
|
cpShape* shape = (*shapes)[i];
|
||||||
|
|
||||||
cpShapeSetFilter(shape, filter);
|
cpShapeSetFilter(shape, filter);
|
||||||
cpShapeSetCollisionType(shape, m_collisionId);
|
cpShapeSetCollisionType(shape, m_collisionId);
|
||||||
cpShapeSetSensor(shape, (m_trigger) ? cpTrue : cpFalse);
|
cpShapeSetSensor(shape, (m_trigger) ? cpTrue : cpFalse);
|
||||||
}
|
}
|
||||||
|
|
||||||
return shapes;
|
return shapeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************** BoxCollider2D *********************************/
|
/******************************** BoxCollider2D *********************************/
|
||||||
|
|
@ -50,9 +51,10 @@ namespace Nz
|
||||||
return ColliderType2D_Box;
|
return ColliderType2D_Box;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BoxCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const
|
std::size_t BoxCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||||
{
|
{
|
||||||
shapes.push_back(cpBoxShapeNew2(body->GetHandle(), cpBBNew(m_rect.x, m_rect.y, m_rect.x + m_rect.width, m_rect.y + m_rect.height), m_radius));
|
shapes->push_back(cpBoxShapeNew2(body->GetHandle(), cpBBNew(m_rect.x, m_rect.y, m_rect.x + m_rect.width, m_rect.y + m_rect.height), m_radius));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************** CircleCollider2D *********************************/
|
/******************************** CircleCollider2D *********************************/
|
||||||
|
|
@ -73,9 +75,10 @@ namespace Nz
|
||||||
return ColliderType2D_Circle;
|
return ColliderType2D_Circle;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CircleCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const
|
std::size_t CircleCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||||
{
|
{
|
||||||
shapes.push_back(cpCircleShapeNew(body->GetHandle(), m_radius, cpv(m_offset.x, m_offset.y)));
|
shapes->push_back(cpCircleShapeNew(body->GetHandle(), m_radius, cpv(m_offset.x, m_offset.y)));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************** CompoundCollider2D *********************************/
|
/******************************** CompoundCollider2D *********************************/
|
||||||
|
|
@ -102,24 +105,29 @@ namespace Nz
|
||||||
return ColliderType2D_Compound;
|
return ColliderType2D_Compound;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompoundCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const
|
std::size_t CompoundCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||||
{
|
{
|
||||||
// Since C++ does not allow protected call from other objects, we have to be a friend of Collider2D, yay
|
// Since C++ does not allow protected call from other objects, we have to be a friend of Collider2D, yay
|
||||||
|
|
||||||
|
std::size_t shapeCount = 0;
|
||||||
for (const auto& geom : m_geoms)
|
for (const auto& geom : m_geoms)
|
||||||
geom->CreateShapes(body, shapes);
|
shapeCount += geom->CreateShapes(body, shapes);
|
||||||
|
|
||||||
|
return shapeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<cpShape*> CompoundCollider2D::GenerateShapes(RigidBody2D* body) const
|
std::size_t CompoundCollider2D::GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||||
{
|
{
|
||||||
// This is our parent's default behavior
|
// This is our parent's default behavior
|
||||||
if (m_doesOverrideCollisionProperties)
|
if (m_doesOverrideCollisionProperties)
|
||||||
return Collider2D::GenerateShapes(body);
|
return Collider2D::GenerateShapes(body, shapes);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::vector<cpShape*> shapes;
|
std::size_t shapeCount = 0;
|
||||||
CreateShapes(body, shapes);
|
for (const auto& geom : m_geoms)
|
||||||
|
shapeCount += geom->GenerateShapes(body, shapes);
|
||||||
|
|
||||||
return shapes;
|
return shapeCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,9 +153,10 @@ namespace Nz
|
||||||
return ColliderType2D_Convex;
|
return ColliderType2D_Convex;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConvexCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const
|
std::size_t ConvexCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||||
{
|
{
|
||||||
shapes.push_back(cpPolyShapeNew(body->GetHandle(), int(m_vertices.size()), reinterpret_cast<const cpVect*>(m_vertices.data()), cpTransformIdentity, m_radius));
|
shapes->push_back(cpPolyShapeNew(body->GetHandle(), int(m_vertices.size()), reinterpret_cast<const cpVect*>(m_vertices.data()), cpTransformIdentity, m_radius));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/********************************* NullCollider2D **********************************/
|
/********************************* NullCollider2D **********************************/
|
||||||
|
|
@ -162,8 +171,9 @@ namespace Nz
|
||||||
return (mass > 0.f) ? 1.f : 0.f; //< Null inertia is only possible for static/kinematic objects
|
return (mass > 0.f) ? 1.f : 0.f; //< Null inertia is only possible for static/kinematic objects
|
||||||
}
|
}
|
||||||
|
|
||||||
void NullCollider2D::CreateShapes(RigidBody2D* /*body*/, std::vector<cpShape*>& /*shapes*/) const
|
std::size_t NullCollider2D::CreateShapes(RigidBody2D* /*body*/, std::vector<cpShape*>* /*shapes*/) const
|
||||||
{
|
{
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************** SegmentCollider2D *********************************/
|
/******************************** SegmentCollider2D *********************************/
|
||||||
|
|
@ -178,8 +188,9 @@ namespace Nz
|
||||||
return ColliderType2D_Segment;
|
return ColliderType2D_Segment;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SegmentCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>& shapes) const
|
std::size_t SegmentCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
|
||||||
{
|
{
|
||||||
shapes.push_back(cpSegmentShapeNew(body->GetHandle(), cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness));
|
shapes->push_back(cpSegmentShapeNew(body->GetHandle(), cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ namespace Nz
|
||||||
NazaraAssert(m_world, "Invalid world");
|
NazaraAssert(m_world, "Invalid world");
|
||||||
|
|
||||||
m_handle = Create(mass);
|
m_handle = Create(mass);
|
||||||
SetGeom(geom);
|
SetGeom(std::move(geom));
|
||||||
}
|
}
|
||||||
|
|
||||||
RigidBody2D::RigidBody2D(const RigidBody2D& object) :
|
RigidBody2D::RigidBody2D(const RigidBody2D& object) :
|
||||||
|
|
@ -311,7 +311,7 @@ namespace Nz
|
||||||
else
|
else
|
||||||
m_geom = NullCollider2D::New();
|
m_geom = NullCollider2D::New();
|
||||||
|
|
||||||
m_shapes = m_geom->GenerateShapes(this);
|
m_geom->GenerateShapes(this, &m_shapes);
|
||||||
|
|
||||||
cpSpace* space = m_world->GetHandle();
|
cpSpace* space = m_world->GetHandle();
|
||||||
for (cpShape* shape : m_shapes)
|
for (cpShape* shape : m_shapes)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue