Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul of physics resources and actors #222

Merged
merged 7 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions code/addons/graphicsfeature/managers/graphicsmanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ GraphicsManager::InitUpdateModelTransformProcessor()
.Func(
[](Game::World* world, Game::Position const& pos, Game::Orientation const& orient, Game::Scale const& scale, GraphicsFeature::Model const& model)
{
Math::mat4 worldTransform = Math::trs(pos, orient, scale);
Models::ModelContext::SetTransform(model.graphicsEntityId, worldTransform);
if (model.graphicsEntityId != -1)
{
Math::mat4 worldTransform = Math::trs(pos, orient, scale);
Models::ModelContext::SetTransform(model.graphicsEntityId, worldTransform);
}
}
)
.Build();
Expand Down
24 changes: 24 additions & 0 deletions code/addons/nflatbuffer/flatbufferinterface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ bool FlatbufferInterface::LoadSchema(IO::URI const& file)
return state.LoadFbs(file);
}

//------------------------------------------------------------------------------
/**
*/
bool FlatbufferInterface::CompileSchema(IO::URI const& file)
{
Ptr<IO::Stream> stream = IO::IoServer::Instance()->CreateStream(file);
if (stream->Open())
{
void* buf = stream->Map();
IO::URI systemInclude = "tool:syswork/data/flatbuffer/";
const char* includes[2];
includes[0] = systemInclude.LocalPath().AsCharPtr();
includes[1] = nullptr;
flatbuffers::Parser* parser = new flatbuffers::Parser;
parser->Parse((const char*)buf, includes, file.LocalPath().AsCharPtr());
parser->Serialize();
auto blorf = parser->builder_.GetSize();
char* ff = reinterpret_cast<char*>(parser->builder_.GetBufferPointer());
}
return false;
}

//------------------------------------------------------------------------------
/**
*/
Expand Down Expand Up @@ -183,7 +205,9 @@ bool FlatbufferInterface::Compile(IO::URI const& source, IO::URI const& targetFo
// FIXME could use own writer
target += "/";
if (IO::IoServer::Instance()->CreateDirectory(target))
{
result = flatbuffers::GenerateBinary(*parser, target.AsCharPtr(), filename.AsCharPtr());
}
}
}
delete parser;
Expand Down
2 changes: 2 additions & 0 deletions code/addons/nflatbuffer/flatbufferinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class FlatbufferInterface
///
static bool LoadSchema(IO::URI const& file);
///
static bool CompileSchema(IO::URI const& file);
///
static bool HasSchema(Util::StringAtom identifier);

/// Helper function, use SerializeFlatbuffer macro instead
Expand Down
28 changes: 28 additions & 0 deletions code/addons/nflatbuffer/nebula_flat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,32 @@ UnPack(const Flat::Quat& v)
Math::quat m(v.x(), v.y(), v.z(), v.w());
return m;
}


//------------------------------------------------------------------------------
/**
*/
Flat::Transform
Pack(const Math::transform& v)
{
Flat::Transform t;
v.position.storeu(t.mutable_position()->data());
v.rotation.storeu(t.mutable_orientation()->data());
v.scale.storeu(t.mutable_scale()->data());
return t;
}

//------------------------------------------------------------------------------
/**
*/
Math::transform
UnPack(const Flat::Transform& v)
{
Math::transform t;
t.position.loadu(v.position()->data());
t.rotation.loadu(v.orientation()->data());
t.scale.loadu(v.scale()->data());
return t;
}

}
5 changes: 5 additions & 0 deletions code/addons/nflatbuffer/nebula_flat.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "math/vec4.h"
#include "math/mat4.h"
#include "math/quat.h"
#include "math/transform.h"
#include "flat/foundation/math.h"

namespace Flat
Expand Down Expand Up @@ -45,4 +46,8 @@ Math::vec2 UnPack(const Flat::Vec2& v);
Flat::Mat4 Pack(const Math::mat4& v);
///
Math::mat4 UnPack(const Flat::Mat4& v);
///
Flat::Transform Pack(const Math::transform& v);
///
Math::transform UnPack(const Flat::Transform& v);
}
13 changes: 13 additions & 0 deletions code/addons/physicsfeature/components/physicsfeature.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"namespace": "PhysicsFeature",
"includes": [
"physicsinterface.h",
"game/api.h"
]
"enums": {
"ActorType": {
"Static": 0,
Expand All @@ -23,5 +27,14 @@
}
},
"IsKinematic": {}
},
"messages": {
"ContactEventMessage": {
"fourcc": "CEVM",
"args": {
"entity": "Game::Entity",
"event": "const Physics::ContactEvent&"
}
}
}
}
11 changes: 8 additions & 3 deletions code/addons/physicsfeature/managers/physicsmanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ PhysicsManager::InitPhysicsActor(Game::World* world, Game::Entity entity, Physic
actor->resource = res;
}

