From c08675bdaaf9ee131d86854d3aa60f65ecc1847e Mon Sep 17 00:00:00 2001 From: tovjemam Date: Sun, 1 Mar 2026 18:21:14 +0100 Subject: [PATCH] Optimize sleeping vehicles --- src/collision/raycastvehicle.hpp | 22 ++++++++++++ src/game/openworld.cpp | 57 +++++++++++++++++++++----------- src/game/vehicle.cpp | 21 ++++++++++-- src/game/vehicle.hpp | 3 +- 4 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 src/collision/raycastvehicle.hpp diff --git a/src/collision/raycastvehicle.hpp b/src/collision/raycastvehicle.hpp new file mode 100644 index 0000000..e87bf0f --- /dev/null +++ b/src/collision/raycastvehicle.hpp @@ -0,0 +1,22 @@ +#include + +namespace collision +{ + +class RaycastVehicle : public btRaycastVehicle +{ +public: + RaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis, btVehicleRaycaster* raycaster) : + btRaycastVehicle(tuning, chassis, raycaster) + { + } + + virtual void updateAction(btCollisionWorld* collisionWorld, btScalar step) override + { + // only update if not sleeping + if (getRigidBody()->isActive()) + btRaycastVehicle::updateAction(collisionWorld, step); + } +}; + +} \ No newline at end of file diff --git a/src/game/openworld.cpp b/src/game/openworld.cpp index aedeb44..0f1c677 100644 --- a/src/game/openworld.cpp +++ b/src/game/openworld.cpp @@ -15,6 +15,28 @@ namespace game } // namespace game + +static const char* GetRandomCarModel() +{ + const char* vehicles[] = {"pickup_hd", "passat", "twingo", "polskifiat"}; + return vehicles[rand() % (sizeof(vehicles) / sizeof(vehicles[0]))]; +} + +static glm::vec3 GetRandomColor() +{ + glm::vec3 color; + // shittiest way to do it + for (int i = 0; i < 3; ++i) + { + net::ColorQ qcol; + qcol.value = rand() % 256; + color[i] = qcol.Decode(); + } + + return color; +} + + game::OpenWorld::OpenWorld() : World("openworld") { srand(time(NULL)); @@ -27,6 +49,21 @@ game::OpenWorld::OpenWorld() : World("openworld") auto& veh = Spawn("twingo", glm::vec3{0.8f, 0.1f, 0.1f}); veh.SetPosition({110.0f, 100.0f, 5.0f}); + + constexpr size_t in_row = 20; + + for (size_t i = 0; i < 3000; ++i) + { + Schedule(i * 40, [this, i] { + size_t col = i % in_row; + size_t row = i / in_row; + glm::vec3 pos(62.0f + static_cast(col) * 4.0f, 165.0f + static_cast(row) * 7.0f, 7.0f); + + auto& veh = Spawn(GetRandomCarModel(), GetRandomColor()); + veh.SetPosition(pos); + }); + } + } void game::OpenWorld::Update(int64_t delta_time) @@ -119,20 +156,6 @@ std::optional> game::OpenWorld: } -static glm::vec3 GetRandomColor() -{ - glm::vec3 color; - // shittiest way to do it - for (int i = 0; i < 3; ++i) - { - net::ColorQ qcol; - qcol.value = rand() % 256; - color[i] = qcol.Decode(); - } - - return color; -} - template static T& SpawnRandomCharacter(game::OpenWorld& world, TArgs&&... args) { @@ -166,12 +189,6 @@ void game::OpenWorld::RemovePlayerCharacter(Player& player) } } -static const char* GetRandomCarModel() -{ - const char* vehicles[] = {"pickup_hd", "passat", "twingo", "polskifiat"}; - return vehicles[rand() % (sizeof(vehicles) / sizeof(vehicles[0]))]; -} - static game::DrivableVehicle& SpawnRandomVehicle(game::World& world) { auto roads = world.GetMap().GetGraph("roads"); diff --git a/src/game/vehicle.cpp b/src/game/vehicle.cpp index f779716..43d6980 100644 --- a/src/game/vehicle.cpp +++ b/src/game/vehicle.cpp @@ -32,13 +32,13 @@ game::Vehicle::Vehicle(World& world, std::string model_name, const glm::vec3& co btRigidBody::btRigidBodyConstructionInfo rb_info(mass, &motion_, shape, local_inertia); body_ = std::make_unique(rb_info); - body_->setActivationState(DISABLE_DEACTIVATION); + // body_->setActivationState(DISABLE_DEACTIVATION); collision::SetObjectInfo(body_.get(), collision::OT_ENTITY, collision::OF_NOTIFY_CONTACT, this); // setup vehicle btRaycastVehicle::btVehicleTuning tuning; - vehicle_ = std::make_unique(tuning, body_.get(), &world_.GetVehicleRaycaster()); + vehicle_ = std::make_unique(tuning, body_.get(), &world_.GetVehicleRaycaster()); vehicle_->setCoordinateSystem(0, 2, 1); // setup wheels @@ -94,6 +94,8 @@ game::Vehicle::Vehicle(World& world, std::string model_name, const glm::vec3& co auto& bt_world = world_.GetBtWorld(); bt_world.addRigidBody(body_.get(), btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::AllFilter); bt_world.addAction(vehicle_.get()); + + Update(); } void game::Vehicle::Update() @@ -197,6 +199,21 @@ void game::Vehicle::ProcessInput() { // TODO: totally fix + //std::string nt = ""; + + if (in_) { + //nt += "in "; + // body_->setActivationState(ACTIVE_TAG); + body_->activate(); + } + else + { + //nt += "no in"; + } + + //nt += std::to_string(body_->getActivationState()); + //SetNametag(nt); + float t_delta = 1.0f / 25.0f; // float steeringIncrement = .04 * 60; diff --git a/src/game/vehicle.hpp b/src/game/vehicle.hpp index f6f03c4..bf46de1 100644 --- a/src/game/vehicle.hpp +++ b/src/game/vehicle.hpp @@ -5,6 +5,7 @@ #include "assets/vehiclemdl.hpp" #include "collision/motionstate.hpp" +#include "collision/raycastvehicle.hpp" #include "entity.hpp" #include "world.hpp" #include "vehicle_sync.hpp" @@ -74,7 +75,7 @@ private: collision::MotionState motion_; std::unique_ptr body_; - std::unique_ptr vehicle_; + std::unique_ptr vehicle_; float steering_ = 0.0f; bool steering_analog_ = false;