Added ground normal & ground velocity C# API e

This commit is contained in:
Antoine Pilote
2024-09-02 18:09:02 -04:00
parent 01000b9d13
commit d9237f83d2
9 changed files with 125 additions and 8 deletions

View File

@@ -69,8 +69,8 @@ namespace NuakeEditor
"name": "Trigger",
"attribs": [ "transparent" ],
"match": "classname",
"pattern": "trigger_*",
"texture": "trigger"
"pattern": "Trigger*",
"texture": "trigger.png"
}
],
"brushface": [
@@ -135,7 +135,7 @@ namespace NuakeEditor
// Copy Icon.png
if (FileSystem::FileExists("icon.png"))
{
std::filesystem::copy_file(FileSystem::RelativeToAbsolute("icon.png"), gameConfigFolderPath + "Icon.png");
std::filesystem::copy_file(FileSystem::RelativeToAbsolute("icon.png"), gameConfigFolderPath + "Icon.png", std::filesystem::copy_options::overwrite_existing);
}
return true;
}

View File

@@ -53,6 +53,7 @@ namespace Nuake
static inline float GetTime() { return s_Time; }
static inline Timestep GetTimestep() { return s_TimeStep; }
static inline void SetPhysicsStep(int amount) { s_FixedUpdateRate = 1.0f / static_cast<float>(amount); }
static inline float GetFixedTimeStep() { return s_FixedUpdateRate; }
static inline void SetTimeScale(float timeScale) { s_TimeScale = timeScale; }
static inline float GetTimeScale() { return s_TimeScale; }
@@ -63,6 +64,7 @@ namespace Nuake
static bool LoadProject(Ref<Project> project);
static Ref<Project> GetProject();
};
}

View File

@@ -352,6 +352,12 @@ namespace Nuake
layer = Layers::MOVING;
}
if (rb->GetForceKinematic())
{
motionType = JPH::EMotionType::Kinematic;
layer = Layers::MOVING;
}
const std::string name = rb->GetEntity().GetComponent<NameComponent>().Name;
if (rb->IsTrigger())
{
@@ -474,6 +480,35 @@ namespace Nuake
return false;
}
Vector3 DynamicWorld::GetCharacterGroundVelocity(const Entity& entity)
{
const uint32_t entityHandle = entity.GetHandle();
if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end())
{
auto& characterController = _registeredCharacters[entityHandle].Character;
characterController->UpdateGroundVelocity();
const auto groundVelocity = characterController->GetGroundVelocity();
return Vector3(groundVelocity.GetX(), groundVelocity.GetY(), groundVelocity.GetZ());
}
return { 0, 0, 0 };
}
Vector3 DynamicWorld::GetCharacterGroundNormal(const Entity& entity)
{
const uint32_t entityHandle = entity.GetHandle();
if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end())
{
auto& characterController = _registeredCharacters[entityHandle].Character;
const auto groundNormal = characterController->GetGroundNormal();
return Vector3(groundNormal.GetX(), groundNormal.GetY(), groundNormal.GetZ());
}
return { 0, 0, 0 };
}
void DynamicWorld::SetBodyPosition(const Entity& entity, const Vector3& position, const Quat& rotation)
{
const auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterface();
@@ -499,7 +534,7 @@ namespace Nuake
{
case JPH::EMotionType::Kinematic:
{
_JoltBodyInterface->MoveKinematic(bodyId, newPosition, newRotation, 0.0f);
_JoltBodyInterface->MoveKinematic(bodyId, newPosition, newRotation, Engine::GetFixedTimeStep());
break;
}
case JPH::EMotionType::Static:
@@ -627,7 +662,7 @@ namespace Nuake
continue;
}
JPH::Vec3 position = bodyInterface.GetCenterOfMassPosition(bodyId);
JPH::Vec3 position = bodyInterface.GetPosition(bodyId);
JPH::Vec3 velocity = bodyInterface.GetLinearVelocity(bodyId);
JPH::Mat44 joltTransform = bodyInterface.GetWorldTransform(bodyId);
const auto bodyRotation = bodyInterface.GetRotation(bodyId);

View File

@@ -75,6 +75,9 @@ namespace Nuake
void AddGhostbody(Ref<GhostObject> gb);
void AddCharacterController(Ref<CharacterController> cc);
bool IsCharacterGrounded(const Entity& entity);
Vector3 GetCharacterGroundVelocity(const Entity& entity);
Vector3 GetCharacterGroundNormal(const Entity& entity);
void SetCharacterControllerPosition(const Entity& entity, const Vector3& position);
void SetBodyPosition(const Entity& entity, const Vector3& position, const Quat& rotation);

View File

@@ -150,6 +150,7 @@ namespace Nuake
}
file->PointEntities.clear();
std::vector<std::string> bases;
for (auto& [name, type] : ScriptingEngineNet::Get().GetPointEntities())
{
FGDPointEntity pointEntity = FGDPointEntity(name);
@@ -207,6 +208,13 @@ namespace Nuake
file->PointEntities.push_back(pointEntity);
}
for (auto& b : bases)
{
FGDBaseEntity baseEntity;
baseEntity.Name = b;
file->BaseEntities.push_back(baseEntity);
}
file->Export();
}