Math::mat4 worldTransform = Math::trs(
Math::transform worldTransform = Math::transform(
world->GetComponent<Game::Position>(entity),
world->GetComponent<Game::Orientation>(entity),
world->GetComponent<Game::Scale>(entity)
Expand All @@ -67,7 +67,7 @@ PhysicsManager::InitPhysicsActor(Game::World* world, Game::Entity entity, Physic
Physics::CreateActorInstance(resId, worldTransform, (Physics::ActorType)actor->actorType, Ids::Id32(entity));
actor->actorId = actorid.id;

if (actor->actorType == Physics::ActorType::Kinematic)
if (actor->actorType == Physics::Kinematic)
{
world->AddComponent<PhysicsFeature::IsKinematic>(entity);
}
Expand All @@ -79,6 +79,8 @@ PhysicsManager::InitPhysicsActor(Game::World* world, Game::Entity entity, Physic
{
Physics::ActorContext::SetAngularVelocity(actorid, world->GetComponent<Game::AngularVelocity>(entity));
}
// fixme, this should be configured, just brute force it for now
Physics::ActorContext::SetCollisionFeedback(actorid, Physics::CollisionFeedback_Full);
}

//------------------------------------------------------------------------------
Expand All @@ -102,7 +104,10 @@ PhysicsManager::OnDecay()
void
PollRigidbodyTransforms(Game::World* world, Game::Position& position, Game::Orientation& orientation, PhysicsFeature::PhysicsActor const& actor)
{
Physics::ActorContext::GetPositionOrientation(actor.actorId, position, orientation);
if (actor.actorId != -1)
{
Physics::ActorContext::GetPositionOrientation(actor.actorId, position, orientation);
}
}

//------------------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions code/addons/physicsfeature/physicsfeatureunit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ PhysicsFeatureUnit::OnActivate()
world->RemoveComponent<Game::Static>(entity);
}
});
Physics::SetEventCallback([](const Util::Array<Physics::ContactEvent>& buffer)
{
Game::World* world = Game::GetWorld(WORLD_DEFAULT);
for (auto const& contact : buffer)
{
if (Physics::ActorContext::IsValid(contact.actor0))
{
Physics::Actor& actor = Physics::ActorContext::GetActor(contact.actor0);
Game::Entity entity = Game::Entity::FromId((Ids::Id32)actor.userData);
if (world->IsValid(entity))
{
PhysicsFeature::ContactEventMessage::Send(entity, contact);
}

}

}
});

}

//------------------------------------------------------------------------------
Expand Down
19 changes: 15 additions & 4 deletions code/application/game/world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ World::ManageEntities()
{
auto const cmd = this->allocQueue.Dequeue();
n_assert(this->IsValid(cmd.entity));
this->AllocateInstance(cmd.entity, cmd.tid);
//this->AllocateInstance(cmd.entity, cmd.tid);
this->FinalizeAllocate(cmd.entity);
}

// Delete all remaining invalid instances
Expand Down Expand Up @@ -578,11 +579,12 @@ World::CreateEntity(EntityCreateInfo const& info)

if (!info.immediate)
{
this->AllocateInstance(cmd.entity, cmd.tid, false);
this->allocQueue.Enqueue(std::move(cmd));
}
else
{
this->AllocateInstance(cmd.entity, cmd.tid);
this->AllocateInstance(cmd.entity, cmd.tid, true);
}

return entity;
Expand Down Expand Up @@ -1178,7 +1180,7 @@ World::AllocateInstance(Entity entity, BlueprintId blueprint)
/**
*/
MemDb::RowId
World::AllocateInstance(Entity entity, TemplateId templateId)
World::AllocateInstance(Entity entity, TemplateId templateId, bool performInitialize)
{
n_assert(this->pool.IsValid(entity));
n_assert(this->entityMap[entity.index].instance == MemDb::InvalidRow);
Expand All @@ -1197,11 +1199,20 @@ World::AllocateInstance(Entity entity, TemplateId templateId)
.GetBuffer(mapping.instance.partition, Game::Entity::Traits::fixed_column_index);
owners[mapping.instance.index] = entity;

InitializeAllComponents(entity, mapping.table, mapping.instance);
if (performInitialize)
{
InitializeAllComponents(entity, mapping.table, mapping.instance);
}

return mapping.instance;
}

