载入中...
搜索中...
未找到
VehicleTireRuntime.cpp
浏览该文件的文档.
1// eve-hot-path
3#include <algorithm>
4#include <cmath>
5#include <optional>
8namespace eve::vehicle {
10public:
11 std::optional<physics::ForceReceipt> receipt;
12 std::vector<VehicleEntity::SuspensionState::WheelState> candidate;
13 bool published = false;
14};
15namespace tire_detail {
16Result<VehiclePhysicsStatus> synchronize(VehicleEntity& vehicle, std::shared_ptr<VehicleWheelRuntime>& runtime) {
19 if (runtime->receipt->status() == ForceStepStatus::Pending)
21 if (runtime->receipt->status() == ForceStepStatus::Cancelled) {
22 runtime.reset();
24 }
25 if (!runtime->published) {
26 vehicle.suspension()->wheels = std::move(runtime->candidate);
27 runtime->published = true;
28 }
30}
31Result<VehiclePhysicsStatus> submissionState(const std::shared_ptr<VehicleWheelRuntime>& runtime,
32 const SimulationStep& step) {
34 auto reject = [](std::string message) {
37 };
38 if (runtime) {
39 if (runtime->receipt->status() == ForceStepStatus::Pending) {
40 const auto previous = runtime->receipt->step();
41 if (previous.tick != step.tick || previous.delta != step.delta)
42 return reject("Complete the pending vehicle world step before submitting another");
44 }
45 if (!runtime->published) return reject("Synchronize the completed vehicle step before submitting another");
46 if (runtime->receipt->step().tick == step.tick && runtime->receipt->step().delta == step.delta)
48 }
50}
54 result.inertia = config.wheelInertia;
55 result.friction = config.tireFriction;
56 result.longitudinalStiffness = config.longitudinalStiffness;
57 result.corneringStiffness = config.corneringStiffness;
58 result.rollingResistance = config.rollingResistance;
59 return result;
60}
62 const SuspensionWheel& wheel, std::size_t driven, double maximumSpeed, double traction) {
63 const double throttle = std::clamp(double(input.throttle), -1.0, 1.0);
64 const double speedLimit = std::abs(throttle) * maximumSpeed;
65 const bool driving =
66 driven > 0 && wheel.drive && throttle * target.longitudinalVelocity < std::abs(throttle) * speedLimit;
67 target.driveTorque = driving ? traction * throttle * wheel.radius / static_cast<double>(driven) : 0;
68 target.brakeTorque = config.brakeTorque * (input.handbrake ? 1 : std::clamp(double(input.brake), 0.0, 1.0));
69}
70namespace {
71template <class W>
72Result<VehiclePhysicsStatus> commitImpl(W& world, physics::ForceBatch& batch,
73 std::shared_ptr<VehicleWheelRuntime>& runtime,
74 std::vector<VehicleEntity::SuspensionState::WheelState> candidate,
75 const SimulationStep& step) {
76 // Reserve all candidate storage before commit. Construct receipt state after commit without allocation.
77 auto next = std::make_shared<VehicleWheelRuntime>();
78 auto submitted = batch.commit(world, step);
79 if (!submitted) return Result<VehiclePhysicsStatus>::failure(submitted.status());
80 next->receipt = std::move(submitted).takeValue();
81 next->candidate = std::move(candidate);
82 next->published = false;
83 runtime = std::move(next);
85}
86} // namespace
88 std::shared_ptr<VehicleWheelRuntime>& runtime,
89 std::vector<VehicleEntity::SuspensionState::WheelState> candidate,
90 const SimulationStep& step) {
91 return commitImpl(world, batch, runtime, std::move(candidate), step);
92}
94 std::shared_ptr<VehicleWheelRuntime>& runtime,
95 std::vector<VehicleEntity::SuspensionState::WheelState> candidate,
96 const SimulationStep& step) {
97 return commitImpl(world, batch, runtime, std::move(candidate), step);
98}
99} // namespace tire_detail
100} // namespace eve::vehicle
LogicalId target
EvpackChunkInput input
Definition Evpack.cpp:170
std::string message
Poco::JSON::Object::Ptr result
double radius
Definition MeleeHit.cpp:42
std::shared_ptr< const OceanSprayFrame > previous
glm::mat4 model
float step
Definition TreeMesh.cpp:451
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Move-only, single-use collection of validated force requests. @ownership Owns values and handles only...
Definition ForceBatch.h:26
Result< ForceReceipt > commit(World &world, const SimulationStep &step)
Revalidate all planar requests and submit once; rejects consumed or foreign batches.
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:47
Box2D world wrapper (2D physics) with pixel-space coordinates. Handles stepping, gravity,...
Definition World.h:44
载具实体:数据全部在组件里,行为在 VehicleSystem。
std::vector< VehicleEntity::SuspensionState::WheelState > candidate
std::optional< physics::ForceReceipt > receipt
ForceStepStatus
Completion of the world step to which a submitted force batch belongs.
Definition ForceReceipt.h:9
Result< VehiclePhysicsStatus > synchronize(VehicleEntity &vehicle, std::shared_ptr< VehicleWheelRuntime > &runtime)
Result< VehiclePhysicsStatus > submissionState(const std::shared_ptr< VehicleWheelRuntime > &runtime, const SimulationStep &step)
Result< VehiclePhysicsStatus > commit(physics::World &world, physics::ForceBatch &batch, std::shared_ptr< VehicleWheelRuntime > &runtime, std::vector< VehicleEntity::SuspensionState::WheelState > candidate, const SimulationStep &step)
void driveInputs(physics::TireInput &target, const SuspensionConfig &config, const VehicleEntity::Input &input, const SuspensionWheel &wheel, std::size_t driven, double maximumSpeed, double traction)
驾驶者接口与玩家控制状态。
@ Pending
Forces submitted; the world has not completed their step.
@ Unavailable
No physics module or no attached body.
@ Applied
Physics handled the request.
@ Cancelled
Restore or destruction cancelled a pending step.
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158
Contact-frame sample: +X forward, +Y lateral; ground-relative m/s, N, Nm.
Caller-owned wheel parameters; stiffness in N per unit slip / radian.
Suspension and tire parameters owned by the immutable vehicle definition.
float tireFriction
Tire friction coefficient and longitudinal / cornering stiffness in N.
float rollingResistance
Rolling-resistance coefficient and maximum brake torque per wheel in Nm.
float wheelInertia
Tire inertia in kg*m^2; all wheels use this definition value.
Wheel mount for 3D ray suspension or 2D top-down tire contact.
每帧输入(命令系统 / 驾驶者写入,移动模型消费)。