Upgrade Physics2D and Physics3D
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
#define NAZARA_COLLIDER2D_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/ObjectRef.hpp>
|
||||
#include <Nazara/Core/ObjectLibrary.hpp>
|
||||
#include <Nazara/Core/Signal.hpp>
|
||||
#include <Nazara/Core/SparsePtr.hpp>
|
||||
@@ -18,20 +17,15 @@
|
||||
#include <Nazara/Physics2D/Enums.hpp>
|
||||
#include <vector>
|
||||
|
||||
struct cpBody;
|
||||
struct cpShape;
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class Collider2D;
|
||||
class RigidBody2D;
|
||||
|
||||
using Collider2DConstRef = ObjectRef<const Collider2D>;
|
||||
using Collider2DLibrary = ObjectLibrary<Collider2D>;
|
||||
using Collider2DRef = ObjectRef<Collider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API Collider2D : public RefCounted
|
||||
class NAZARA_PHYSICS2D_API Collider2D
|
||||
{
|
||||
friend Collider2DLibrary;
|
||||
friend RigidBody2D;
|
||||
friend class CompoundCollider2D; //< See CompoundCollider2D::CreateShapes
|
||||
|
||||
@@ -74,7 +68,7 @@ namespace Nz
|
||||
NazaraSignal(OnColliderRelease, const Collider2D* /*collider*/);
|
||||
|
||||
protected:
|
||||
virtual std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const = 0;
|
||||
virtual std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const = 0;
|
||||
|
||||
UInt32 m_categoryMask;
|
||||
UInt32 m_collisionGroup;
|
||||
@@ -86,16 +80,9 @@ namespace Nz
|
||||
unsigned int m_collisionId;
|
||||
|
||||
private:
|
||||
virtual std::size_t GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const;
|
||||
|
||||
static Collider2DLibrary::LibraryMap s_library;
|
||||
virtual std::size_t GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const;
|
||||
};
|
||||
|
||||
class BoxCollider2D;
|
||||
|
||||
using BoxCollider2DConstRef = ObjectRef<const BoxCollider2D>;
|
||||
using BoxCollider2DRef = ObjectRef<BoxCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API BoxCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
@@ -110,20 +97,13 @@ namespace Nz
|
||||
inline Vector2f GetSize() const;
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static BoxCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
|
||||
|
||||
Rectf m_rect;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class CircleCollider2D;
|
||||
|
||||
using CircleCollider2DConstRef = ObjectRef<const CircleCollider2D>;
|
||||
using CircleCollider2DRef = ObjectRef<CircleCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API CircleCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
@@ -136,50 +116,36 @@ namespace Nz
|
||||
inline float GetRadius() const;
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static CircleCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t CreateShapes(cpBody* 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);
|
||||
CompoundCollider2D(std::vector<std::shared_ptr<Collider2D>> geoms);
|
||||
|
||||
Nz::Vector2f ComputeCenterOfMass() const override;
|
||||
float ComputeMomentOfInertia(float mass) const override;
|
||||
|
||||
inline bool DoesOverrideCollisionProperties() const;
|
||||
|
||||
inline const std::vector<Collider2DRef>& GetGeoms() const;
|
||||
inline const std::vector<std::shared_ptr<Collider2D>>& GetGeoms() const;
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
inline void OverridesCollisionProperties(bool shouldOverride);
|
||||
|
||||
template<typename... Args> static CompoundCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
|
||||
|
||||
std::vector<Collider2DRef> m_geoms;
|
||||
std::vector<std::shared_ptr<Collider2D>> m_geoms;
|
||||
bool m_doesOverrideCollisionProperties;
|
||||
};
|
||||
|
||||
class ConvexCollider2D;
|
||||
|
||||
using ConvexCollider2DConstRef = ObjectRef<const ConvexCollider2D>;
|
||||
using ConvexCollider2DRef = ObjectRef<ConvexCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API ConvexCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
@@ -191,20 +157,13 @@ namespace Nz
|
||||
ColliderType2D GetType() const override;
|
||||
inline const std::vector<Vector2d>& GetVertices() const;
|
||||
|
||||
template<typename... Args> static ConvexCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
|
||||
|
||||
std::vector<Vector2d> m_vertices;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class NullCollider2D;
|
||||
|
||||
using NullCollider2DConstRef = ObjectRef<const NullCollider2D>;
|
||||
using NullCollider2DRef = ObjectRef<NullCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API NullCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
@@ -215,17 +174,10 @@ namespace Nz
|
||||
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static NullCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
|
||||
};
|
||||
|
||||
class SegmentCollider2D;
|
||||
|
||||
using SegmentCollider2DConstRef = ObjectRef<const SegmentCollider2D>;
|
||||
using SegmentCollider2DRef = ObjectRef<SegmentCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API SegmentCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
@@ -243,10 +195,8 @@ namespace Nz
|
||||
inline float GetThickness() const;
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static SegmentCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
|
||||
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
|
||||
|
||||
Vector2f m_first;
|
||||
Vector2f m_firstNeighbor;
|
||||
|
||||
@@ -115,14 +115,6 @@ namespace Nz
|
||||
return m_rect.GetLengths();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
BoxCollider2DRef BoxCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<BoxCollider2D> object(new BoxCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
inline const Vector2f& CircleCollider2D::GetOffset() const
|
||||
{
|
||||
@@ -134,21 +126,13 @@ namespace Nz
|
||||
return m_radius;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
CircleCollider2DRef CircleCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<CircleCollider2D> object(new CircleCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
inline bool Nz::CompoundCollider2D::DoesOverrideCollisionProperties() const
|
||||
{
|
||||
return m_doesOverrideCollisionProperties;
|
||||
}
|
||||
|
||||
inline const std::vector<Collider2DRef>& CompoundCollider2D::GetGeoms() const
|
||||
inline const std::vector<std::shared_ptr<Collider2D>>& CompoundCollider2D::GetGeoms() const
|
||||
{
|
||||
return m_geoms;
|
||||
}
|
||||
@@ -158,37 +142,12 @@ namespace Nz
|
||||
m_doesOverrideCollisionProperties = shouldOverride;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
CompoundCollider2DRef CompoundCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<CompoundCollider2D> object(new CompoundCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
inline const std::vector<Vector2d>& ConvexCollider2D::GetVertices() const
|
||||
{
|
||||
return m_vertices;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
ConvexCollider2DRef ConvexCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<ConvexCollider2D> object(new ConvexCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NullCollider2DRef NullCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NullCollider2D> object(new NullCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
SegmentCollider2D::SegmentCollider2D(const Vector2f& first, const Vector2f& second, float thickness) :
|
||||
SegmentCollider2D(first, first, second, second, thickness)
|
||||
@@ -233,15 +192,6 @@ namespace Nz
|
||||
{
|
||||
return m_thickness;
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
@@ -9,16 +9,16 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
enum ColliderType2D
|
||||
enum class ColliderType2D
|
||||
{
|
||||
ColliderType2D_Box,
|
||||
ColliderType2D_Compound,
|
||||
ColliderType2D_Convex,
|
||||
ColliderType2D_Circle,
|
||||
ColliderType2D_Null,
|
||||
ColliderType2D_Segment,
|
||||
Box,
|
||||
Compound,
|
||||
Convex,
|
||||
Circle,
|
||||
Null,
|
||||
Segment,
|
||||
|
||||
ColliderType2D_Max = ColliderType2D_Segment
|
||||
Max = Segment
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Nz
|
||||
using VelocityFunc = std::function<void(RigidBody2D& body2D, const Nz::Vector2f& gravity, float damping, float deltaTime)>;
|
||||
|
||||
RigidBody2D(PhysWorld2D* world, float mass);
|
||||
RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom);
|
||||
RigidBody2D(PhysWorld2D* world, float mass, std::shared_ptr<Collider2D> geom);
|
||||
RigidBody2D(const RigidBody2D& object);
|
||||
RigidBody2D(RigidBody2D&& object) noexcept;
|
||||
~RigidBody2D();
|
||||
@@ -55,7 +55,7 @@ namespace Nz
|
||||
inline Vector2f GetCenterOfGravity(CoordSys coordSys = CoordSys_Local) const;
|
||||
float GetElasticity(std::size_t shapeIndex = 0) const;
|
||||
float GetFriction(std::size_t shapeIndex = 0) const;
|
||||
const Collider2DRef& GetGeom() const;
|
||||
const std::shared_ptr<Collider2D>& GetGeom() const;
|
||||
cpBody* GetHandle() const;
|
||||
float GetMass() const;
|
||||
Vector2f GetMassCenter(CoordSys coordSys = CoordSys_Local) const;
|
||||
@@ -84,7 +84,7 @@ namespace Nz
|
||||
void SetElasticity(std::size_t shapeIndex, float elasticity);
|
||||
void SetFriction(float friction);
|
||||
void SetFriction(std::size_t shapeIndex, float friction);
|
||||
void SetGeom(Collider2DRef geom, bool recomputeMoment = true, bool recomputeMassCenter = true);
|
||||
void SetGeom(std::shared_ptr<Collider2D> geom, bool recomputeMoment = true, bool recomputeMassCenter = true);
|
||||
void SetMass(float mass, bool recomputeMoment = true);
|
||||
void SetMassCenter(const Vector2f& center, CoordSys coordSys = CoordSys_Local);
|
||||
void SetMomentOfInertia(float moment);
|
||||
@@ -122,7 +122,7 @@ namespace Nz
|
||||
Vector2f m_positionOffset;
|
||||
VelocityFunc m_velocityFunc;
|
||||
std::vector<cpShape*> m_shapes;
|
||||
Collider2DRef m_geom;
|
||||
std::shared_ptr<Collider2D> m_geom;
|
||||
cpBody* m_handle;
|
||||
void* m_userData;
|
||||
PhysWorld2D* m_world;
|
||||
|
||||
Reference in New Issue
Block a user