Physics2D: Fix some last stuffs

This commit is contained in:
Lynix 2019-03-26 22:01:10 +01:00
parent ae20ad6b65
commit 4821eb14a7
7 changed files with 33 additions and 19 deletions

View File

@ -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:

View File

@ -20,8 +20,9 @@ namespace Ndk
class NDK_API CollisionComponent2D : public Component<CollisionComponent2D>
{
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;

View File

@ -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();
}
}

View File

@ -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();

View File

@ -31,9 +31,17 @@ namespace Ndk
Nz::PhysWorld2D& world = entityWorld->GetSystem<PhysicsSystem2D>().GetPhysWorld();
Nz::Vector2f positionOffset;
Nz::Collider2DRef geom;
if (m_entity->HasComponent<CollisionComponent2D>())
geom = m_entity->GetComponent<CollisionComponent2D>().GetGeom();
{
const CollisionComponent2D& entityCollision = m_entity->GetComponent<CollisionComponent2D>();
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<NodeComponent>())
@ -42,6 +50,7 @@ namespace Ndk
matrix.MakeIdentity();
m_object = std::make_unique<Nz::RigidBody2D>(&world, 1.f, geom);
m_object->SetPositionOffset(positionOffset);
m_object->SetPosition(Nz::Vector2f(matrix.GetTranslation()));
m_object->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));
}

View File

@ -234,10 +234,10 @@ namespace Ndk
const Nz::Boxf& obb = entityGfx.GetAABB();
CollisionComponent2D& entityCollision2D = entity->GetComponent<CollisionComponent2D>();
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<CollisionComponent2D>())
{
@ -375,9 +375,12 @@ namespace Ndk
// Find center of mass
if (entity->HasComponent<PhysicsComponent2D>())
*origin = entity->GetComponent<PhysicsComponent2D>().GetMassCenter(Nz::CoordSys_Global);
{
const PhysicsComponent2D& entityPhys = entity->GetComponent<PhysicsComponent2D>();
*offset = entityPhys.GetMassCenter(Nz::CoordSys_Global) - entityPhys.GetPosition(); // GetPosition already takes GetGeomOffset into account
}
else
*origin = entity->GetComponent<NodeComponent>().GetPosition(Nz::CoordSys_Global);
*offset = entityCollision.GetGeomOffset();
return model;
}

View File

@ -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());*/
}
}