void
World::FinalizeAllocate(Entity entity)
{
EntityMapping& mapping = this->entityMap[entity.index];
InitializeAllComponents(entity, mapping.table, mapping.instance);
}
//------------------------------------------------------------------------------
/**
*/
Expand Down
3 changes: 2 additions & 1 deletion code/application/game/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ class World
/// Allocate an entity instance from a blueprint. Use this with caution!
MemDb::RowId AllocateInstance(Entity entity, BlueprintId blueprint);
/// Allocate an entity instance from a template. Use this with caution!
MemDb::RowId AllocateInstance(Entity entity, TemplateId templateId);
MemDb::RowId AllocateInstance(Entity entity, TemplateId templateId, bool performInitialize);
void FinalizeAllocate(Entity entity);
/// Deallocate an entity instance. Use this with caution!
void DeallocateInstance(MemDb::TableId table, MemDb::RowId instance);
/// Deallocate an entity instance. Use this with caution!
Expand Down
6 changes: 5 additions & 1 deletion code/foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ nebula_begin_module(foundation)
bbox.cc
bbox.h
clipstatus.h
curves.cc
curves.h
euler.h
extrapolator.h
frustum.h
half.h
Expand All @@ -296,6 +299,7 @@ nebula_begin_module(foundation)
sphere.cc
sphere.h
sse.h
transform.h
transform44.h
vec2.h
vec3.h
Expand Down Expand Up @@ -525,7 +529,7 @@ nebula_begin_module(foundation)
)
fips_dir(.)
if (FIPS_WINDOWS)
fips_files(foundation.natvis)
fips_files(foundation.natvis foundation.natstepfilter)
fips_dir(core GROUP "core/win32")
fips_files(
win32/precompiled.h
Expand Down
46 changes: 46 additions & 0 deletions code/foundation/foundation.natstepfilter
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
.natstepfilter file for Visual Studio debugger.
Purpose: instruct debugger to skip some functions when using StepInto (F11)

Since Visual Studio 2022 version 17.6 Preview 2 (currently available as a "Preview" build on March 14, 2023)
It is possible to add the .natstepfilter file to your project file and it will automatically be used.
(https://developercommunity.visualstudio.com/t/allow-natstepfilter-and-natjmc-to-be-included-as-p/561718)

For older Visual Studio version prior to 2022 17.6 Preview 2:
* copy in %USERPROFILE%\Documents\Visual Studio XXXX\Visualizers (current user)
* or copy in %VsInstallDirectory%\Common7\Packages\Debugger\Visualizers (all users)
If you have multiple VS version installed, the version that matters is the one you are using the IDE/debugger
of (not the compiling toolset). This is supported since Visual Studio 2012.

More information at: https://docs.microsoft.com/en-us/visualstudio/debugger/just-my-code?view=vs-2019#BKMK_C___Just_My_Code
-->

<StepFilter xmlns="http://schemas.microsoft.com/vstudio/debugger/natstepfilter/2010">

<!-- Disable stepping into trivial functions -->
<Function>
<Name>Ptr&lt;.*&gt;::.*</Name>
<Action>NoStepInto</Action>
</Function>
<Function>
<Name>Util::String::String.*</Name>
<Action>NoStepInto</Action>
</Function>
<Function>
<Name>IO::URI::URI.*</Name>
<Action>NoStepInto</Action>
</Function>
<Function>
<Name>Util::StringAtom::StringAtom.*</Name>
<Action>NoStepInto</Action>
</Function>
<Function>
<Name>Util::String::AsCharPtr.*</Name>
<Action>NoStepInto</Action>
</Function>
<Function>
<Name>Util::Array&lt;.*&gt;::operator.*</Name>
<Action>NoStepInto</Action>
</Function>
</StepFilter>
16 changes: 16 additions & 0 deletions code/foundation/math/curves.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//------------------------------------------------------------------------------
// curves.cc
// (C) 2024 Individual contributors, see AUTHORS file
//------------------------------------------------------------------------------

#include "math/curves.h"
#include "math/vec2.h"
#include "math/vec3.h"
#include "math/vec4.h"

namespace Math
{
template struct BezierCubic<Math::vec3>;
template struct BezierCubic<Math::vec2>;
template struct BezierCubic<Math::vec4>;
}
28 changes: 28 additions & 0 deletions code/foundation/math/curves.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
//------------------------------------------------------------------------------
/**
Different curves

@copyright
(C) 2024 Individual contributors, see AUTHORS file
*/

namespace Math
{

template<class POINT>
struct BezierCubic
{
BezierCubic(POINT pp0, POINT pp1, POINT pp2, POINT pp3) { Set(pp0, pp1, pp2, pp3); }
void Set(POINT pp0, POINT pp1, POINT pp2, POINT pp3) { p0 = pp0; p3 = pp3; s1 = pp1*3.0f - pp0 - pp3; s2 = pp2*3.0f - pp3 - pp0; }
POINT Eval(float t)
{
POINT p03 = lerp(p0, p3, t);
POINT s12 = lerp(s1, s2, t);
return lerp(p03, s12, 1.0f - t);
}

POINT p0, p3, s1, s2;
};

};
Loading
Loading