载入中...
搜索中...
未找到
Joint3DParameters.cpp
浏览该文件的文档.
2#include <box3d/box3d.h>
3#include <functional>
4#include <limits>
5#include <vector>
7
9namespace {
10using Numbers = std::vector<float>;
11struct NumericField {
12 const char* name;
13 std::size_t count;
15 bool ordered = false, quaternion = false;
16 std::function<Numbers(b3JointId)> read;
17 std::function<void(b3JointId, const Numbers&)> write;
18};
19struct BooleanField {
20 const char* name;
21 bool (*read)(b3JointId);
22 void (*write)(b3JointId, bool);
23};
24struct Fields {
25 std::vector<NumericField> numbers;
26 std::vector<BooleanField> flags;
27};
28constexpr float largest = std::numeric_limits<float>::max();
29NumericField scalar(const char* name, float (*get)(b3JointId), void (*set)(b3JointId, float), float minimum = 0,
30 float maximum = largest) {
31 return {name,
32 1,
33 minimum,
34 maximum,
35 false,
36 false,
37 [get](b3JointId id) { return Numbers{get(id)}; },
38 [set](b3JointId id, const Numbers& n) { set(id, n[0]); }};
39}
40NumericField pair(const char* name, float (*lower)(b3JointId), float (*upper)(b3JointId),
41 void (*set)(b3JointId, float, float), float minimum = -largest, float maximum = largest) {
42 return {name,
43 2,
44 minimum,
45 maximum,
46 true,
47 false,
48 [lower, upper](b3JointId id) { return Numbers{lower(id), upper(id)}; },
49 [set](b3JointId id, const Numbers& n) { set(id, n[0], n[1]); }};
50}
51Fields fields(b3JointType kind) {
52 Fields f;
53 f.flags.push_back({"collideConnected", b3Joint_GetCollideConnected, b3Joint_SetCollideConnected});
54 f.numbers.push_back(scalar("forceThreshold", b3Joint_GetForceThreshold, b3Joint_SetForceThreshold));
55 f.numbers.push_back(scalar("torqueThreshold", b3Joint_GetTorqueThreshold, b3Joint_SetTorqueThreshold));
56 f.numbers.push_back({"constraintTuning", 2, 0, largest, false, false,
57 [](b3JointId id) {
58 float h, d;
59 b3Joint_GetConstraintTuning(id, &h, &d);
60 return Numbers{h, d};
61 },
62 [](b3JointId id, const Numbers& n) { b3Joint_SetConstraintTuning(id, n[0], n[1]); }});
63 switch (kind) {
64 case b3_distanceJoint:
65 f.flags.push_back({"SpringEnabled", b3DistanceJoint_IsSpringEnabled, b3DistanceJoint_EnableSpring});
66 f.flags.push_back({"MotorEnabled", b3DistanceJoint_IsMotorEnabled, b3DistanceJoint_EnableMotor});
67 f.numbers.push_back(
68 scalar("SpringHertz", b3DistanceJoint_GetSpringHertz, b3DistanceJoint_SetSpringHertz, 0, largest));
69 f.numbers.push_back(scalar("SpringDampingRatio", b3DistanceJoint_GetSpringDampingRatio,
70 b3DistanceJoint_SetSpringDampingRatio, 0, largest));
71 f.numbers.push_back(scalar("MaxMotorForce", b3DistanceJoint_GetMaxMotorForce,
72 b3DistanceJoint_SetMaxMotorForce, 0, largest));
73 f.numbers.push_back(
74 scalar("MotorSpeed", b3DistanceJoint_GetMotorSpeed, b3DistanceJoint_SetMotorSpeed, -largest, largest));
75 f.flags.push_back({"LimitEnabled", b3DistanceJoint_IsLimitEnabled, b3DistanceJoint_EnableLimit});
76 f.numbers.push_back(scalar("Length", b3DistanceJoint_GetLength, b3DistanceJoint_SetLength, 0, largest));
77 f.numbers.push_back(pair("LengthRange", b3DistanceJoint_GetMinLength, b3DistanceJoint_GetMaxLength,
78 b3DistanceJoint_SetLengthRange, 0, largest));
79 f.numbers.push_back(
80 {"SpringForceRange", 2, -largest, largest, true, false,
81 [](b3JointId id) {
82 float a, b;
83 b3DistanceJoint_GetSpringForceRange(id, &a, &b);
84 return Numbers{a, b};
85 },
86 [](b3JointId id, const Numbers& n) { b3DistanceJoint_SetSpringForceRange(id, n[0], n[1]); }});
87 break;
88 case b3_revoluteJoint:
89 f.flags.push_back({"SpringEnabled", b3RevoluteJoint_IsSpringEnabled, b3RevoluteJoint_EnableSpring});
90 f.flags.push_back({"MotorEnabled", b3RevoluteJoint_IsMotorEnabled, b3RevoluteJoint_EnableMotor});
91 f.numbers.push_back(
92 scalar("SpringHertz", b3RevoluteJoint_GetSpringHertz, b3RevoluteJoint_SetSpringHertz, 0, largest));
93 f.numbers.push_back(scalar("SpringDampingRatio", b3RevoluteJoint_GetSpringDampingRatio,
94 b3RevoluteJoint_SetSpringDampingRatio, 0, largest));
95 f.numbers.push_back(scalar("MaxMotorTorque", b3RevoluteJoint_GetMaxMotorTorque,
96 b3RevoluteJoint_SetMaxMotorTorque, 0, largest));
97 f.numbers.push_back(
98 scalar("MotorSpeed", b3RevoluteJoint_GetMotorSpeed, b3RevoluteJoint_SetMotorSpeed, -largest, largest));
99 f.flags.push_back({"LimitEnabled", b3RevoluteJoint_IsLimitEnabled, b3RevoluteJoint_EnableLimit});
100 f.numbers.push_back(pair("Limits", b3RevoluteJoint_GetLowerLimit, b3RevoluteJoint_GetUpperLimit,
101 b3RevoluteJoint_SetLimits, -largest, largest));
102 f.numbers.push_back(
103 scalar("TargetAngle", b3RevoluteJoint_GetTargetAngle, b3RevoluteJoint_SetTargetAngle, -B3_PI, B3_PI));
104 break;
105 case b3_prismaticJoint:
106 f.flags.push_back({"SpringEnabled", b3PrismaticJoint_IsSpringEnabled, b3PrismaticJoint_EnableSpring});
107 f.flags.push_back({"MotorEnabled", b3PrismaticJoint_IsMotorEnabled, b3PrismaticJoint_EnableMotor});
108 f.numbers.push_back(
109 scalar("SpringHertz", b3PrismaticJoint_GetSpringHertz, b3PrismaticJoint_SetSpringHertz, 0, largest));
110 f.numbers.push_back(scalar("SpringDampingRatio", b3PrismaticJoint_GetSpringDampingRatio,
111 b3PrismaticJoint_SetSpringDampingRatio, 0, largest));
112 f.numbers.push_back(scalar("MaxMotorForce", b3PrismaticJoint_GetMaxMotorForce,
113 b3PrismaticJoint_SetMaxMotorForce, 0, largest));
114 f.numbers.push_back(scalar("MotorSpeed", b3PrismaticJoint_GetMotorSpeed, b3PrismaticJoint_SetMotorSpeed,
115 -largest, largest));
116 f.flags.push_back({"LimitEnabled", b3PrismaticJoint_IsLimitEnabled, b3PrismaticJoint_EnableLimit});
117 f.numbers.push_back(pair("Limits", b3PrismaticJoint_GetLowerLimit, b3PrismaticJoint_GetUpperLimit,
118 b3PrismaticJoint_SetLimits, -largest, largest));
119 f.numbers.push_back(scalar("TargetTranslation", b3PrismaticJoint_GetTargetTranslation,
120 b3PrismaticJoint_SetTargetTranslation, -largest, largest));
121 break;
122 case b3_sphericalJoint:
123 f.flags.push_back({"SpringEnabled", b3SphericalJoint_IsSpringEnabled, b3SphericalJoint_EnableSpring});
124 f.flags.push_back({"MotorEnabled", b3SphericalJoint_IsMotorEnabled, b3SphericalJoint_EnableMotor});
125 f.numbers.push_back(
126 scalar("SpringHertz", b3SphericalJoint_GetSpringHertz, b3SphericalJoint_SetSpringHertz, 0, largest));
127 f.numbers.push_back(scalar("SpringDampingRatio", b3SphericalJoint_GetSpringDampingRatio,
128 b3SphericalJoint_SetSpringDampingRatio, 0, largest));
129 f.numbers.push_back(scalar("MaxMotorTorque", b3SphericalJoint_GetMaxMotorTorque,
130 b3SphericalJoint_SetMaxMotorTorque, 0, largest));
131 f.flags.push_back(
132 {"ConeLimitEnabled", b3SphericalJoint_IsConeLimitEnabled, b3SphericalJoint_EnableConeLimit});
133 f.flags.push_back(
134 {"TwistLimitEnabled", b3SphericalJoint_IsTwistLimitEnabled, b3SphericalJoint_EnableTwistLimit});
135 f.numbers.push_back(
136 scalar("ConeLimit", b3SphericalJoint_GetConeLimit, b3SphericalJoint_SetConeLimit, 0, 0.5f * B3_PI));
137 f.numbers.push_back(pair("TwistLimits", b3SphericalJoint_GetLowerTwistLimit,
138 b3SphericalJoint_GetUpperTwistLimit, b3SphericalJoint_SetTwistLimits, -largest,
139 largest));
140 f.numbers.push_back({"TargetRotation", 4, -1, 1, false, true,
141 [](b3JointId id) {
142 auto q = b3SphericalJoint_GetTargetRotation(id);
143 return Numbers{q.v.x, q.v.y, q.v.z, q.s};
144 },
145 [](b3JointId id, const Numbers& n) {
146 b3SphericalJoint_SetTargetRotation(id, {{n[0], n[1], n[2]}, n[3]});
147 }});
148 f.numbers.push_back(
149 {"MotorVelocity", 3, -largest, largest, false, false,
150 [](b3JointId id) {
151 auto v = b3SphericalJoint_GetMotorVelocity(id);
152 return Numbers{v.x, v.y, v.z};
153 },
154 [](b3JointId id, const Numbers& n) { b3SphericalJoint_SetMotorVelocity(id, {n[0], n[1], n[2]}); }});
155 break;
156 case b3_wheelJoint:
157 f.flags.push_back({"SuspensionEnabled", b3WheelJoint_IsSuspensionEnabled, b3WheelJoint_EnableSuspension});
158 f.flags.push_back(
159 {"SuspensionLimitEnabled", b3WheelJoint_IsSuspensionLimitEnabled, b3WheelJoint_EnableSuspensionLimit});
160 f.flags.push_back({"SpinMotorEnabled", b3WheelJoint_IsSpinMotorEnabled, b3WheelJoint_EnableSpinMotor});
161 f.flags.push_back({"SteeringEnabled", b3WheelJoint_IsSteeringEnabled, b3WheelJoint_EnableSteering});
162 f.flags.push_back(
163 {"SteeringLimitEnabled", b3WheelJoint_IsSteeringLimitEnabled, b3WheelJoint_EnableSteeringLimit});
164 f.numbers.push_back(scalar("SuspensionHertz", b3WheelJoint_GetSuspensionHertz,
165 b3WheelJoint_SetSuspensionHertz, 0, largest));
166 f.numbers.push_back(scalar("SuspensionDampingRatio", b3WheelJoint_GetSuspensionDampingRatio,
167 b3WheelJoint_SetSuspensionDampingRatio, 0, largest));
168 f.numbers.push_back(
169 scalar("MaxSpinTorque", b3WheelJoint_GetMaxSpinTorque, b3WheelJoint_SetMaxSpinTorque, 0, largest));
170 f.numbers.push_back(
171 scalar("SteeringHertz", b3WheelJoint_GetSteeringHertz, b3WheelJoint_SetSteeringHertz, 0, largest));
172 f.numbers.push_back(scalar("SteeringDampingRatio", b3WheelJoint_GetSteeringDampingRatio,
173 b3WheelJoint_SetSteeringDampingRatio, 0, largest));
174 f.numbers.push_back(scalar("MaxSteeringTorque", b3WheelJoint_GetMaxSteeringTorque,
175 b3WheelJoint_SetMaxSteeringTorque, 0, largest));
176 f.numbers.push_back(scalar("SpinMotorSpeed", b3WheelJoint_GetSpinMotorSpeed, b3WheelJoint_SetSpinMotorSpeed,
177 -largest, largest));
178 f.numbers.push_back(scalar("TargetSteeringAngle", b3WheelJoint_GetTargetSteeringAngle,
179 b3WheelJoint_SetTargetSteeringAngle, -largest, largest));
180 f.numbers.push_back(pair("SuspensionLimits", b3WheelJoint_GetLowerSuspensionLimit,
181 b3WheelJoint_GetUpperSuspensionLimit, b3WheelJoint_SetSuspensionLimits, -largest,
182 largest));
183 f.numbers.push_back(pair("SteeringLimits", b3WheelJoint_GetLowerSteeringLimit,
184 b3WheelJoint_GetUpperSteeringLimit, b3WheelJoint_SetSteeringLimits, -largest,
185 largest));
186 break;
187 case b3_weldJoint:
188 f.numbers.push_back(scalar("LinearHertz", b3WeldJoint_GetLinearHertz, b3WeldJoint_SetLinearHertz));
189 f.numbers.push_back(
190 scalar("LinearDampingRatio", b3WeldJoint_GetLinearDampingRatio, b3WeldJoint_SetLinearDampingRatio));
191 f.numbers.push_back(scalar("AngularHertz", b3WeldJoint_GetAngularHertz, b3WeldJoint_SetAngularHertz));
192 f.numbers.push_back(
193 scalar("AngularDampingRatio", b3WeldJoint_GetAngularDampingRatio, b3WeldJoint_SetAngularDampingRatio));
194 break;
195 case b3_parallelJoint:
196 f.numbers.push_back(scalar("SpringHertz", b3ParallelJoint_GetSpringHertz, b3ParallelJoint_SetSpringHertz));
197 f.numbers.push_back(scalar("SpringDampingRatio", b3ParallelJoint_GetSpringDampingRatio,
198 b3ParallelJoint_SetSpringDampingRatio));
199 f.numbers.push_back(scalar("MaxTorque", b3ParallelJoint_GetMaxTorque, b3ParallelJoint_SetMaxTorque));
200 break;
201 case b3_motorJoint:
202 f.numbers.push_back(
203 scalar("MaxVelocityForce", b3MotorJoint_GetMaxVelocityForce, b3MotorJoint_SetMaxVelocityForce));
204 f.numbers.push_back(
205 scalar("MaxVelocityTorque", b3MotorJoint_GetMaxVelocityTorque, b3MotorJoint_SetMaxVelocityTorque));
206 f.numbers.push_back(scalar("LinearHertz", b3MotorJoint_GetLinearHertz, b3MotorJoint_SetLinearHertz));
207 f.numbers.push_back(
208 scalar("LinearDampingRatio", b3MotorJoint_GetLinearDampingRatio, b3MotorJoint_SetLinearDampingRatio));
209 f.numbers.push_back(scalar("AngularHertz", b3MotorJoint_GetAngularHertz, b3MotorJoint_SetAngularHertz));
210 f.numbers.push_back(scalar("AngularDampingRatio", b3MotorJoint_GetAngularDampingRatio,
211 b3MotorJoint_SetAngularDampingRatio));
212 f.numbers.push_back(
213 scalar("MaxSpringForce", b3MotorJoint_GetMaxSpringForce, b3MotorJoint_SetMaxSpringForce));
214 f.numbers.push_back(
215 scalar("MaxSpringTorque", b3MotorJoint_GetMaxSpringTorque, b3MotorJoint_SetMaxSpringTorque));
216 f.numbers.push_back(
217 {"LinearVelocity", 3, -largest, largest, false, false,
218 [](b3JointId id) {
219 auto v = b3MotorJoint_GetLinearVelocity(id);
220 return Numbers{v.x, v.y, v.z};
221 },
222 [](b3JointId id, const Numbers& n) { b3MotorJoint_SetLinearVelocity(id, {n[0], n[1], n[2]}); }});
223 f.numbers.push_back(
224 {"AngularVelocity", 3, -largest, largest, false, false,
225 [](b3JointId id) {
226 auto v = b3MotorJoint_GetAngularVelocity(id);
227 return Numbers{v.x, v.y, v.z};
228 },
229 [](b3JointId id, const Numbers& n) { b3MotorJoint_SetAngularVelocity(id, {n[0], n[1], n[2]}); }});
230 break;
231 default: break;
232 }
233 return f;
234}
235Result<void> invalid(const std::string& name) {
237 "payload.joints.parameters." + name));
238}
239} // namespace
241 const auto definition = fields(b3Joint_GetType(id));
243 for (const auto& f : definition.flags) out.emplace(f.name, Value(f.read(id)));
244 for (const auto& f : definition.numbers) {
246 for (float x : f.read(id)) values.emplace_back(x);
247 out.emplace(f.name, Value(std::move(values)));
248 }
249 return Value(std::move(out));
250}
252 const auto* object = value.getIf<Value::Object>();
253 const auto definition = fields(b3Joint_GetType(id));
254 if (!object || object->size() != definition.flags.size() + definition.numbers.size()) return invalid("fields");
255 std::vector<bool> flags;
256 std::vector<Numbers> numbers;
257 for (const auto& f : definition.flags) {
258 auto parsed = readBool(*object, f.name);
259 if (!parsed) return Result<void>::failure(parsed.status());
260 flags.push_back(parsed.value());
261 }
262 for (const auto& f : definition.numbers) {
263 auto parsed = readFloatArray(*object, f.name);
264 if (!parsed) return Result<void>::failure(parsed.status());
265 const auto& n = parsed.value();
266 if (n.size() != f.count) return invalid(f.name);
267 for (float x : n)
268 if (x < f.minimum || x > f.maximum) return invalid(f.name);
269 if (f.ordered && n[0] > n[1]) return invalid(f.name);
270 if (f.quaternion && !b3IsNormalizedQuat({{n[0], n[1], n[2]}, n[3]})) return invalid(f.name);
271 numbers.push_back(std::move(parsed).takeValue());
272 }
273 // No setter runs until every field has passed validation.
274 for (std::size_t i = 0; i < definition.numbers.size(); ++i) definition.numbers[i].write(id, numbers[i]);
275 for (std::size_t i = 0; i < definition.flags.size(); ++i) definition.flags[i].write(id, flags[i]);
276 return Result<void>::success();
277}
278} // namespace eve::physics::snapshot_detail
double x
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
float maximum[3]
float minimum[3]
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
float v
int h
std::function< void(b3JointId, const Numbers &)> write
bool ordered
std::vector< NumericField > numbers
bool quaternion
std::function< Numbers(b3JointId)> read
std::string id
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float f
std::set< std::string > values
Object::Ptr value
float d
ScriptStructure out
std::string kind
int count
Json object
uint32_t flags
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Result< void > restoreJoint3DParameters(b3JointId id, const Value &value)
Validate the complete parameter object before updating a live candidate joint; no retained references...
Result< bool > readBool(const Value::Object &object, std::string_view name)
Reads a required boolean.
eve::Result< std::vector< float > > readFloatArray(const eve::Value::Object &object, std::string_view name)
Read a finite float array, rejecting missing or malformed elements.
Value captureJoint3DParameters(b3JointId id)
Capture solver-owned parameters on the physics owner thread into an owning value.