Skip to content

Commit 3f89aeb

Browse files
committed
FPCharacter object[2]
- Currently cannot move the character controller - Sometimes falling trough the static colliders
1 parent 3779692 commit 3f89aeb

13 files changed

Lines changed: 73 additions & 187 deletions

File tree

3DRadSpace/3DRadSpace_CPPSample/Main.cpp

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,4 @@
1-
#include <Engine3DRadSpace/Games/Game.hpp>
2-
#include <Engine3DRadSpace/Graphics/Rendering/PostProcessEffect.hpp>
3-
#include <Engine3DRadSpace/Graphics/Rendering/LinearPixelFogEffect.hpp>
4-
5-
using namespace Engine3DRadSpace;
6-
using namespace Engine3DRadSpace::Graphics;
7-
using namespace Engine3DRadSpace::Graphics::Rendering;
8-
9-
ShaderDescFile mandelbrotShaderDescFile("MandelbrotEffect.hlsl", "Mandelbrot_PixelShader", ShaderType::Fragment, ShaderFeatureLevel::DX_V5);
10-
11-
class MandelbrotEffect : public PostProcessEffect
12-
{
13-
public:
14-
MandelbrotEffect(IGraphicsDevice* device) : PostProcessEffect(device, mandelbrotShaderDescFile)
15-
{
16-
Enabled = true;
17-
NotDepthAware = true;
18-
}
19-
~MandelbrotEffect() override = default;
20-
};
21-
22-
class MyGame : public Engine3DRadSpace::Game
23-
{
24-
public:
25-
MyGame() : Game("MyGame")
26-
{
27-
}
28-
29-
void Load() override
30-
{
31-
Game::Load();
32-
33-
PostProcesses->Add<MandelbrotEffect>();
34-
}
35-
36-
~MyGame() override = default;
37-
};
1+
#include "MyGame.hpp"
382

