diff --git a/ChangeLog.md b/ChangeLog.md index 44ab35911..d2c5b2d83 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -176,6 +176,7 @@ Nazara Engine: - ENet DisconnectLater now reflects libenet behavior (and is waiting for unreliable commands to be sent before disconnecting for good) - ⚠ Collider3D::ForEachPolygon now takes a void(Vector3f\*, std::size_t) callback (instead of void(float\*, std::size_t)) - Added Collider2D::ForEachPolygon method +- Added RigidBody::[Get|Set]PositionOffset allowing set an offset between body logic position and body physics position (center of mass position) Nazara Development Kit: - Added ImageWidget (#139) @@ -248,6 +249,7 @@ Nazara Development Kit: - Add missing `recomputeMoment` parameter to PhysicsComponent2D::SetMass - Added possibility of disabling synchronization between PhysicsComponent2D and NodeComponent - Fixed GraphicsComponent not invalidating render queue on material change (causing crashes or visual errors) +- Added CollisionComponent2D::SetGeomOffset and CollisionComponent2D::Recenter # 0.4: diff --git a/SDK/include/NDK/Components/CollisionComponent2D.hpp b/SDK/include/NDK/Components/CollisionComponent2D.hpp index ef8043629..a5da3b8c2 100644 --- a/SDK/include/NDK/Components/CollisionComponent2D.hpp +++ b/SDK/include/NDK/Components/CollisionComponent2D.hpp @@ -20,8 +20,9 @@ namespace Ndk class NDK_API CollisionComponent2D : public Component { - friend class PhysicsSystem2D; friend class ConstraintComponent2D; + friend class PhysicsComponent2D; + friend class PhysicsSystem2D; public: CollisionComponent2D(Nz::Collider2DRef geom = Nz::Collider2DRef()); @@ -47,6 +48,7 @@ namespace Ndk Nz::RigidBody2D* GetRigidBody(); const Nz::RigidBody2D* GetRigidBody() const; Nz::RigidBody2D* GetStaticBody(); + const Nz::RigidBody2D* GetStaticBody() const; void OnAttached() override; void OnComponentAttached(BaseComponent& component) override; diff --git a/SDK/include/NDK/Components/CollisionComponent2D.inl b/SDK/include/NDK/Components/CollisionComponent2D.inl index 010780443..1bc9f0978 100644 --- a/SDK/include/NDK/Components/CollisionComponent2D.inl +++ b/SDK/include/NDK/Components/CollisionComponent2D.inl @@ -52,13 +52,13 @@ namespace Ndk return *this; } - /*! - * \brief Gets the static body used by the entity - * \return A pointer to the entity - */ - inline Nz::RigidBody2D* CollisionComponent2D::GetStaticBody() { return m_staticBody.get(); } + + inline const Nz::RigidBody2D* CollisionComponent2D::GetStaticBody() const + { + return m_staticBody.get(); + } } diff --git a/SDK/include/NDK/Systems/DebugSystem.hpp b/SDK/include/NDK/Systems/DebugSystem.hpp index 0cb4839b7..362b59f32 100644 --- a/SDK/include/NDK/Systems/DebugSystem.hpp +++ b/SDK/include/NDK/Systems/DebugSystem.hpp @@ -26,7 +26,7 @@ namespace Ndk private: Nz::InstancedRenderableRef GenerateBox(Nz::Boxf box); - Nz::InstancedRenderableRef GenerateCollision2DMesh(Entity* entity, Nz::Vector3f* origin); + Nz::InstancedRenderableRef GenerateCollision2DMesh(Entity* entity, Nz::Vector3f* offset); Nz::InstancedRenderableRef GenerateCollision3DMesh(Entity* entity); Nz::MaterialRef GetCollisionMaterial(); diff --git a/SDK/src/NDK/Components/PhysicsComponent2D.cpp b/SDK/src/NDK/Components/PhysicsComponent2D.cpp index 62cd922a4..f9feaff6e 100644 --- a/SDK/src/NDK/Components/PhysicsComponent2D.cpp +++ b/SDK/src/NDK/Components/PhysicsComponent2D.cpp @@ -31,9 +31,17 @@ namespace Ndk Nz::PhysWorld2D& world = entityWorld->GetSystem().GetPhysWorld(); + Nz::Vector2f positionOffset; + Nz::Collider2DRef geom; if (m_entity->HasComponent()) - geom = m_entity->GetComponent().GetGeom(); + { + const CollisionComponent2D& entityCollision = m_entity->GetComponent(); + geom = entityCollision.GetGeom(); + positionOffset = entityCollision.GetStaticBody()->GetPositionOffset(); //< Calling GetGeomOffset would retrieve current component which is not yet initialized + } + else + positionOffset = Nz::Vector2f::Zero(); Nz::Matrix4f matrix; if (m_entity->HasComponent()) @@ -42,6 +50,7 @@ namespace Ndk matrix.MakeIdentity(); m_object = std::make_unique(&world, 1.f, geom); + m_object->SetPositionOffset(positionOffset); m_object->SetPosition(Nz::Vector2f(matrix.GetTranslation())); m_object->SetUserdata(reinterpret_cast(static_cast(m_entity->GetId()))); } diff --git a/SDK/src/NDK/Systems/DebugSystem.cpp b/SDK/src/NDK/Systems/DebugSystem.cpp index df429dd1d..54295f611 100644 --- a/SDK/src/NDK/Systems/DebugSystem.cpp +++ b/SDK/src/NDK/Systems/DebugSystem.cpp @@ -234,10 +234,10 @@ namespace Ndk const Nz::Boxf& obb = entityGfx.GetAABB(); CollisionComponent2D& entityCollision2D = entity->GetComponent(); - Nz::Vector3f origin; - Nz::InstancedRenderableRef renderable = GenerateCollision2DMesh(entity, &origin); + Nz::Vector3f offset; + Nz::InstancedRenderableRef renderable = GenerateCollision2DMesh(entity, &offset); if (renderable) - entityGfx.Attach(renderable, Nz::Matrix4f::Translate(origin - entityNode.GetPosition() + entityCollision2D.GetGeomOffset()), DebugDrawOrder); + entityGfx.Attach(renderable, Nz::Matrix4f::Translate(offset), DebugDrawOrder); entityDebug.UpdateDebugRenderable(option, std::move(renderable)); break; @@ -318,7 +318,7 @@ namespace Ndk return model; } - Nz::InstancedRenderableRef DebugSystem::GenerateCollision2DMesh(Entity* entity, Nz::Vector3f* origin) + Nz::InstancedRenderableRef DebugSystem::GenerateCollision2DMesh(Entity* entity, Nz::Vector3f* offset) { if (entity->HasComponent()) { @@ -375,9 +375,12 @@ namespace Ndk // Find center of mass if (entity->HasComponent()) - *origin = entity->GetComponent().GetMassCenter(Nz::CoordSys_Global); + { + const PhysicsComponent2D& entityPhys = entity->GetComponent(); + *offset = entityPhys.GetMassCenter(Nz::CoordSys_Global) - entityPhys.GetPosition(); // GetPosition already takes GetGeomOffset into account + } else - *origin = entity->GetComponent().GetPosition(Nz::CoordSys_Global); + *offset = entityCollision.GetGeomOffset(); return model; } diff --git a/SDK/src/NDK/Systems/PhysicsSystem2D.cpp b/SDK/src/NDK/Systems/PhysicsSystem2D.cpp index 6579bb1fb..dac509128 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem2D.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem2D.cpp @@ -213,7 +213,7 @@ namespace Ndk Nz::Vector2f newPosition = Nz::Vector2f(node.GetPosition(Nz::CoordSys_Global)); // To move static objects and ensure their collisions, we have to specify them a velocity - // (/!\: the physical motor does not apply the speed on static objects) + // (/!\: the physical engine does not apply the speed on static objects) if (newPosition != oldPosition) { body->SetPosition(newPosition); @@ -222,8 +222,7 @@ namespace Ndk else body->SetVelocity(Nz::Vector2f::Zero()); -/* - if (newRotation != oldRotation) + /*if (newRotation != oldRotation) { Nz::Quaternionf transition = newRotation * oldRotation.GetConjugate(); Nz::EulerAnglesf angles = transition.ToEulerAngles(); @@ -235,8 +234,7 @@ namespace Ndk physObj->SetAngularVelocity(angularVelocity); } else - physObj->SetAngularVelocity(Nz::Vector3f::Zero()); -*/ + physObj->SetAngularVelocity(Nz::Vector3f::Zero());*/ } }