ChipmunkPhysics2D/RigidBody2D: Add unique body index and remove userdata
This commit is contained in:
@@ -14,11 +14,13 @@
|
||||
#include <Nazara/Core/Time.hpp>
|
||||
#include <Nazara/Math/Angle.hpp>
|
||||
#include <Nazara/Math/Vector2.hpp>
|
||||
#include <NazaraUtils/Bitset.hpp>
|
||||
#include <NazaraUtils/FunctionRef.hpp>
|
||||
#include <NazaraUtils/Signal.hpp>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
struct cpCollisionHandler;
|
||||
struct cpSpace;
|
||||
@@ -51,10 +53,10 @@ namespace Nz
|
||||
|
||||
ChipmunkPhysWorld2D();
|
||||
ChipmunkPhysWorld2D(const ChipmunkPhysWorld2D&) = delete;
|
||||
ChipmunkPhysWorld2D(ChipmunkPhysWorld2D&&) = delete; ///TODO
|
||||
ChipmunkPhysWorld2D(ChipmunkPhysWorld2D&&) = delete;
|
||||
~ChipmunkPhysWorld2D();
|
||||
|
||||
void DebugDraw(const DebugDrawOptions& options, bool drawShapes = true, bool drawConstraints = true, bool drawCollisions = true);
|
||||
void DebugDraw(const DebugDrawOptions& options, bool drawShapes = true, bool drawConstraints = true, bool drawCollisions = true) const;
|
||||
|
||||
float GetDamping() const;
|
||||
Vector2f GetGravity() const;
|
||||
@@ -135,32 +137,27 @@ namespace Nz
|
||||
NazaraSignal(OnPhysWorld2DPostStep, const ChipmunkPhysWorld2D* /*physWorld*/, float /*invStepCount*/);
|
||||
|
||||
private:
|
||||
void InitCallbacks(cpCollisionHandler* handler, Callback callbacks);
|
||||
|
||||
using PostStep = std::function<void(ChipmunkRigidBody2D* body)>;
|
||||
|
||||
void OnRigidBodyMoved(ChipmunkRigidBody2D* oldPointer, ChipmunkRigidBody2D* newPointer);
|
||||
void OnRigidBodyRelease(ChipmunkRigidBody2D* rigidBody);
|
||||
static constexpr std::size_t FreeBodyIdGrowRate = 256;
|
||||
|
||||
void RegisterPostStep(ChipmunkRigidBody2D* rigidBody, PostStep&& func);
|
||||
|
||||
struct PostStepContainer
|
||||
{
|
||||
NazaraSlot(ChipmunkRigidBody2D, OnRigidBody2DMove, onMovedSlot);
|
||||
NazaraSlot(ChipmunkRigidBody2D, OnRigidBody2DRelease, onReleaseSlot);
|
||||
|
||||
std::vector<PostStep> funcs;
|
||||
};
|
||||
|
||||
static_assert(std::is_nothrow_move_constructible<PostStepContainer>::value, "PostStepContainer should be noexcept MoveConstructible");
|
||||
void DeferBodyAction(ChipmunkRigidBody2D& rigidBody, PostStep&& func);
|
||||
void InitCallbacks(cpCollisionHandler* handler, Callback callbacks);
|
||||
inline UInt32 RegisterBody(ChipmunkRigidBody2D& rigidBody);
|
||||
inline void UnregisterBody(UInt32 bodyIndex);
|
||||
inline void UpdateBodyPointer(ChipmunkRigidBody2D& rigidBody);
|
||||
|
||||
std::size_t m_maxStepCount;
|
||||
std::unordered_map<cpCollisionHandler*, std::unique_ptr<Callback>> m_callbacks;
|
||||
std::unordered_map<ChipmunkRigidBody2D*, PostStepContainer> m_rigidPostSteps;
|
||||
std::unordered_map<UInt32, std::vector<PostStep>> m_rigidBodyPostSteps;
|
||||
std::vector<ChipmunkRigidBody2D*> m_bodies;
|
||||
cpSpace* m_handle;
|
||||
Bitset<UInt64> m_freeBodyIndices;
|
||||
Time m_stepSize;
|
||||
Time m_timestepAccumulator;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/ChipmunkPhysics2D/ChipmunkPhysWorld2D.inl>
|
||||
|
||||
#endif // NAZARA_CHIPMUNKPHYSICS2D_CHIPMUNKPHYSWORLD2D_HPP
|
||||
|
||||
47
include/Nazara/ChipmunkPhysics2D/ChipmunkPhysWorld2D.inl
Normal file
47
include/Nazara/ChipmunkPhysics2D/ChipmunkPhysWorld2D.inl
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - ChipmunkPhysics2D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <cassert>
|
||||
#include <Nazara/ChipmunkPhysics2D/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline UInt32 ChipmunkPhysWorld2D::RegisterBody(ChipmunkRigidBody2D& rigidBody)
|
||||
{
|
||||
std::size_t bodyIndex = m_freeBodyIndices.FindFirst();
|
||||
if (bodyIndex == m_freeBodyIndices.npos)
|
||||
{
|
||||
bodyIndex = m_freeBodyIndices.GetSize();
|
||||
m_freeBodyIndices.Resize(bodyIndex + FreeBodyIdGrowRate, true);
|
||||
m_bodies.resize(m_freeBodyIndices.GetSize());
|
||||
}
|
||||
|
||||
assert(m_freeBodyIndices.Test(bodyIndex));
|
||||
m_freeBodyIndices.Set(bodyIndex, false);
|
||||
|
||||
assert(!m_bodies[bodyIndex]);
|
||||
m_bodies[bodyIndex] = &rigidBody;
|
||||
|
||||
return SafeCast<UInt32>(bodyIndex);
|
||||
}
|
||||
|
||||
inline void ChipmunkPhysWorld2D::UnregisterBody(UInt32 bodyIndex)
|
||||
{
|
||||
assert(!m_freeBodyIndices.Test(bodyIndex));
|
||||
m_freeBodyIndices.Set(bodyIndex, true);
|
||||
assert(m_bodies[bodyIndex]);
|
||||
m_bodies[bodyIndex] = nullptr;
|
||||
|
||||
m_rigidBodyPostSteps.erase(bodyIndex);
|
||||
}
|
||||
|
||||
inline void ChipmunkPhysWorld2D::UpdateBodyPointer(ChipmunkRigidBody2D& rigidBody)
|
||||
{
|
||||
UInt32 bodyIndex = rigidBody.GetBodyIndex();
|
||||
assert(m_bodies[bodyIndex]);
|
||||
m_bodies[bodyIndex] = &rigidBody;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/ChipmunkPhysics2D/DebugOff.hpp>
|
||||
@@ -53,6 +53,7 @@ namespace Nz
|
||||
|
||||
Rectf GetAABB() const;
|
||||
RadianAnglef GetAngularVelocity() const;
|
||||
inline UInt32 GetBodyIndex() const;
|
||||
float GetElasticity(std::size_t shapeIndex = 0) const;
|
||||
float GetFriction(std::size_t shapeIndex = 0) const;
|
||||
inline const std::shared_ptr<ChipmunkCollider2D>& GetGeom() const;
|
||||
@@ -66,7 +67,6 @@ namespace Nz
|
||||
inline std::size_t GetShapeCount() const;
|
||||
inline std::size_t GetShapeIndex(cpShape* shape) const;
|
||||
Vector2f GetSurfaceVelocity(std::size_t shapeIndex = 0) const;
|
||||
inline void* GetUserdata() const;
|
||||
Vector2f GetVelocity() const;
|
||||
inline const VelocityFunc& GetVelocityFunction() const;
|
||||
inline ChipmunkPhysWorld2D* GetWorld() const;
|
||||
@@ -93,7 +93,6 @@ namespace Nz
|
||||
void SetSurfaceVelocity(const Vector2f& surfaceVelocity);
|
||||
void SetSurfaceVelocity(std::size_t shapeIndex, const Vector2f& surfaceVelocity);
|
||||
void SetStatic(bool setStaticBody = true);
|
||||
inline void SetUserdata(void* ud);
|
||||
void SetVelocity(const Vector2f& velocity);
|
||||
void SetVelocityFunction(VelocityFunc velocityFunc);
|
||||
|
||||
@@ -104,11 +103,9 @@ namespace Nz
|
||||
void Wakeup();
|
||||
|
||||
ChipmunkRigidBody2D& operator=(const ChipmunkRigidBody2D& object);
|
||||
ChipmunkRigidBody2D& operator=(ChipmunkRigidBody2D&& object);
|
||||
|
||||
NazaraSignal(OnRigidBody2DMove, ChipmunkRigidBody2D* /*oldPointer*/, ChipmunkRigidBody2D* /*newPointer*/);
|
||||
NazaraSignal(OnRigidBody2DRelease, ChipmunkRigidBody2D* /*rigidBody*/);
|
||||
ChipmunkRigidBody2D& operator=(ChipmunkRigidBody2D&& object) noexcept;
|
||||
|
||||
static constexpr UInt32 InvalidBodyIndex = std::numeric_limits<UInt32>::max();
|
||||
static constexpr std::size_t InvalidShapeIndex = std::numeric_limits<std::size_t>::max();
|
||||
|
||||
struct CommonSettings
|
||||
@@ -151,6 +148,7 @@ namespace Nz
|
||||
void Destroy();
|
||||
|
||||
private:
|
||||
void DestroyBody();
|
||||
void RegisterToSpace();
|
||||
void UnregisterFromSpace();
|
||||
|
||||
@@ -161,7 +159,7 @@ namespace Nz
|
||||
std::shared_ptr<ChipmunkCollider2D> m_geom;
|
||||
MovablePtr<cpBody> m_handle;
|
||||
MovablePtr<ChipmunkPhysWorld2D> m_world;
|
||||
MovablePtr<void> m_userData;
|
||||
UInt32 m_bodyIndex;
|
||||
Vector2f m_positionOffset;
|
||||
VelocityFunc m_velocityFunc;
|
||||
bool m_isRegistered;
|
||||
|
||||
@@ -18,8 +18,6 @@ namespace Nz
|
||||
|
||||
inline ChipmunkRigidBody2D::~ChipmunkRigidBody2D()
|
||||
{
|
||||
OnRigidBody2DRelease(this);
|
||||
|
||||
Destroy();
|
||||
}
|
||||
|
||||
@@ -33,6 +31,11 @@ namespace Nz
|
||||
return AddImpulse(impulse, GetMassCenter(coordSys), coordSys);
|
||||
}
|
||||
|
||||
inline UInt32 ChipmunkRigidBody2D::GetBodyIndex() const
|
||||
{
|
||||
return m_bodyIndex;
|
||||
}
|
||||
|
||||
inline const std::shared_ptr<ChipmunkCollider2D>& ChipmunkRigidBody2D::GetGeom() const
|
||||
{
|
||||
return m_geom;
|
||||
@@ -67,11 +70,6 @@ namespace Nz
|
||||
return m_shapes.size();
|
||||
}
|
||||
|
||||
inline void* ChipmunkRigidBody2D::GetUserdata() const
|
||||
{
|
||||
return m_userData;
|
||||
}
|
||||
|
||||
inline const ChipmunkRigidBody2D::VelocityFunc& ChipmunkRigidBody2D::GetVelocityFunction() const
|
||||
{
|
||||
return m_velocityFunc;
|
||||
@@ -96,11 +94,6 @@ namespace Nz
|
||||
{
|
||||
return m_isStatic;
|
||||
}
|
||||
|
||||
inline void ChipmunkRigidBody2D::SetUserdata(void* ud)
|
||||
{
|
||||
m_userData = ud;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/ChipmunkPhysics2D/DebugOff.hpp>
|
||||
|
||||
Reference in New Issue
Block a user