Optimize sleeping vehicles
This commit is contained in:
parent
85afa79312
commit
c08675bdaa
22
src/collision/raycastvehicle.hpp
Normal file
22
src/collision/raycastvehicle.hpp
Normal 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);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -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<game::DrivableVehicle>("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<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)
|
||||
@ -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>
|
||||
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");
|
||||
|
||||
@ -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<btRigidBody>(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<btRaycastVehicle>(tuning, body_.get(), &world_.GetVehicleRaycaster());
|
||||
vehicle_ = std::make_unique<collision::RaycastVehicle>(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;
|
||||
|
||||
@ -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<btRigidBody> body_;
|
||||
std::unique_ptr<btRaycastVehicle> vehicle_;
|
||||
std::unique_ptr<collision::RaycastVehicle> vehicle_;
|
||||
|
||||
float steering_ = 0.0f;
|
||||
bool steering_analog_ = false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user