mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-15 20:08:54 +03:00
Started collision callbacks for .Net
This commit is contained in:
16
Nuake/src/Physics/CollisionData.h
Normal file
16
Nuake/src/Physics/CollisionData.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <src/Core/Maths.h>
|
||||
|
||||
namespace Nuake {
|
||||
|
||||
namespace Physics {
|
||||
|
||||
struct CollisionData
|
||||
{
|
||||
uint32_t Entity1;
|
||||
uint32_t Entity2;
|
||||
Vector3 Normal;
|
||||
Vector3 Position;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -144,18 +144,23 @@ namespace Nuake
|
||||
|
||||
virtual void OnContactAdded(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override
|
||||
{
|
||||
auto entId1 = static_cast<int>(inBody1.GetUserData());
|
||||
Entity entity1 = Engine::GetCurrentScene()->GetEntityByID(entId1);
|
||||
int entity1 = static_cast<int>(inBody1.GetUserData());
|
||||
int entity2 = static_cast<int>(inBody2.GetUserData());
|
||||
|
||||
auto entId2 = static_cast<int>(inBody2.GetUserData());
|
||||
Entity entity2 = Engine::GetCurrentScene()->GetEntityByID(entId2);
|
||||
JPH::Vec3 joltNormal = inManifold.mWorldSpaceNormal;
|
||||
Vector3 normal = Vector3(joltNormal.GetX(), joltNormal.GetY(), joltNormal.GetZ());
|
||||
|
||||
const std::string entity1Name = entity1.GetComponent<NameComponent>().Name;
|
||||
const std::string entity2Name = entity2.GetComponent<NameComponent>().Name;
|
||||
JPH::Vec3 joltPos = inManifold.GetWorldSpaceContactPointOn1(0);
|
||||
Vector3 position = Vector3(joltPos.GetX(), joltPos.GetY(), joltPos.GetZ());
|
||||
|
||||
Logger::Log("Collision detected between " + entity1Name + " and " + entity2Name);
|
||||
Physics::CollisionData data
|
||||
{
|
||||
entity1,
|
||||
entity2,
|
||||
normal,
|
||||
position
|
||||
};
|
||||
|
||||
Physics::CollisionCallbackData data;
|
||||
_World->RegisterCollisionCallback(std::move(data));
|
||||
}
|
||||
|
||||
@@ -399,12 +404,12 @@ namespace Nuake
|
||||
Logger::Log("ERROR");
|
||||
}
|
||||
|
||||
bodySettings.mUserData = 1337;
|
||||
bodySettings.mUserData = cc->Owner.GetHandle();
|
||||
|
||||
// Create the actual rigid body
|
||||
JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr
|
||||
uint32_t bodyIndex = body.GetIndexAndSequenceNumber();
|
||||
_registeredBodies.push_back(bodyIndex);
|
||||
//_registeredBodies.push_back(bodyIndex);
|
||||
|
||||
// To get the jolt character control from a scene entity.
|
||||
_registeredCharacters[cc->Owner.GetHandle()] = CharacterGhostPair{ character, bodyIndex };
|
||||
@@ -548,6 +553,12 @@ namespace Nuake
|
||||
|
||||
void DynamicWorld::StepSimulation(Timestep ts)
|
||||
{
|
||||
// Clear collisions, before very step
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(_CollisionCallbackMutex);
|
||||
_CollisionCallbacks.clear();
|
||||
}
|
||||
|
||||
if (ts > 0.1f)
|
||||
{
|
||||
ts = 0.08f;
|
||||
@@ -677,7 +688,7 @@ namespace Nuake
|
||||
}
|
||||
}
|
||||
|
||||
void DynamicWorld::RegisterCollisionCallback(const CollisionCallbackData& data)
|
||||
void DynamicWorld::RegisterCollisionCallback(const CollisionData& data)
|
||||
{
|
||||
// This will be called from multiple threads
|
||||
std::scoped_lock<std::mutex> lock(_CollisionCallbackMutex);
|
||||
@@ -685,6 +696,12 @@ namespace Nuake
|
||||
_CollisionCallbacks.push_back(std::move(data));
|
||||
}
|
||||
|
||||
const std::vector<CollisionData>& DynamicWorld::GetCollisionsData()
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(_CollisionCallbackMutex);
|
||||
return _CollisionCallbacks;
|
||||
}
|
||||
|
||||
void DynamicWorld::MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity)
|
||||
{
|
||||
const uint32_t entityHandle = entity.GetHandle();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <src/Physics/GhostObject.h>
|
||||
#include "CharacterController.h"
|
||||
#include "CollisionData.h"
|
||||
|
||||
#include "Jolt/Jolt.h"
|
||||
|
||||
@@ -41,13 +42,7 @@ namespace Nuake
|
||||
uint32_t Ghost;
|
||||
};
|
||||
|
||||
struct CollisionCallbackData
|
||||
{
|
||||
uint32_t Entity1;
|
||||
uint32_t Entity2;
|
||||
Vector3 Normal;
|
||||
Vector3 Position;
|
||||
};
|
||||
|
||||
|
||||
class DynamicWorld
|
||||
{
|
||||
@@ -65,7 +60,7 @@ namespace Nuake
|
||||
std::map<uint32_t, CharacterGhostPair> _registeredCharacters;
|
||||
|
||||
std::mutex _CollisionCallbackMutex;
|
||||
std::vector<CollisionCallbackData> _CollisionCallbacks;
|
||||
std::vector<CollisionData> _CollisionCallbacks;
|
||||
public:
|
||||
DynamicWorld();
|
||||
|
||||
@@ -85,8 +80,8 @@ namespace Nuake
|
||||
void StepSimulation(Timestep ts);
|
||||
void Clear();
|
||||
|
||||
void RegisterCollisionCallback(const CollisionCallbackData& data);
|
||||
|
||||
void RegisterCollisionCallback(const CollisionData& data);
|
||||
const std::vector<CollisionData>& GetCollisionsData();
|
||||
private:
|
||||
JPH::Ref<JPH::Shape> GetJoltShape(const Ref<PhysicShape> shape);
|
||||
void SyncEntitiesTranforms();
|
||||
|
||||
@@ -42,6 +42,11 @@ namespace Nuake
|
||||
return m_World->Raycast(from, to);
|
||||
}
|
||||
|
||||
const std::vector<Physics::CollisionData>& PhysicsManager::GetCollisions()
|
||||
{
|
||||
return m_World->GetCollisionsData();
|
||||
}
|
||||
|
||||
void PhysicsManager::DrawDebug()
|
||||
{
|
||||
if (m_DrawDebug)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "../Scene/Entities/Entity.h"
|
||||
#include "DynamicWorld.h"
|
||||
#include "Rigibody.h"
|
||||
#include "CollisionData.h"
|
||||
|
||||
#include "RaycastResult.h"
|
||||
|
||||
@@ -49,6 +50,8 @@ namespace Nuake
|
||||
|
||||
std::vector<RaycastResult> Raycast(const Vector3& from, const Vector3& to);
|
||||
|
||||
const std::vector<Physics::CollisionData>& GetCollisions();
|
||||
|
||||
void RegisterBody(Ref<Physics::RigidBody> rb);
|
||||
void RegisterGhostBody(Ref<GhostObject> rb);
|
||||
void RegisterCharacterController(Ref<Physics::CharacterController> c);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "Engine.h"
|
||||
|
||||
#include "src/Scripting/ScriptingEngineNet.h"
|
||||
|
||||
#include "src/Physics/PhysicsManager.h"
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
@@ -129,6 +129,8 @@ namespace Nuake
|
||||
auto scriptInstance = scriptingEngineNet.GetEntityScript(entity);
|
||||
scriptInstance.InvokeMethod("OnFixedUpdate", ts.GetSeconds());
|
||||
}
|
||||
|
||||
DispatchPhysicCallbacks();
|
||||
}
|
||||
|
||||
void ScriptingSystem::Exit()
|
||||
@@ -166,4 +168,23 @@ namespace Nuake
|
||||
ScriptingEngine::Close();
|
||||
ScriptingEngineNet::Get().Uninitialize();
|
||||
}
|
||||
|
||||
void ScriptingSystem::DispatchPhysicCallbacks()
|
||||
{
|
||||
auto& scriptingEngineNet = ScriptingEngineNet::Get();
|
||||
|
||||
auto& physicsManager = PhysicsManager::Get();
|
||||
const auto& collisions = physicsManager.GetCollisions();
|
||||
for (const auto& col : collisions)
|
||||
{
|
||||
Entity entity1 = m_Scene->GetEntityByID(col.Entity1);
|
||||
Entity entity2 = m_Scene->GetEntityByID(col.Entity2);
|
||||
|
||||
if (entity1.IsValid() && scriptingEngineNet.HasEntityScriptInstance(entity1))
|
||||
{
|
||||
auto scriptInstance = scriptingEngineNet.GetEntityScript(entity1);
|
||||
scriptInstance.InvokeMethod("OnCollision", col.Entity1, col.Entity2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,5 +13,8 @@ namespace Nuake {
|
||||
void Draw() override {}
|
||||
void FixedUpdate(Timestep ts) override;
|
||||
void Exit() override;
|
||||
|
||||
private:
|
||||
void DispatchPhysicCallbacks();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user