Merge pull request #22 from antopilo/NK-104-rigidbodies-initial-rotation

NK-104 Physics bodies now support initial rotation
This commit is contained in:
Antoine Pilote
2023-07-10 14:59:21 -04:00
committed by GitHub
5 changed files with 31 additions and 19 deletions

View File

@@ -5,6 +5,7 @@
#include <glm/ext/quaternion_float.hpp>
#include <src/Vendors/glm/ext/vector_float2.hpp>
#include <src/Vendors/glm/ext/matrix_float4x4.hpp>
#include "src/Vendors/glm/gtx/matrix_decompose.hpp"
#include <glm/gtx/quaternion.hpp>
namespace Nuake

View File

@@ -3,6 +3,7 @@
#include "src/Core/Core.h"
#include "src/Core/Logger.h"
#include "src/Core/Maths.h"
#include <src/Core/Physics/PhysicsShapes.h>
#include "src/Scene/Components/TransformComponent.h"
#include "src/Scene/Components/CharacterControllerComponent.h"
@@ -259,9 +260,11 @@ namespace Nuake
}
const auto& startPos = rb->GetPosition();
const Quat& bodyRotation = rb->GetRotation();
const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w);
const auto& joltPos = JPH::Vec3(startPos.x, startPos.y, startPos.z);
auto joltShape = GetJoltShape(rb->GetShape());
JPH::BodyCreationSettings bodySettings(joltShape, joltPos, JPH::Quat::sIdentity(), motionType, Layers::MOVING);
JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, Layers::MOVING);
if (mass > 0.0f)
{
@@ -410,7 +413,7 @@ namespace Nuake
// Do 1 collision step per 1 / 60th of a second (round up).
int collisionSteps = 1;
constexpr float minStepDuration = 1.0f / 90.0f;
constexpr int maxStepCount = 4;
constexpr int maxStepCount = 32;
if(ts > minStepDuration)
{
collisionSteps = static_cast<float>(ts) / minStepDuration;
@@ -427,7 +430,7 @@ namespace Nuake
for (auto& c : _registeredCharacters)
{
c.second->PostSimulation(0.01);
c.second->PostSimulation(0.001);
}
SyncEntitiesTranforms();

View File

@@ -1,31 +1,38 @@
#pragma once
#include "PhysicsShapes.h"
#include "../Core/Core.h"
#include "../Scene/Entities/Entity.h"
#include "src/Core/Core.h"
#include "src/Core/Maths.h"
#include "src/Scene/Entities/Entity.h"
#include <glm/ext/vector_float3.hpp>
namespace Nuake
{
class Entity;
namespace Physics {
class RigidBody {
namespace Physics
{
class RigidBody
{
private:
Ref<PhysicShape> _collisionShape;
Vector3 _position;
Quat _rotation;
Entity _entity;
public:
float _mass;
Matrix4 _transform;
RigidBody();
RigidBody(glm::vec3 position, Entity handle);
RigidBody(float mass, glm::vec3 position, Matrix4 transform, Ref<PhysicShape> shape, Entity entity, glm::vec3 initialVel = glm::vec3(0, 0, 0));
RigidBody(Vector3 position, Entity handle);
RigidBody(float mass, Vector3 position, Quat rotation, Matrix4 transform, Ref<PhysicShape> shape, Entity entity, glm::vec3 initialVel = glm::vec3(0, 0, 0));
void UpdateTransform();
void SetEntityID(Entity ent);
Vector3 GetPosition() const { return _position; }
Quat GetRotation() const { return _rotation; }
bool HasShape() { return _collisionShape != nullptr; }
void SetShape(Ref<PhysicShape> shape);
Ref<PhysicShape> GetShape() const { return _collisionShape; }

View File

@@ -18,12 +18,13 @@ namespace Nuake
}
RigidBody::RigidBody(float mass, glm::vec3 position, Matrix4 transform, Ref<PhysicShape> shape, Entity entity, glm::vec3 initialVel) :
RigidBody::RigidBody(float mass, Vector3 position, Quat rotation, Matrix4 transform, Ref<PhysicShape> shape, Entity entity, glm::vec3 initialVel) :
_position(position),
_collisionShape(shape),
_mass(mass),
_entity(entity),
_transform(transform)
_transform(transform),
_rotation(rotation)
{
}

View File

@@ -141,11 +141,12 @@ namespace Nuake
for (const auto& hull : brushComponent.Hulls)
{
const auto entity = Entity({ e, m_Scene });
const Vector3 startPosition = transformComponent.GetGlobalPosition();
const Matrix4 startTransform = transformComponent.GetGlobalTransform();
const Vector3& startPosition = transformComponent.GetGlobalPosition();
const Matrix4& startTransform = transformComponent.GetGlobalTransform();
const Quat& startRotation = transformComponent.GetGlobalRotation();
const auto collisionShape = CreateRef<Physics::ConvexHullShape>(hull);
auto rigidBody = CreateRef<Physics::RigidBody>(0.0f, startPosition, startTransform, collisionShape, entity);
auto rigidBody = CreateRef<Physics::RigidBody>(0.0f, startPosition, startRotation, startTransform, collisionShape, entity);
brushComponent.Rigidbody.push_back(rigidBody);
PhysicsManager::Get().RegisterBody(rigidBody);
@@ -220,8 +221,7 @@ namespace Nuake
BoxColliderComponent& boxComponent = ent.GetComponent<BoxColliderComponent>();
Ref<Physics::Box> boxShape = CreateRef<Physics::Box>(boxComponent.Size);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), boxShape, ent);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), boxShape, ent);
PhysicsManager::Get().RegisterBody(rigidBody);
}
@@ -232,7 +232,7 @@ namespace Nuake
float height = capsuleComponent.Height;
auto capsuleShape = CreateRef<Physics::Capsule>(radius, height);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), capsuleShape, ent);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), capsuleShape, ent);
PhysicsManager::Get().RegisterBody(rigidBody);
}
@@ -243,7 +243,7 @@ namespace Nuake
const auto& component = ent.GetComponent<SphereColliderComponent>();
auto shape = CreateRef<Physics::Sphere>(component.Radius);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), shape, ent);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), shape, ent);
PhysicsManager::Get().RegisterBody(rigidBody);
}
@@ -266,7 +266,7 @@ namespace Nuake
}
Ref<Mesh> mesh = submeshes[subMeshId];
auto shape = CreateRef<Physics::MeshShape>(mesh);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), shape, ent);
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), shape, ent);
PhysicsManager::Get().RegisterBody(rigidBody);
}
}