Fixed ApplyForce on rigidbody not working

This commit is contained in:
Antoine Pilote
2023-08-09 00:29:48 -04:00
parent 81026fb1af
commit 899e2dee86
8 changed files with 34 additions and 7 deletions

View File

@@ -192,7 +192,7 @@ class RigidBody {
}
AddForce(force) {
Scene.AddForce_(_entityId, force.x, force,y, force.z)
Scene.AddForce_(_entityId, force.x, force.y, force.z)
}
}

View File

@@ -555,21 +555,21 @@ namespace Nuake
}
}
void DynamicWorld::AddForceToRigidBody(const Entity& entity, const Vector3& force)
void DynamicWorld::AddForceToRigidBody(Entity& entity, const Vector3& force)
{
auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterface();
for (const auto& body : _registeredBodies)
{
auto bodyId = static_cast<JPH::BodyID>(body);
auto entityId = static_cast<uint32_t>(bodyInterface.GetUserData(bodyId));
if (entityId == entity.GetHandle())
if (entityId == entity.GetID())
{
bodyInterface.AddForce(bodyId, JPH::Vec3(force.x, force.y, force.z));
return;
}
}
Logger::Log("Failed to add force to rigidbody. Body not found with id: " + std::to_string(entity.GetHandle()), "physics", WARNING);
//Logger::Log("Failed to add force to rigidbody. Body not found with id: " + std::to_string(entity.GetHandle()), "physics", WARNING);
}
JPH::Ref<JPH::Shape> DynamicWorld::GetJoltShape(const Ref<PhysicShape> shape)

View File

@@ -63,7 +63,7 @@ namespace Nuake
bool IsCharacterGrounded(const Entity& entity);
// This is going to be ugly. TODO: Find a better way that passing itself as a parameter
void MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity);
void AddForceToRigidBody(const Entity& entity, const Vector3& force);
void AddForceToRigidBody(Entity& entity, const Vector3& force);
std::vector<RaycastResult> Raycast(const Vector3& from, const Vector3& to);
void StepSimulation(Timestep ts);

View File

@@ -1,7 +1,8 @@
#include "Prefab.h"
#include "src/Scene/Components/ParentComponent.h"
namespace Nuake {
namespace Nuake
{
Ref<Prefab> Prefab::CreatePrefabFromEntity(Entity entity)
{
Ref<Prefab> prefab = CreateRef<Prefab>();

View File

@@ -15,6 +15,8 @@ namespace Nuake {
float Mass;
Ref<Physics::RigidBody> Rigidbody;
Vector3 QueuedForce = Vector3();
RigidBodyComponent();
Ref<Physics::RigidBody> GetRigidBody() const;

View File

@@ -121,6 +121,7 @@ namespace Nuake
return;
InitializeRigidbodies();
ApplyForces();
PhysicsManager::Get().Step(ts);
}
@@ -333,4 +334,25 @@ namespace Nuake
// TODO: Other types of collider supported for character controller?
}
}
void PhysicsSystem::ApplyForces()
{
auto view = m_Scene->m_Registry.view<TransformComponent, RigidBodyComponent>();
for (auto e : view)
{
auto [transform, rigidBodyComponent] = view.get<TransformComponent, RigidBodyComponent>(e);
Entity ent = Entity({ e, m_Scene });
Ref<Physics::RigidBody> rigidBody;
// Not initialized yet.
if (!rigidBodyComponent.GetRigidBody() || rigidBodyComponent.QueuedForce == Vector3() || rigidBodyComponent.Mass == 0.0)
{
continue;
}
PhysicsManager::Get().GetWorld()->AddForceToRigidBody(ent, rigidBodyComponent.QueuedForce);
rigidBodyComponent.QueuedForce = Vector3();
}
}
}

View File

@@ -18,5 +18,7 @@ namespace Nuake
void InitializeQuakeMap();
void InitializeRigidbodies();
void InitializeCharacterControllers();
void ApplyForces();
};
}

View File

@@ -305,7 +305,7 @@ namespace Nuake
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& rigidBodyComponent = ent.GetComponent<RigidBodyComponent>();
rigidBodyComponent.Rigidbody->AddForce(Vector3(x, y, z));
rigidBodyComponent.QueuedForce += Vector3(x, y, z);
}
static void GetTranslation(WrenVM* vm)