393
int main()
404
{
Lines changed: 6 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,6 @@
1-
//#include "MyGame.hpp"
2-
//#include <Engine3DRadSpace\Objects\Impl\Skinmesh.hpp>
3-
//#include <Engine3DRadSpace\Objects\Impl\Camera.hpp>
4-
//#include <Engine3DRadSpace\Objects\ObjectList.hpp>
5-
//#include <Engine3DRadSpace\Math\Quaternion.hpp>
6-
//#include <Engine3DRadSpace\Graphics\Primitives\Box.hpp>
7-
//
8-
//using namespace Engine3DRadSpace;
9-
//using namespace Engine3DRadSpace::Objects;
10-
//using namespace Engine3DRadSpace::Input;
11-
//using namespace Engine3DRadSpace::Math;
12-
//
13-
//using namespace Engine3DRadSpace::Graphics;
14-
//using namespace Engine3DRadSpace::Graphics::Primitives;
15-
//
16-
//std::unique_ptr<Box> box;
17-
//
18-
//MyGame::MyGame() : Game("3DRadSpace Empty", 800, 600)
19-
//{
20-
//}
21-
//
22-
//void MyGame::Initialize()
23-
//{
24-
// Game::Initialize();
25-
// Device->ImmediateContext()->ToggleFullScreen();
26-
//
27-
// this->ClearColor = Colors::CornflowerBlue;
28-
//
29-
// //Add a camera viewing the fish model
30-
// Camera cam;
31-
// cam.Position = { 0, 1, 1.5 };
32-
// cam.Rotation = Quaternion::FromYawPitchRoll(0, 0, 0);
33-
// cam.LookAt = { 0.5f,0.5f,0.5f };
34-
// cam.LookMode = Camera::CameraMode::UseLookAtCoordinates;
35-
// _cam = this->Objects->AddNew(std::move(cam)).first; //first is object pointer, second is ID.
36-
//
37-
// //Directly create a fish model into the scene
38-
// //_fish = this->Objects->AddNew<Skinmesh>("fish", true, "Data\\Models\\YellowFish.x").first;
39-
//
40-
// box = std::make_unique<Box>(Device.get(), BoundingBox(Vector3(0,0,0), Vector3(1,1,1)), Colors::Orange);
41-
//}
42-
//
43-
//void MyGame::Load()
44-
//{
45-
// Game::Load();
46-
//
47-
// _sound = this->Content->Load<Sound>("Data\\Sounds\\explosion.ogg");
48-
// _soundInstance = std::make_unique<SoundInstance>(_sound);
49-
// _soundInstance->SetLooping(false);
50-
//
51-
// // zoom the camera into the model.
52-
// //float distance = _fish->GetModel()->GetBoundingSphere().Radius + 0.25f;
53-
// _cam->Position *= 2;
54-
//}
55-
//
56-
//void MyGame::Update()
57-
//{
58-
// Game::Update();
59-
//
60-
// if(Keyboard.IsKeyDown(Key::X))
61-
// {
62-
// _soundInstance->Play();
63-
// }
64-
//
65-
// /*
66-
// if(Keyboard.IsKeyDown(Key::X))
67-
// {
68-
// _fish->Rotation *= Quaternion::FromYawPitchRoll(0, this->Draw_dt, 0);
69-
// }
70-
// if(Keyboard.IsKeyDown(Key::Y))
71-
// {
72-
// _fish->Rotation *= Quaternion::FromYawPitchRoll(this->Draw_dt, 0, 0);
73-
// }
74-
// if(Keyboard.IsKeyDown(Key::Z))
75-
// {
76-
// _fish->Rotation *= Quaternion::FromYawPitchRoll(0, 0, this->Draw_dt);
77-
// }
78-
// if(Keyboard.IsKeyDown(Key::R))
79-
// {
80-
// _fish->Rotation = Quaternion();
81-
// }
82-
// _fish->Rotation.Normalize();
83-
// */
84-
//}
85-
//
86-
//void MyGame::Draw3D()
87-
//{
88-
// Game::Draw3D();
89-
//
90-
// box->Transform = _cam->GetViewMatrix() * _cam->GetProjectionMatrix();
91-
// box->Draw3D();
92-
//}
93-
//
94-
//void MyGame::Draw2D()
95-
//{
96-
// Game::Draw2D();
97-
//}
1+
#include "MyGame.hpp"
2+
3+
MyGame::MyGame() : Game("FPCharacter Demo", 1280, 720)
4+
{
5+
AppendScene(R"(D:\Projects\3D_Rad_Space\3DRadSpace\out\build\x64-Release\Projects\FPCharacter Demo.3drsp)");
6+
}
Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
//#pragma once
2-
//#include <Engine3DRadSpace\Games\Game.hpp>
3-
//#include <Engine3DRadSpace\Objects\Impl\Skinmesh.hpp>
4-
//#include <Engine3DRadSpace\Objects\Impl\Camera.hpp>
5-
//#include <Engine3DRadSpace\Audio\SoundInstance.hpp>
6-
//
7-
//using namespace Engine3DRadSpace;
8-
//using namespace Engine3DRadSpace::Audio;
9-
//using namespace Engine3DRadSpace::Objects;
10-
//using namespace Engine3DRadSpace::Graphics;
11-
//
12-
//class MyGame : public Game
13-
//{
14-
// Skinmesh* _fish = nullptr;
15-
// Camera* _cam = nullptr;
16-
//
17-
// Sound* _sound = nullptr;
18-
// std::unique_ptr<SoundInstance> _soundInstance;
19-
//public:
20-
// MyGame();
21-
//
22-
// void Initialize() override;
23-
// void Load() override;
24-
// void Update() override;
25-
// void Draw3D() override;
26-
// void Draw2D() override;
27-
//
28-
// ~MyGame() = default;
29-
//};
1+
#pragma once
2+
#include <Engine3DRadSpace\Games\Game.hpp>
3+
#include <Engine3DRadSpace\Objects\Impl\Skinmesh.hpp>
4+
#include <Engine3DRadSpace\Physics\Objects\FPCharacter.hpp>
5+
#include <Engine3DRadSpace\Audio\SoundInstance.hpp>
6+
7+
using namespace Engine3DRadSpace;
8+
using namespace Engine3DRadSpace::Audio;
9+
using namespace Engine3DRadSpace::Objects;
10+
using namespace Engine3DRadSpace::Graphics;
11+
12+
class MyGame : public Game
13+
{
14+
public:
15+
MyGame();
16+
17+
//void Initialize() override;
18+
//void Load() override;
19+
//void Update() override;
20+
//void Draw3D() override;
21+
//void Draw2D() override;
22+
23+
~MyGame() = default;
24+
};

3DRadSpace/Engine3DRadSpace/Games/Game.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ void Game::RunOneFrame()
134134

135135
//PostProcesses->DisableAll(); //to be re-enabled by the objects
136136
Audio->Update();
137+
if(Physics) Physics->Simulate(static_cast<float>(Update_dt));
137138
Update();
138139

139140
auto ts_d1 = std::chrono::high_resolution_clock::now();

3DRadSpace/Engine3DRadSpace/Objects/Impl/FreeCam.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ FreeCam::FreeCam(
2525

2626
void FreeCam::Update()
2727
{
28-
if(Enabled) return;
28+
if(!Enabled) return;
2929
if (Frozen)
3030
{
3131
Camera::Update();
@@ -55,6 +55,7 @@ void FreeCam::Update()
5555

5656
auto &kb = game->Keyboard;
5757

58+
_dir = Vector3::Zero();
5859
if(kb.IsKeyDown(Forward)) _dir += _fwd;
5960
if(kb.IsKeyDown(Backward)) _dir -= _fwd;
6061
if(kb.IsKeyDown(Left)) _dir -= right;

3DRadSpace/Engine3DRadSpace/Physics/IPhysicsEngine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Engine3DRadSpace::Physics
2727
const Math::Vector3 &scale = Math::Vector3::One()
2828
) = 0;
2929
virtual std::unique_ptr<IDynamicCollider> CreateDynamicCollider() = 0;
30-
virtual std::unique_ptr<ICharacterController> CreateCharacterController(float radius, float height) = 0;
30+
virtual std::unique_ptr<ICharacterController> CreateCharacterController(float radius, float height, const Math::Vector3& position = Math::Vector3::Zero()) = 0;
3131
virtual double dt() const noexcept = 0;
3232

3333
~IPhysicsEngine() override = default;

3DRadSpace/Engine3DRadSpace/Physics/NVPhysX/CharacterController.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ using namespace Engine3DRadSpace::Physics::NVPhysX;
1717
CharacterController::CharacterController(
1818
IPhysicsEngine* physics,
1919
float height,
20-
float radius
20+
float radius,
21+
const Math::Vector3& position
2122
) : ICharacterController(physics)
2223
{
2324
_height = height;
2425
_radius = radius;
26+
_position = position;
2527

2628
auto nvPhysics = static_cast<physx::PxPhysics*>(static_cast<PhysicsEngine*>(physics)->GetPhysics());
2729
auto nvControllerManager = static_cast<physx::PxControllerManager*>(static_cast<PhysicsEngine*>(physics)->_controllerManager.get());
2830

2931
physx::PxCapsuleControllerDesc desc;
3032
desc.setToDefault();
33+
desc.position = physx::PxExtendedVec3(position.X, position.Y, position.Z);
3134
desc.height = height;
3235
desc.radius = radius;
3336
desc.slopeLimit = std::cos(_maxSlopeAngle);
@@ -210,7 +213,12 @@ void CharacterController::UpdateTransform()
210213
if(!_controller) return;
211214

212215
auto p = _controller->getPosition();
213-
_position = Vector3(static_cast<float>(p.x), static_cast<float>(p.y), static_cast<float>(p.z));
216+
Vector3 candidate(static_cast<float>(p.x), static_cast<float>(p.y), static_cast<float>(p.z));
217+
218+
// Guard against NaN/inf coming from a diverged simulation (e.g. character fell
219+
// out of the physics world). Retain the last known-good position instead.
220+
if(std::isfinite(candidate.X) && std::isfinite(candidate.Y) && std::isfinite(candidate.Z))
221+
_position = candidate;
214222
}
215223

216224
void CharacterController::UpdateTransform(const Math::Vector3& position, const Math::Quaternion& rotation)
@@ -228,6 +236,7 @@ Math::Vector3 CharacterController::_getPosition() const
228236

229237
void CharacterController::_setPosition(const Math::Vector3& position)
230238
{
239+
_position = position; // keep cache in sync immediately
231240
if(!_controller) return;
232241
_controller->setPosition(physx::PxExtendedVec3(position.X, position.Y, position.Z));
233242
}

3DRadSpace/Engine3DRadSpace/Physics/NVPhysX/CharacterController.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Engine3DRadSpace::Physics::NVPhysX
3535

3636
Math::Vector3 _position;
3737
public:
38-
CharacterController(IPhysicsEngine* physics, float height, float radius);
38+
CharacterController(IPhysicsEngine* physics, float height, float radius, const Math::Vector3& position = Math::Vector3::Zero());
3939

4040
std::optional<float> Intersects(const Math::Ray& r) override;
4141
void UpdateTransform() override;

3DRadSpace/Engine3DRadSpace/Physics/NVPhysX/PhysicsEngine.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ void PhysicsEngine::Simulate(float dt)
6969
{
7070
for (_accTimer += dt; _accTimer >= _timeStep; _accTimer -= _timeStep)
7171
{
72-
_scene->simulate(dt);
73-
_scene->fetchResults(false);
72+
_scene->simulate(static_cast<float>(_timeStep));
73+
_scene->fetchResults(true);
7474
}
7575
}
7676

@@ -131,12 +131,12 @@ std::unique_ptr<IDynamicCollider> PhysicsEngine::CreateDynamicCollider()
131131
return std::make_unique<DynamicCollider>(this);
132132
}
133133

134-
std::unique_ptr<ICharacterController> PhysicsEngine::CreateCharacterController(float radius, float height)
134+
std::unique_ptr<ICharacterController> PhysicsEngine::CreateCharacterController(float radius, float height, const Math::Vector3& position)
135135
{
136-
return std::make_unique<CharacterController>(this, radius, height);
136+
return std::make_unique<CharacterController>(this, height, radius, position);
137137
}
138138

139139
double PhysicsEngine::dt() const noexcept
140140
{
141-
return _dt;
141+
return _timeStep;
142142
}

3DRadSpace/Engine3DRadSpace/Physics/NVPhysX/PhysicsEngine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace Engine3DRadSpace::Physics::NVPhysX
6767
) override;
6868
std::unique_ptr<IDynamicCollider> CreateDynamicCollider() override;
6969

70-
std::unique_ptr<ICharacterController> CreateCharacterController(float radius, float height) override;
70+
std::unique_ptr<ICharacterController> CreateCharacterController(float radius, float height, const Math::Vector3& position = Math::Vector3::Zero()) override;
7171
double dt() const noexcept override;
7272

7373
friend class CharacterController;

0 commit comments

Comments
 (0)