converted to Jolt physics

This commit is contained in:
2025-11-23 02:00:31 +03:00
parent 3f0484e87c
commit cd82fb0eed
52 changed files with 4302 additions and 1458 deletions

View File

@@ -1,7 +1,6 @@
#include <unordered_set>
#include <iostream>
#include <Ogre.h>
#include <OgreBullet.h>
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
#include <OgreTerrain.h>
#include <OgreTerrainGroup.h>
@@ -14,6 +13,7 @@
#include "Components.h"
#include "CharacterModule.h"
#include "SunModule.h"
#include "PhysicsModule.h"
#include "TerrainModule.h"
#define TERRAIN_SIZE 129
@@ -173,7 +173,7 @@ class FlatTerrainDefiner
: public Ogre::TerrainPagedWorldSection::TerrainDefiner,
public Ogre::FrameListener {
Ogre::SceneManager *mScnMgr;
Ogre::Bullet::DynamicsWorld *mWorld;
// Ogre::Bullet::DynamicsWorld *mWorld;
struct gen_collider {
Ogre::TerrainGroup *group;
long x;
@@ -182,18 +182,44 @@ class FlatTerrainDefiner
std::deque<struct gen_collider> collider_queue;
public:
FlatTerrainDefiner(Ogre::SceneManager *scm,
Ogre::Bullet::DynamicsWorld *world)
FlatTerrainDefiner(Ogre::SceneManager *
scm /*, Ogre::Bullet::DynamicsWorld *world */)
: Ogre::TerrainPagedWorldSection::TerrainDefiner()
, Ogre::FrameListener()
, mScnMgr(scm)
, mWorld(world)
// , mWorld(world)
{
Ogre::Root::getSingleton().addFrameListener(this);
}
private:
public:
void createTerrainChunk(Ogre::TerrainGroup *terrainGroup, long x,
long y)
{
Ogre::Terrain *terrain = terrainGroup->getTerrain(x, y);
float minH = terrain->getMinHeight();
float maxH = terrain->getMaxHeight();
uint16_t terrainSize = terrainGroup->getTerrainSize();
OgreAssert(terrain && terrain->getHeightData() &&
terrain->isLoaded(),
"invalid terrain supplied");
uint16_t size = terrain->getSize();
float *heightData = terrain->getHeightData();
Ogre::SceneNode *node = terrain->_getRootSceneNode();
float worldSize = terrain->getWorldSize();
float scaled = worldSize / (size - 1);
Ogre::Vector3 bodyPosition = terrain->getPosition();
bodyPosition.y += (maxH + minH) / 2.0f;
bodyPosition.x += worldSize / 2.0f;
bodyPosition.z += worldSize / 2.0f;
Ogre::Vector3 offset =
node->_getDerivedPosition() - bodyPosition;
flecs::entity e = PhysicsModule::createTerrainChunkBody(
node, heightData, offset,
Ogre::Vector3(scaled, 1, scaled), size);
node->getUserObjectBindings().setUserAny("_collider", e);
}
void define(Ogre::TerrainGroup *terrainGroup, long x, long y) override
{
uint16_t terrainSize = terrainGroup->getTerrainSize();
@@ -258,6 +284,10 @@ public:
float maxH = terrain->getMaxHeight();
int size = terrain->getSize();
float worldSize = terrain->getWorldSize();
{
createTerrainChunk(group, x, y);
}
#if 0
if (true) {
btRigidBody *body =
mWorld->addTerrainRigidBody(
@@ -297,6 +327,7 @@ public:
y));
created = true;
}
#endif
collider_queue.pop_front();
// FIXME: create entities and components instead
Ogre::SceneNode *items =
@@ -317,11 +348,6 @@ public:
what->attachObject(ent);
what->setOrientation(item.rotation);
what->setPosition(item.position);
ECS::get<EngineData>()
.mWorld->addRigidBody(
0, ent,
Ogre::Bullet::CT_TRIMESH,
nullptr, 2, 0x7fffffff);
}
} else {
output.push_back(collider_queue.front());
@@ -338,13 +364,10 @@ public:
};
class DummyPageProvider : public Ogre::PageProvider {
public:
DummyPageProvider(btDynamicsWorld *world)
DummyPageProvider(/* btDynamicsWorld *world */)
: Ogre::PageProvider()
, mBtWorld(world)
{
}
std::unordered_map<Ogre::PageID, btRigidBody *> body;
btDynamicsWorld *mBtWorld;
bool prepareProceduralPage(Ogre::Page *page,
Ogre::PagedWorldSection *section)
{
@@ -377,6 +400,7 @@ TerrainModule::TerrainModule(flecs::world &ecs)
ecs.component<CanSetPlayerPosition>().add(flecs::Singleton);
ecs.component<Terrain>().add(flecs::Singleton);
ecs.component<TerrainPrivate>().add(flecs::Singleton);
ecs.component<PlacementObjects>();
ecs.set<TerrainPrivate>({ nullptr, {} });
ecs.system<const EngineData, const Camera, const Sun, Terrain,
TerrainPrivate>("SetupUpdateTerrain")
@@ -389,8 +413,8 @@ TerrainModule::TerrainModule(flecs::world &ecs)
if (!priv.mDummyPageProvider)
priv.mDummyPageProvider =
new DummyPageProvider(
eng.mWorld
->getBtWorld());
/* eng.mWorld
->getBtWorld() */);
terrain.mTerrainGlobals =
OGRE_NEW Ogre::TerrainGlobalOptions();
@@ -477,7 +501,7 @@ TerrainModule::TerrainModule(flecs::world &ecs)
terrain.mTerrainPagedWorldSection->setDefiner(
OGRE_NEW FlatTerrainDefiner(
eng.mScnMgr, eng.mWorld));
eng.mScnMgr /*, eng.mWorld */));
terrain.mTerrainGroup->freeTemporaryResources();
std::cout << "Terrain setup done\n";