Optimize sleeping vehicles

This commit is contained in:
tovjemam 2026-03-01 18:21:14 +01:00
parent 85afa79312
commit c08675bdaa
4 changed files with 80 additions and 23 deletions

View File

@ -0,0 +1,22 @@
#include <btBulletDynamicsCommon.h>
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);
}
};
}

View File

@ -15,6 +15,28 @@ namespace game
} // 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") game::OpenWorld::OpenWorld() : World("openworld")
{ {
srand(time(NULL)); srand(time(NULL));
@ -27,6 +49,21 @@ game::OpenWorld::OpenWorld() : World("openworld")
auto& veh = Spawn<game::DrivableVehicle>("twingo", glm::vec3{0.8f, 0.1f, 0.1f}); auto& veh = Spawn<game::DrivableVehicle>("twingo", glm::vec3{0.8f, 0.1f, 0.1f});
veh.SetPosition({110.0f, 100.0f, 5.0f}); 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<float>(col) * 4.0f, 165.0f + static_cast<float>(row) * 7.0f, 7.0f);
auto& veh = Spawn<game::DrivableVehicle>(GetRandomCarModel(), GetRandomColor());
veh.SetPosition(pos);
});
}
} }
void game::OpenWorld::Update(int64_t delta_time) void game::OpenWorld::Update(int64_t delta_time)
@ -119,20 +156,6 @@ std::optional<std::pair<game::Usable&, const game::UseTarget&>> 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 <class T, typename... TArgs> template <class T, typename... TArgs>
static T& SpawnRandomCharacter(game::OpenWorld& world, TArgs&&... args) 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) static game::DrivableVehicle& SpawnRandomVehicle(game::World& world)
{ {
auto roads = world.GetMap().GetGraph("roads"); auto roads = world.GetMap().GetGraph("roads");

View File

@ -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); btRigidBody::btRigidBodyConstructionInfo rb_info(mass, &motion_, shape, local_inertia);
body_ = std::make_unique<btRigidBody>(rb_info); body_ = std::make_unique<btRigidBody>(rb_info);
body_->setActivationState(DISABLE_DEACTIVATION); // body_->setActivationState(DISABLE_DEACTIVATION);
collision::SetObjectInfo(body_.get(), collision::OT_ENTITY, collision::OF_NOTIFY_CONTACT, this); collision::SetObjectInfo(body_.get(), collision::OT_ENTITY, collision::OF_NOTIFY_CONTACT, this);
// setup vehicle // setup vehicle
btRaycastVehicle::btVehicleTuning tuning; btRaycastVehicle::btVehicleTuning tuning;
vehicle_ = std::make_unique<btRaycastVehicle>(tuning, body_.get(), &world_.GetVehicleRaycaster()); vehicle_ = std::make_unique<collision::RaycastVehicle>(tuning, body_.get(), &world_.GetVehicleRaycaster());
vehicle_->setCoordinateSystem(0, 2, 1); vehicle_->setCoordinateSystem(0, 2, 1);
// setup wheels // setup wheels
@ -94,6 +94,8 @@ game::Vehicle::Vehicle(World& world, std::string model_name, const glm::vec3& co
auto& bt_world = world_.GetBtWorld(); auto& bt_world = world_.GetBtWorld();
bt_world.addRigidBody(body_.get(), btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::AllFilter); bt_world.addRigidBody(body_.get(), btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::AllFilter);
bt_world.addAction(vehicle_.get()); bt_world.addAction(vehicle_.get());
Update();
} }
void game::Vehicle::Update() void game::Vehicle::Update()
@ -197,6 +199,21 @@ void game::Vehicle::ProcessInput()
{ {
// TODO: totally fix // 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 t_delta = 1.0f / 25.0f;
// float steeringIncrement = .04 * 60; // float steeringIncrement = .04 * 60;

View File

@ -5,6 +5,7 @@
#include "assets/vehiclemdl.hpp" #include "assets/vehiclemdl.hpp"
#include "collision/motionstate.hpp" #include "collision/motionstate.hpp"
#include "collision/raycastvehicle.hpp"
#include "entity.hpp" #include "entity.hpp"
#include "world.hpp" #include "world.hpp"
#include "vehicle_sync.hpp" #include "vehicle_sync.hpp"
@ -74,7 +75,7 @@ private:
collision::MotionState motion_; collision::MotionState motion_;
std::unique_ptr<btRigidBody> body_; std::unique_ptr<btRigidBody> body_;
std::unique_ptr<btRaycastVehicle> vehicle_; std::unique_ptr<collision::RaycastVehicle> vehicle_;
float steering_ = 0.0f; float steering_ = 0.0f;
bool steering_analog_ = false; bool steering_analog_ = false;