View File

@@ -399,6 +399,52 @@ namespace Nuake {
return false;
}
Coral::Array<float> GetGroundVelocity(int entityId)
{
Entity entity = Entity((entt::entity)(entityId), Engine::GetCurrentScene().get());
Coral::Array<float> resultArray = Coral::Array<float>::New(3);
if (entity.IsValid() && entity.HasComponent<CharacterControllerComponent>())
{
auto& characterController = entity.GetComponent<CharacterControllerComponent>();
Vector3 groundVelocity = PhysicsManager::Get().GetWorld()->GetCharacterGroundVelocity(entity);
resultArray[0] = groundVelocity.x;
resultArray[1] = groundVelocity.y;
resultArray[2] = groundVelocity.z;
return resultArray;
}
resultArray[0] = 0.0f;
resultArray[1] = 0.0f;
resultArray[2] = 0.0f;
return resultArray;
}
Coral::Array<float> GetGroundNormal(int entityId)
{
Entity entity = Entity((entt::entity)(entityId), Engine::GetCurrentScene().get());
Coral::Array<float> resultArray = Coral::Array<float>::New(3);
if (entity.IsValid() && entity.HasComponent<CharacterControllerComponent>())
{
auto& characterController = entity.GetComponent<CharacterControllerComponent>();
Vector3 groundVelocity = PhysicsManager::Get().GetWorld()->GetCharacterGroundNormal(entity);
resultArray[0] = groundVelocity.x;
resultArray[1] = groundVelocity.y;
resultArray[2] = groundVelocity.z;
return resultArray;
}
resultArray[0] = 0.0f;
resultArray[1] = 0.0f;
resultArray[2] = 0.0f;
return resultArray;
}
void Play(int entityId, Coral::String animation)
{
Entity entity = Entity((entt::entity)(entityId), Engine::GetCurrentScene().get());
@@ -520,7 +566,8 @@ namespace Nuake {
// Character Controller
RegisterMethod("CharacterControllerComponent.MoveAndSlideIcall", &MoveAndSlide);
RegisterMethod("CharacterControllerComponent.IsOnGroundIcall", &IsOnGround);
RegisterMethod("CharacterControllerComponent.GetGroundVelocityIcall", &GetGroundVelocity);
RegisterMethod("CharacterControllerComponent.GetGroundNormalIcall", &GetGroundNormal);
// Skinned
RegisterMethod("SkinnedModelComponent.PlayIcall", &Play);

View File

@@ -323,7 +323,7 @@ namespace Nuake
// Point
bool isPointScript = false;
std::string pointDescription;
std::string pointBase;
for (auto& attribute : type->GetAttributes())
{
if (attribute.GetType() == brushScriptAttributeType)
@@ -336,6 +336,7 @@ namespace Nuake
if (attribute.GetType() == pointScriptAttributeType)
{
pointDescription = attribute.GetFieldValue<Coral::String>("Description");
pointBase = attribute.GetFieldValue<Coral::String>("Base");
isPointScript = true;
}
}
@@ -354,7 +355,7 @@ namespace Nuake
NetGameScriptObject gameScriptObject;
gameScriptObject.coralType = type;
gameScriptObject.exposedVars = std::vector<ExposedVar>();
gameScriptObject.Base = baseTypeName;
for (auto& f : type->GetFields())
{
for (auto& a : f.GetAttributes())

View File

@@ -48,6 +48,7 @@ namespace Nuake {
Coral::Type* coralType;
std::vector<ExposedVar> exposedVars;
std::string Base = "";
bool isTrigger = false;
std::string Description = "";
AABB aabb = AABB({0, 0, 0}, { 1, 1, 1 });

View File

@@ -311,6 +311,8 @@ namespace Nuake.Net
{
internal static unsafe delegate*<int, float, float, float, void> MoveAndSlideIcall;
internal static unsafe delegate*<int, bool> IsOnGroundIcall;
internal static unsafe delegate*<int, NativeArray<float>> GetGroundVelocityIcall;
internal static unsafe delegate*<int, NativeArray<float>> GetGroundNormalIcall;
public CharacterControllerComponent(int entityId)
{
@@ -326,6 +328,24 @@ namespace Nuake.Net
{
unsafe { return IsOnGroundIcall(EntityID); }
}
public Vector3 GetGroundNormal()
{
unsafe
{
NativeArray<float> resultArray = GetGroundNormalIcall(EntityID);
return new(resultArray[0], resultArray[1], resultArray[2]);
}
}
public Vector3 GetGroundVelocity()
{
unsafe
{
NativeArray<float> resultArray = GetGroundVelocityIcall(EntityID);
return new(resultArray[0], resultArray[1], resultArray[2]);
}
}
}
public class ParticleEmitterComponent : IComponent