载入中...
搜索中...
未找到
Coupling3DBindings.cpp
浏览该文件的文档.
2#include <simplesquirrel/simplesquirrel.hpp>
5#include "physics/Body3D.h"
6#include "physics/Joint3D.h"
7#include "physics/World3D.h"
9
10namespace eve::physics {
11namespace {
12template <class T>
13T* borrowed(const ssq::Object& object) {
14 if (object.getType() != ssq::Type::INSTANCE || object.getTypeTag() != eve::script::detail::squirrelTypeHash<T*>())
15 return nullptr;
16 try {
17 return object.to<T*>();
18 } catch (const ssq::TypeException&) {
19 return nullptr;
20 }
21}
22template <class T>
23T* receiver(HSQUIRRELVM vm, T* value) {
24 SQUserPointer tag = nullptr;
25 if (sq_gettype(vm, 1) != OT_INSTANCE || SQ_FAILED(sq_gettypetag(vm, 1, &tag)) ||
26 reinterpret_cast<std::size_t>(tag) != eve::script::detail::squirrelTypeHash<T*>())
27 return nullptr;
28 return value;
29}
30ssq::Table invalid(HSQUIRRELVM vm) {
33 DiagnosticCode::InvalidArgument, "Expected typed transmission inputs", "physics.coupling3d.script")));
34}
35template <class T>
36ssq::Table own(HSQUIRRELVM vm, Result<std::unique_ptr<T>> result) {
37 if (!result) return script::projectStatusResult(vm, result.status());
38 auto instance = script::makeOwnedSquirrelInstance(vm, std::move(result).takeValue());
39 if (!instance) return script::projectStatusResult(vm, instance.status());
40 return script::projectStatusResult(vm, Status::success(), std::move(instance).takeValue());
41}
42// This VM-owned object observes a single incarnation; it never owns the solver constraint.
43struct CouplingObserver {
44 World3D* owner;
45 std::weak_ptr<const void> worldLifetime, instanceLifetime;
46 int id;
47};
48ResultRef<Coupling3D> resolve(Coupling3D* coupling) {
49 if (coupling && coupling->isValid()) return ResultRef<Coupling3D>::success(std::ref(*coupling));
51 Diagnostic::error(DiagnosticCode::StaleHandle, "Transmission is absent or destroyed"));
52}
53ResultRef<Coupling3D> resolve(CouplingObserver* observer) {
54 if (observer) {
55 const auto worldGuard = observer->worldLifetime.lock();
56 const auto instanceGuard = observer->instanceLifetime.lock();
57 if (worldGuard && instanceGuard) return observer->owner->resolveCoupling3D(observer->id);
58 }
60 Diagnostic::error(DiagnosticCode::StaleHandle, "Transmission reference is stale"));
61}
62int identity(Coupling3D* coupling) { return coupling ? coupling->getId() : 0; }
63int identity(CouplingObserver* observer) { return observer ? observer->id : 0; }
64Result<void> remove(Coupling3D* coupling) {
65 return coupling ? coupling->destroy()
67}
68Result<void> remove(CouplingObserver* observer) {
69 auto target = resolve(observer);
70 return target ? target.value().get().destroy() : Result<void>::failure(target.status());
71}
72template <class T>
73void exposeOperations(ssq::Class& coupling, HSQUIRRELVM vm) {
74 coupling.addFunc("getId", [vm](T* self) { return identity(receiver(vm, self)); });
75 coupling.addFunc("isValid", [vm](T* self) { return resolve(receiver(vm, self)).ok(); });
76 coupling.addFunc("setEnabled", [vm](T* self, bool enabled) {
77 auto target = resolve(receiver(vm, self));
78 return target ? script::projectResult(vm, target.value().get().setEnabled(enabled))
80 });
81 coupling.addFunc("destroy", [vm](T* self) { return script::projectResult(vm, remove(receiver(vm, self))); });
82 coupling.addFunc("sample", [vm](T* self) {
83 auto target = resolve(receiver(vm, self));
84 if (!target) return script::projectStatusResult(vm, target.status());
85 return script::projectResult(vm, target.value().get().sample(), [](const Coupling3DState& state) {
86 return Value(Value::Object{{"coordinateA", Value(state.coordinateA)},
87 {"coordinateB", Value(state.coordinateB)},
88 {"error", Value(state.error)},
89 {"impulse", Value(state.impulse)},
90 {"active", Value(state.active)},
91 {"saturated", Value(state.saturated)}});
92 });
93 });
94}
95} // namespace
96void exposeCoupling3DBindings(ssq::Table& table) {
97 auto point = table.addClass("CouplingPoint3D", ssq::Class::Ctor<CouplingPoint3D()>());
98 point.addVar("x", &CouplingPoint3D::x);
99 point.addVar("y", &CouplingPoint3D::y);
100 point.addVar("z", &CouplingPoint3D::z);
101 auto limits = table.addClass("CouplingLimits3D", ssq::Class::Ctor<CouplingLimits3D()>());
102 limits.addVar("positionCorrection", &CouplingLimits3D::positionCorrection);
103 limits.addVar("maxCorrectionSpeed", &CouplingLimits3D::maxCorrectionSpeed);
104 limits.addVar("maxForce", &CouplingLimits3D::maxForce);
105 auto gear = table.addClass("GearCoupling3DSettings", ssq::Class::Ctor<GearCoupling3DSettings()>());
106 gear.addVar("ratio", &GearCoupling3DSettings::ratio);
107 gear.addVar("limits", &GearCoupling3DSettings::limits);
108 auto pulley = table.addClass("PulleyCoupling3DSettings", ssq::Class::Ctor<PulleyCoupling3DSettings()>());
109 pulley.addVar("localAnchorA", &PulleyCoupling3DSettings::localAnchorA);
110 pulley.addVar("localAnchorB", &PulleyCoupling3DSettings::localAnchorB);
111 pulley.addVar("groundA", &PulleyCoupling3DSettings::groundA);
112 pulley.addVar("groundB", &PulleyCoupling3DSettings::groundB);
113 pulley.addVar("ratio", &PulleyCoupling3DSettings::ratio);
114 pulley.addVar("length", &PulleyCoupling3DSettings::length);
115 pulley.addVar("tensionOnly", &PulleyCoupling3DSettings::tensionOnly);
116 pulley.addVar("limits", &PulleyCoupling3DSettings::limits);
117 auto coupling =
118 table.addClass<Coupling3D>("Coupling3D", std::function<Coupling3D*()>([] { return nullptr; }), true);
119 const auto vm = table.getHandle();
120 exposeOperations<Coupling3D>(coupling, vm);
121 auto observer = table.addClass<CouplingObserver>("Coupling3DReference",
122 std::function<CouplingObserver*()>([] { return nullptr; }), true);
123 exposeOperations<CouplingObserver>(observer, vm);
124 table.addFunc("resolveCoupling3D", [vm](ssq::Object worldObject, int id) {
125 auto* world = borrowed<World3D>(worldObject);
126 if (!world) return invalid(vm);
127 auto target = world->resolveCoupling3D(id);
128 if (!target) return script::projectStatusResult(vm, target.status());
129 return own(vm, Result<std::unique_ptr<CouplingObserver>>::success(std::make_unique<CouplingObserver>(
130 CouplingObserver{world, world->lifetimeToken(), target.value().get().lifetimeToken(), id})));
131 });
132 table.addFunc("createGearCoupling3D", [vm](ssq::Object w, ssq::Object a, ssq::Object b, ssq::Object s) {
133 auto* world = borrowed<World3D>(w);
134 auto* first = borrowed<Joint3D>(a);
135 auto* second = borrowed<Joint3D>(b);
136 auto* settings = borrowed<GearCoupling3DSettings>(s);
137 if (!world || !first || !second || !settings) return invalid(vm);
138 return own(vm, Coupling3D::createGear(*world, *first, *second, *settings));
139 });
140 table.addFunc("createPulleyCoupling3D", [vm](ssq::Object w, ssq::Object a, ssq::Object b, ssq::Object s) {
141 auto* world = borrowed<World3D>(w);
142 auto* first = borrowed<Body3D>(a);
143 auto* second = borrowed<Body3D>(b);
144 auto* settings = borrowed<PulleyCoupling3DSettings>(s);
145 if (!world || !first || !second || !settings) return invalid(vm);
146 return own(vm, Coupling3D::createPulley(*world, *first, *second, *settings));
147 });
148}
149} // namespace eve::physics
LogicalId target
float w
Definition AnimClip.cpp:738
int observer
Definition AnimSmr.cpp:162
const std::string & s
struct SQVM * HSQUIRRELVM
std::weak_ptr< const void > instanceLifetime
std::weak_ptr< const void > worldLifetime
HSQUIRRELVM vm
Definition ECS.cpp:38
wgpu::PopErrorScopeStatus status
std::int32_t second
std::int32_t first
std::string id
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
const std::string * tag
OceanWakeSettings settings
std::shared_ptr< OceanGpuSpectrum > owner
const RuntimeTensor * borrowed
std::string script
Definition PlayHost.cpp:117
Object::Ptr value
std::vector< Gear > gear
The single Squirrel projection for common Result, Status and Value.
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
const AssetImportLimits & limits
glm::vec3 point
uint64_t identity
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Owning 3D transmission constraint, stepped transactionally by its World3D. @ownership Caller/VM owns ...
Definition Coupling3D.h:50
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
void exposeCoupling3DBindings(ssq::Table &table)
Register owning transmission Result factories; table is borrowed on the VM owner thread during this c...
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
eve::Result< ssq::Object > makeOwnedSquirrelInstance(HSQUIRRELVM vm, Owned< T > object)
Creates a Squirrel instance that owns a C++ wrapper through a release hook.
bool enabled
Stabilization and force limits for an ideal mechanical transmission.
Definition Coupling3D.h:16
Owning SI point (meters); pulley attachments use body-local points, guides use world points.
Definition Coupling3D.h:12
Gear relation q1 + ratio*q2 = constant captured at creation. Mixed joints give ratio length-dependent...
Definition Coupling3D.h:23
Weighted rope length lA + ratio*lB; ideal taut pulley by default, optional tension-only slack rope.
Definition Coupling3D.h:28