Continue Jolt integration
This commit is contained in:
committed by
Jérôme Leclercq
parent
21e08798ce
commit
021801f02e
@@ -102,7 +102,7 @@ int main()
|
||||
cameraComponent.UpdateClearColor(Nz::Color(0.5f, 0.5f, 0.5f));
|
||||
}
|
||||
|
||||
auto shipCollider = std::make_shared<Nz::ConvexCollider3D>(vertices, vertexMapper.GetVertexCount());
|
||||
auto shipCollider = std::make_shared<Nz::BulletConvexCollider3D>(vertices, vertexMapper.GetVertexCount());
|
||||
|
||||
std::shared_ptr<Nz::MaterialInstance> colliderMat = Nz::Graphics::Instance()->GetDefaultMaterials().basicMaterial->Instantiate();
|
||||
colliderMat->SetValueProperty("BaseColor", Nz::Color::Green());
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Sources pour https://github.com/NazaraEngine/NazaraEngine/wiki/(FR)-Tutoriel:-%5B01%5D-Hello-World
|
||||
|
||||
#define USE_JOLT 0
|
||||
#define USE_JOLT 1
|
||||
|
||||
#include <Nazara/Core.hpp>
|
||||
#include <Nazara/Graphics.hpp>
|
||||
@@ -22,7 +22,7 @@ int main()
|
||||
try {
|
||||
// Mise en place de l'application, de la fenêtre et du monde
|
||||
Nz::Renderer::Config renderConfig;
|
||||
//renderConfig.preferredAPI = Nz::RenderAPI::OpenGL;
|
||||
renderConfig.validationLevel = Nz::RenderAPIValidationLevel::None;
|
||||
|
||||
#if USE_JOLT
|
||||
Nz::Application<Nz::Graphics, Nz::JoltPhysics3D> app(renderConfig);
|
||||
@@ -150,7 +150,7 @@ int main()
|
||||
std::uniform_real_distribution<float> colorDis(0.f, 360.f);
|
||||
std::uniform_real_distribution<float> radiusDis(0.1f, 0.5f);
|
||||
|
||||
constexpr std::size_t SphereCount = 2000;
|
||||
constexpr std::size_t SphereCount = 1000;
|
||||
for (std::size_t i = 0; i < SphereCount; ++i)
|
||||
{
|
||||
float radius = radiusDis(rd);
|
||||
@@ -364,15 +364,15 @@ int main()
|
||||
{
|
||||
auto& cameraNode = cameraEntity.get<Nz::NodeComponent>();
|
||||
|
||||
//physSystem.GetPhysWorld().SetGravity(cameraNode.GetBackward() * 9.81f);
|
||||
physSystem.GetPhysWorld().SetGravity(cameraNode.GetBackward() * 9.81f);
|
||||
}
|
||||
});
|
||||
|
||||
Nz::DegreeAnglef rotation = 0.f;
|
||||
app.AddUpdater([&](Nz::Time elapsedTime)
|
||||
{
|
||||
rotation += elapsedTime.AsSeconds() * 30.f;
|
||||
physSystem.GetPhysWorld().SetGravity(Nz::Quaternionf(Nz::EulerAnglesf(0.f, rotation, 0.f)) * Nz::Vector3f::Forward());
|
||||
rotation += elapsedTime.AsSeconds() * 45.f;
|
||||
//physSystem.GetPhysWorld().SetGravity(Nz::Quaternionf(Nz::EulerAnglesf(0.f, rotation, 0.f)) * Nz::Vector3f::Forward() * 10.f);
|
||||
});
|
||||
|
||||
Nz::MillisecondClock fpsClock;
|
||||
|
||||
@@ -52,6 +52,8 @@ int main()
|
||||
|
||||
physSytem.GetPhysWorld().SetGravity({ 0.f, -9.81f, 0.f });
|
||||
|
||||
std::optional<Nz::JoltCharacter> character;
|
||||
|
||||
entt::handle playerEntity = world.CreateEntity();
|
||||
entt::handle playerRotation = world.CreateEntity();
|
||||
entt::handle playerCamera = world.CreateEntity();
|
||||
@@ -61,8 +63,18 @@ int main()
|
||||
|
||||
auto playerCollider = std::make_shared<Nz::JoltBoxCollider3D>(Nz::Vector3f(0.2f, 1.8f, 0.2f));
|
||||
|
||||
auto& playerBody = playerEntity.emplace<Nz::JoltRigidBody3DComponent>(physSytem.CreateRigidBody(playerCollider));
|
||||
playerBody.SetMass(42.f);
|
||||
//auto& playerBody = playerEntity.emplace<Nz::JoltRigidBody3DComponent>(physSytem.CreateRigidBody(playerCollider));
|
||||
//playerBody.SetMass(42.f);
|
||||
|
||||
character.emplace(physSytem.GetPhysWorld(), playerCollider, Nz::Vector3f::Up() * 5.f);
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
{
|
||||
auto [position, rotation] = character->GetPositionAndRotation();
|
||||
|
||||
auto& playerNode = playerEntity.get<Nz::NodeComponent>();
|
||||
playerNode.SetTransform(position, rotation, Nz::Vector3f::Unit());
|
||||
});
|
||||
|
||||
std::shared_ptr<Nz::Mesh> colliderMesh = Nz::Mesh::Build(playerCollider->GenerateDebugMesh());
|
||||
std::shared_ptr<Nz::GraphicalMesh> colliderGraphicalMesh = Nz::GraphicalMesh::BuildFromMesh(*colliderMesh);
|
||||
@@ -215,7 +227,7 @@ int main()
|
||||
auto& bobGfx = bobEntity.emplace<Nz::GraphicsComponent>();
|
||||
bobGfx.AttachRenderable(bobModel);
|
||||
|
||||
auto& sharedSkeleton = bobEntity.emplace<Nz::SharedSkeletonComponent>(skeleton);
|
||||
bobEntity.emplace<Nz::SharedSkeletonComponent>(skeleton);
|
||||
|
||||
entt::handle sphereEntity = world.CreateEntity();
|
||||
{
|
||||
@@ -333,7 +345,10 @@ int main()
|
||||
|
||||
floorEntity.emplace<Nz::NodeComponent>();
|
||||
|
||||
auto& planeBody = floorEntity.emplace<Nz::JoltRigidBody3DComponent>(physSytem.CreateRigidBody(std::make_shared<Nz::JoltBoxCollider3D>(Nz::Vector3f(planeSize.x, 0.5f, planeSize.y))));
|
||||
auto floorCollider = std::make_shared<Nz::JoltBoxCollider3D>(Nz::Vector3f(planeSize.x, 1.f, planeSize.y));
|
||||
auto translatedFloorCollider = std::make_shared<Nz::JoltTranslatedRotatedCollider3D>(std::move(floorCollider), Nz::Vector3f::Down() * 0.5f);
|
||||
|
||||
auto& planeBody = floorEntity.emplace<Nz::JoltRigidBody3DComponent>(physSytem.CreateRigidBody(translatedFloorCollider));
|
||||
planeBody.SetMass(0.f);
|
||||
|
||||
std::shared_ptr<Nz::GraphicalMesh> boxMeshGfx = Nz::GraphicalMesh::Build(Nz::Primitive::Box(Nz::Vector3f(0.5f, 0.5f, 0.5f)), meshPrimitiveParams);
|
||||
@@ -344,6 +359,27 @@ int main()
|
||||
entt::handle boxEntity = world.CreateEntity();
|
||||
boxEntity.emplace<Nz::NodeComponent>();
|
||||
boxEntity.emplace<Nz::GraphicsComponent>().AttachRenderable(boxModel);
|
||||
|
||||
|
||||
std::shared_ptr<Nz::Model> colliderModel;
|
||||
{
|
||||
std::shared_ptr<Nz::MaterialInstance> colliderMat = Nz::Graphics::Instance()->GetDefaultMaterials().basicMaterial->Instantiate();
|
||||
colliderMat->SetValueProperty("BaseColor", Nz::Color::Green());
|
||||
colliderMat->UpdatePassesStates([](Nz::RenderStates& states)
|
||||
{
|
||||
states.primitiveMode = Nz::PrimitiveMode::LineList;
|
||||
return true;
|
||||
});
|
||||
|
||||
std::shared_ptr<Nz::Mesh> colliderMesh = Nz::Mesh::Build(translatedFloorCollider->GenerateDebugMesh());
|
||||
std::shared_ptr<Nz::GraphicalMesh> colliderGraphicalMesh = Nz::GraphicalMesh::BuildFromMesh(*colliderMesh);
|
||||
|
||||
colliderModel = std::make_shared<Nz::Model>(colliderGraphicalMesh);
|
||||
for (std::size_t i = 0; i < colliderModel->GetSubMeshCount(); ++i)
|
||||
colliderModel->SetMaterial(i, colliderMat);
|
||||
|
||||
planeGfx.AttachRenderable(std::move(colliderModel));
|
||||
}
|
||||
}
|
||||
|
||||
Nz::MillisecondClock fpsClock, updateClock;
|
||||
@@ -385,25 +421,32 @@ int main()
|
||||
{
|
||||
float updateTime = deltaTime->AsSeconds();
|
||||
|
||||
auto& playerBody = playerEntity.get<Nz::JoltRigidBody3DComponent>();
|
||||
//auto& playerBody = playerEntity.get<Nz::JoltRigidBody3DComponent>();
|
||||
//playerBody.SetAngularDamping(std::numeric_limits<float>::max());
|
||||
|
||||
float mass = playerBody.GetMass();
|
||||
Nz::Vector3f velocity = character->GetLinearVelocity();
|
||||
velocity.x = 0.f;
|
||||
velocity.z = 0.f;
|
||||
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::RShift))
|
||||
playerBody.AddForce(Nz::Vector3f(0.f, mass * 50.f, 0.f));
|
||||
{
|
||||
if (character->IsOnGround())
|
||||
velocity += Nz::Vector3f::Up() * 2.f;
|
||||
}
|
||||
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Up))
|
||||
playerBody.AddForce(Nz::Vector3f::Forward() * 25.f * mass, Nz::CoordSys::Local);
|
||||
velocity += Nz::Vector3f::Forward();
|
||||
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Down))
|
||||
playerBody.AddForce(Nz::Vector3f::Backward() * 25.f * mass, Nz::CoordSys::Local);
|
||||
velocity += Nz::Vector3f::Backward();
|
||||
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Left))
|
||||
playerBody.AddForce(Nz::Vector3f::Left() * 25.f * mass, Nz::CoordSys::Local);
|
||||
velocity += Nz::Vector3f::Left();
|
||||
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Right))
|
||||
playerBody.AddForce(Nz::Vector3f::Right() * 25.f * mass, Nz::CoordSys::Local);
|
||||
velocity += Nz::Vector3f::Right();
|
||||
|
||||
character->SetLinearVelocity(velocity);
|
||||
|
||||
float cameraSpeed = 2.f;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user