1#include "../Viewscreen/SparkoProp.h"
2#include "../Viewscreen/SparkoTrace.h"
3#include "../Rendering/ClientMath.h"
4#include <cstdio>
5#include <cstdlib>
6#include <limits>
7#include <fstream>
9static int failures=0;
10static void Check(bool condition,const char* message) { if(!condition) {std::printf("FAIL: %s\n",message);++failures;} }
11static Pose Authored() {
return {{130,123,-122},
Quat::Ypr(-1.570796,0,-1.308997)};}
15 for(
double elapsed=0;elapsed<seconds-1e-10;elapsed+=1.0/hz) s.
Update(std::min(1.0/hz,seconds-elapsed),m);
16}
19static bool SamePose(
const Pose& a,
const Pose& b) {
22}
23static void TestPropAudio() {
27 int starts=0;
29 Check(starts==0,"sustained rolling does not restart its recording each frame");
31 Check(!audio.
Update(1.0/1000,input).
stopRolling,
"no-substep render frame keeps rolling episode");
33 for(
int i=0;i<3;++i) {++input.
frame;Check(!audio.
Update(1.0/60,input).
stopRolling,
"brief faceted-rim contact gap is quiet");}
35 Check(!audio.
Update(1.0/60,input).
startRolling,
"contact gap does not retrigger rolling");
37 int stops=0;
39 Check(stops==1,"airborne prop stops rolling sound");
41 Check(!audio.
Update(1.0/60,input).
startRolling,
"sliding without rotation does not start a roll sound");
43 auto events=audio.
Update(1.0/60,input);
44 Check(events.startRolling && events.hit,"new rolling and edge impact can sound together");
45 Check(!audio.
Update(.2,input).
hit,
"re-reading the same physics frame cannot replay an impact");
46 ++input.
frame;Check(audio.
Update(1.0/60,input).
hit,
"later impact can select another sample");
47 ++input.
frame;Check(!audio.
Update(1.0/60,input).
hit,
"adjacent solver impacts are debounced");
51 Check(audio.
Update(1.0/60,input).
stopAll,
"inactivity clears impact tails as well as rolling");
53 Check(audio.
Update(1.0/60,input).
startRolling,
"resuming starts a fresh rolling episode");
54 Check(audio.
Update(.3,input).
stopAll,
"hitch clears playing sounds");
55
56
59 audio.
Reset();
int restHits=0,restRolls=0;
60 for(int i=0;i<180;++i) {
61 prop.Update(1.0/60,force);
auto e=audio.
Update(1.0/60,prop.AudioInput(
true));
62 restHits+=e.
hit;restRolls+=e.startRolling;
63 }
64 Check(restHits==0 && restRolls<=1,"initial settling has no impact chatter and at most one rolling start");
65 for(int i=0;i<120;++i) {
66 prop.Update(1.0/60,force);
auto e=audio.
Update(1.0/60,prop.AudioInput(
true));
67 Check(!e.hit && !e.startRolling,"settled can remains silent under continuous support load");
68 }
69 force.specificForceMss.z=-5;
70 int driveHits=0,driveRolls=0;
71 for(int i=0;i<120;++i) {
72 prop.Update(1.0/60,force);
auto e=audio.
Update(1.0/60,prop.AudioInput(
true));
73 driveHits+=e.
hit;driveRolls+=e.startRolling;
74 }
75 Check(driveRolls>0,"actual reverse acceleration triggers rolling audio");
76 Check(driveHits>0,"actual footwell collision triggers impact audio");
77 Check(driveHits<20 && driveRolls<10,"sustained contact does not chatter each render frame");
78 force.suppressed=true;prop.Update(1.0/60,force);
79 events=audio.
Update(1.0/60,prop.AudioInput(
true));
80 Check(events.stopAll && !events.hit && !events.startRolling,"suppression clears audio without a spawn impact");
81 force.suppressed=false;prop.Update(1.0/60,force);
82 events=audio.
Update(1.0/60,prop.AudioInput(
true));
83 Check(!events.hit && !events.startRolling,"suppression exit primes quietly");
84 prop.Update(.3,force);
85 Check(!prop.AudioInput(true).active,"controller hitch recovery cannot generate audio");
86 std::printf("Prop audio: rest hits/rolls %d/%d; driving hits/rolls %d/%d\n",restHits,restRolls,driveHits,driveRolls);
87}
88int main(
int argc,
char** argv) {
89 TestPropAudio();
92 Check(s.
Scene().size()>150,
"dashboard and obstacle geometry present");
93 Check(std::abs(
Length(
Vec{3,3,0})-std::sqrt(18.0))<1e-10,
"can radial bound includes diagonal vertices");
94 FloorClear(s);Run(s,3);Dump("rest",s);FloorClear(s);
98 Run(s,.25,brake);Dump("brake",s);
100 Check(
Length(s.
body.
omega)>1,
"floor friction produces rolling spin");FloorClear(s);
105 s.
Update(.01,{{std::numeric_limits<double>::quiet_NaN(),0,0},{},{}});
106 Check(SamePose(s.
body.
pose,seatPose),
"invalid force preserves current pose");
108 Check(SamePose(s.
body.
pose,seatPose),
"invalid body recovers last valid seat pose");
110 auto a=
Can(),b=
Can(),c=
Can();Run(a,1,brake,30);Run(b,1,brake,60);Run(c,1,brake,144);
111 Check(
Length(a.body.pose.position-b.body.pose.position)<1e-6 &&
Length(b.body.pose.position-c.body.pose.position)<1e-6,
"fixed steps agree at 30/60/144 Hz");
112 for(
Vec velocity:std::vector<Vec>{{0,4000,0},{0,-4000,0},{4000,0,0},{-4000,0,0},{0,0,-4000},{0,0,3000}}) {
113 auto fast=
Can();fast.body.velocity=velocity;Run(fast,.5);Dump(
"swept",fast);FloorClear(fast);
114 Check(fast.body.pose.position.x>83 && fast.body.pose.position.x<171,"high-speed side containment");
115 Check(
Support(fast.Shape(),fast.body.pose,{0,-1,0}).
y>=47.998,
"rear stop full shape");
116 Check(
Support(fast.Shape(),fast.body.pose,{0,1,0}).
y<=158.002,
"front wall full shape");
117 Check(
Support(fast.Shape(),fast.body.pose,{0,0,1}).
z<=-33.998,
"upper footwell containment");
118 }
119
120 auto post=
Can();post.body.pose.position={113,75,-120};post.body.velocity={-2000,0,0};
121 Run(post,.025);Dump("post",post);
122 Check(
Support(post.Shape(),post.body.pose,{-1,0,0}).
x>=94.98,
"swept rim cannot tunnel through monitor post");
123
124 Body edge;edge.
pose=Authored();std::vector<Contact> hit{{{0,0,1},{7,0,-4}}};edge.
velocity={0,0,-100};
126 Body centre;centre.
velocity={0,0,-100};std::vector<Contact> middle{{{0,0,1},{0,0,-4}}};
129 Motion turning{{},{0,0,1},{0,0,.5}};
131 Check(
Length(force-
Vec{170,50,0})<1e-8,
"centrifugal Euler and Coriolis terms");
132
133
139 Check(
Length(Authored().rotation.Rotate(v)-
Vec{output.x,output.y,output.z})<1e-5,
"authored quaternion matches production renderer");
140 }
148 Check(!
SparkoProp::Matches(70,
false,nominal,ypr,44) && !
SparkoProp::Matches(70,
true,nominal+
Vec{.001,0,0},ypr,44),
"unconfigured type 70 props remain static");
150 SparkoProp::Sample sample{{0,11.27,0},{},{0,3000,0},{},
false,{-.0002,.0002,-.0002}};
151 for(int i=0;i<180;++i)control.Update(1.0/60,sample);
152 Check(control.simulation.body.sleeping && control.simulation.body.pose.position.z<-117,"runtime positive support force gives downward Vieneo gravity");
154 for(int i=0;i<20;++i)control.Update(1.0/60,sample);
155 Check(control.simulation.body.pose.position.y>125,"runtime acceleration and units move can forward");
156
157
158 for(double acceleration:{2.,5.,10.}) for(bool underSeat:{false,true}) {
161 if(underSeat) reverse.simulation.body.pose=seatPose;
162 double furthest=reverse.simulation.body.pose.
position.
y;
163 for(int frame=0;frame<360;++frame) {
164 reverse.Update(1.0/60,backing);
165 furthest=std::max(furthest,reverse.simulation.body.pose.position.y);
166 }
167 std::printf("reverse %.1f m/s2 seat=%d furthestY=%.3f\n",acceleration,underSeat,furthest);
168
169
170 if(!underSeat || acceleration>=5) Check(furthest>128,"reverse acceleration passes spawn from floor and seat");
171 Check(reverse.resets==0,"steady reverse does not reset prop");
172 if(underSeat && acceleration==2) {
174 frictionless.simulation.body.pose=seatPose;
175 frictionless.simulation.material.friction=0;frictionless.simulation.material.rollingResistance=0;
177 for(int frame=0;frame<360;++frame) {
178 frictionless.Update(1.0/60,backing);
179 released=std::max(released,frictionless.simulation.body.pose.position.y);
180 }
181 Check(released>128,"weak reverse seat hold comes from contact friction rather than spawn clamp");
182 }
183 }
184
185
186 auto airborne=
Can();Run(airborne,1);
188 Check(
Support(airborne.Shape(),airborne.body.pose,{0,0,-1}).
z>-120,
"entire can becomes airborne");
189 Check(airborne.body.velocity.z>0,"lift-off has upward velocity");
190 Run(airborne,3);FloorClear(airborne);
191 Check(airborne.body.sleeping,"airborne can lands and settles");
192
193
194 auto accounting=
Can();accounting.body.velocity={1400,-1800,650};accounting.body.omega={20,30,40};
195 for(int frame=0;frame<240;++frame) {
196 Body before=accounting.body;accounting.Update(1.0/60,
Gravity());
const auto& stats=accounting.stats;
197 Check(
Length(accounting.body.velocity-before.
velocity-stats.forceDeltaV-stats.contactDeltaV-stats.discardDeltaV)<1e-8,
"trace balances linear velocity");
198 Check(
Length(accounting.body.omega-before.
omega-stats.forceDeltaOmega-stats.contactDeltaOmega-stats.discardDeltaOmega)<1e-8,
"trace balances angular velocity");
199 Check(
Length(accounting.body.pose.position-before.
pose.
position-stats.integratedPosition-stats.positionCorrection)<1e-8,
"trace distinguishes integration from position repair");
200 }
201
202
203 const auto traceDir=std::filesystem::temp_directory_path()/L"RiseSparkoTraceTests";
204 std::filesystem::create_directories(traceDir);
205 {
208 capture.Reset("test, \"reset\"");
209 for(int frame=0;frame<240;++frame) {
210 capture.Update(1.0/60,sample);trace.Write(1.0/60,sample,capture,{});
211 }
212 trace.Flush();Check(!trace.Failed(),"bounded CSV capture succeeds");
213 for(const auto* name:{L"SparkoPhysics.csv",L"SparkoPhysics.previous.csv"}) {
214 const auto path=traceDir/name;
215 Check(std::filesystem::exists(path) && std::filesystem::file_size(path)<=65536,"trace rotates within byte limit");
216 std::ifstream recorded(path);std::string line;unsigned triangles=0;bool foundHeader=false,foundRow=false;
217 while(std::getline(recorded,line)) {
218 if(line.find("# triangle,")==0 && line.find("# triangle,id,")!=0) ++triangles;
219 if(line.find("frame,program_seconds,dt,action,")==0) foundHeader=true;
220 if(line.find("\"test, \"\"reset\"\"\"")!=std::string::npos) foundRow=true;
221 }
222 Check(triangles==185 && foundHeader && foundRow,"each trace segment has geometry schema and escaped reset reason");
223 }
225 unavailable.Write(1.0/60,sample,capture,{});
226 Check(unavailable.Failed(),"unwritable trace is nonfatal");
227 }
228 std::filesystem::remove(traceDir/L"SparkoPhysics.csv");
229 std::filesystem::remove(traceDir/L"SparkoPhysics.previous.csv");
230 sample.
suppressed=
true;control.Update(1.0/60,sample);
231 Check(
Length(control.simulation.body.velocity)==0 &&
Length(control.simulation.body.pose.position-Authored().position)<1,
"simulator entry resets all transient state");
232 sample.
specificForceMss={500,500,500};
for(
int i=0;i<10;++i)control.Update(1.0/60,sample);
233 Check(
Length(control.simulation.body.velocity)==0,
"simulator does not accumulate hidden motion");
234 sample.
suppressed=
false;control.Update(1.0/60,sample);
235 Check(
Length(control.simulation.body.velocity)==0,
"simulator exit primes history without replay");
237 Check(
Length(control.simulation.body.velocity)==0,
"teleport resets and primes force/rate history");
238 control.simulation.body.velocity={20,30,40};control.Reset();
239 Check(
Length(control.simulation.body.velocity)==0,
"explicit relocation/cockpit reset clears motion");
240
241
242
243
244
245 struct RecordedRelocation {double dt;Vec delta,velocity,can;Quat rotation;};
246 const Vec surfaceVelocity{.3186473548412323,-.15561601519584656,0};
247 const RecordedRelocation recorded[] {
248 {.041927199810743332,{.0001220703125,0,.00018310546875},{.32389992475509644,-.1576732099056244,.00460659759119153},{160.462803982918,71.95972884059391,-121.86628699847955},{.70154572976729612,-.081745817497776069,-.2600635858167758,-.65842094567154597}},
249 {.028978999704122543,{0,0,.0001678466796875},{.31767502427101135,-.15470671653747559,.005578816868364811},{106.44007680701388,148.85551718191496,-121.82180873482312},{.066933354535684109,-.70402940721991403,-.69441376946876832,-.13286096715120363}},
250 {.029773600399494171,{0,0,.0001068115234375},{.3213927149772644,-.1565895527601242,.00363371754065156},{161.4932390421108,113.69113195807364,-121.70557527838132},{.54588268515617122,.45056420289940857,.1331674396430915,-.69373656825282937}},
251 {.048822298645973206,{0,0,.0000457763671875},{.31894993782043457,-.15570279955863953,.0007344036712311208},{130.63016855573338,153.74920653493854,-121.74261997647852},{-.2186397486828206,.67177714814860856,.21828070802455571,-.67325006945824684}}
252 };
253 for(const auto& frame:recorded) {
254 Check(
Length(frame.delta-frame.velocity*frame.dt)>.01,
"recorded driving frame reproduces old relocation threshold");
256 SparkoProp::Sample driving{{0,11.27,0},{},{},frame.velocity,
false,{},surfaceVelocity};
257 driven.Update(1.0/60,driving);driven.simulation.body.pose={frame.can,frame.rotation};
258 driving.
positionKm=frame.delta;driven.Update(frame.dt,driving);
259 Check(driven.resets==0 && std::string(driven.action)=="step","recorded driving frame must not respawn can");
260 Check(driven.relocationErrorKm<.001,"relocation residual uses planet-fixed velocity");
261 Check(
Length(driven.simulation.body.pose.position-frame.can)<1,
"recorded can stays local at seat and outer wall");
262 driving.
positionKm.
x+=1;driven.Update(1.0/60,driving);
263 Check(driven.resets==1 && driven.resetReason=="position_discontinuity","real teleport still resets with planet rotation present");
264 }
266 SparkoProp::Sample stationary{{0,11.27,0},{},{},surfaceVelocity,
false,{},surfaceVelocity};
267 parked.Update(1.0/60,stationary);parked.simulation.body.pose=seatPose;
268 for(int frame=0;frame<90;++frame) parked.Update(frame%2?1.0/30:1.0/60,stationary);
269 Check(parked.resets==0 &&
Length(parked.simulation.body.pose.position-seatPose.
position)<1,
"stationary rotating-planet cabin does not reset at 30 Hz");
270 const Pose parkedPose=parked.simulation.body.pose;
271 stationary.
barycentricKms.
x=std::numeric_limits<double>::quiet_NaN();parked.Update(1.0/60,stationary);
272 Check(parked.resets==0 && SamePose(parked.simulation.body.pose,parkedPose) &&
273 std::string(parked.action)=="invalid_sample_hold","invalid rotation velocity preserves can pose");
276 hitch.Update(1.0/60,steady);
277 hitch.simulation.body.pose=seatPose;
278 hitch.simulation.body.velocity={20,-30,40};hitch.simulation.body.omega={1,2,3};
279 hitch.Update(.3,steady);
280 Check(hitch.resets==0 && std::string(hitch.action)=="hitch_hold" && std::string(hitch.simulation.stats.recovery)=="hitch_hold","trace labels a hitch without reporting a reset");
281 Check(SamePose(hitch.simulation.body.pose,seatPose) &&
Length(hitch.simulation.body.velocity)==0 &&
282 Length(hitch.simulation.body.omega)==0,
"controller 300 ms hitch cannot send can to other end of footwell");
284 Check(SamePose(hitch.simulation.body.pose,seatPose),"first good sample after hitch primes rates without replay");
285 steady.
cameraViewMountKm.
x=std::numeric_limits<double>::quiet_NaN();hitch.Update(1.0/60,steady);
286 Check(SamePose(hitch.simulation.body.pose,seatPose),"invalid mount sample preserves seat position");
288 Check(SamePose(hitch.simulation.body.pose,seatPose),"invalid sample recovery primes history");
289 steady.
suppressed=
true;hitch.Update(0,steady);
290 Check(
Length(hitch.simulation.body.pose.position-Authored().position)<1,
"simulator entry still resets during zero-length frame");
291 steady.
suppressed=
false;hitch.Update(1.0/60,steady);
292 Check(
Length(hitch.simulation.body.velocity)==0,
"simulator exit after pause does not replay motion");
293 auto door=
Can();door.body.pose={{140,101,-105},{}};door.body.velocity={2000,0,0};
294 for(int i=0;i<6;++i) {
295 door.Update(1.0/240,
Gravity());Dump(
"door step",door);
296 double gap=100;
for(
const auto&t:door.Scene())
if(
Near(door.body,t,0)) gap=std::min(gap,
Query(door.Shape(),door.body.pose,t).
gap);
297 Check(gap>-.05,"door sweep leaves no hull/triangle penetration");
298 }
299 Check(door.body.velocity.x<1500 && door.body.velocity.z<-100,"inward door face deflects can down before outer sill");
300 auto seat=
Can();seat.body.pose.position={124,55,-121.9};seat.body.velocity={0,0,1000};
301 Run(seat,.025);Dump("seat lip",seat);
302 Check(
Support(seat.Shape(),seat.body.pose,{0,0,1}).
z<-101,
"real seat front underside blocks upward flight");
303 auto overhang=
Can();overhang.body.pose.position={124,53,-121.9};Run(overhang,2);
304 Check(overhang.body.sleeping &&
Support(overhang.Shape(),overhang.body.pose,{0,-1,0}).
y>=48 &&
305 Support(overhang.Shape(),overhang.body.pose,{0,1,0}).
y<58,
"whole can fits and rests under the front seat overhang");
306 for(double x:{105.,124.,140.}) for(double rearwardForce:{300.,800.}) {
307 auto entry=
Can();entry.body.pose.position={x,100,-121.9};
309 for(int frame=0;frame<240;++frame) {
311 Check(
Length(entry.body.pose.position-before)<5,
"rolling into seat overhang never causes a large position jump");
312 Check(
Support(entry.Shape(),entry.body.pose,{0,-1,0}).
y>=47.998,
"seat entry preserves rear stop");
313 FloorClear(entry);
314 }
315 Check(entry.body.pose.position.y<58,"rearward loading brings can under seat locally");
316 }
317 auto slide=
Can(),grip=
Can();slide.material.friction=0;slide.material.rollingResistance=0;
318 slide.body.velocity=grip.body.velocity={0,80,0};Run(slide,.1);Run(grip,.1);
319 Check(
Length(slide.body.velocity)>
Length(grip.body.velocity) &&
Length(grip.body.omega)>
Length(slide.body.omega),
"sliding friction trades translation for rolling");
321 Check(tilt.body.pose.position.y>124,"tilted gravity produces downhill motion");
322 unsigned seed=1048;
323 auto random=[&]() {seed=1664525*seed+1013904223;return (double(seed)/4294967295.0)*2-1;};
324 for(int scenario=0;scenario<16;++scenario) {
325 auto stress=
Can();stress.body.velocity={random()*3000,random()*3000,random()*3000};
326 stress.body.omega={random()*80,random()*80,random()*80};
327 for(int tick=0;tick<90;++tick) {
328 stress.Update(1.0/240,
Gravity());
329 FloorClear(stress);
330 Check(
Support(stress.Shape(),stress.body.pose,{0,0,1}).
z<=-33.998,
"stress upper bound");
331 Check(
Support(stress.Shape(),stress.body.pose,{0,-1,0}).
y>=47.998,
"stress rear bound");
332 Check(
Support(stress.Shape(),stress.body.pose,{0,1,0}).
y<=158.002,
"stress forward bound");
333 Check(
Support(stress.Shape(),stress.body.pose,{1,0,0}).
x<=171.002,
"stress sill bound");
334 Vec n{1,-11.0/113,0};
335 Check(
Dot(
Support(stress.Shape(),stress.body.pose,-n),n)>=83-45*11.0/113-.002,
"stress tapered console bound");
336 }
337 }
338 if(argc==2) {
339 std::ofstream trace(argv[1]);
340 Check(bool(trace),"trace output opens");
341 trace<<"time,x,y,z,qx,qy,qz,qw\n";
343 for(int frame=0;frame<121;++frame) {
344 const auto&p=preview.body.pose;const auto&q=p.rotation;
345 trace<<frame/60.0<<','<<p.position.x<<','<<p.position.y<<','<<p.position.z<<','<<q.x<<','<<q.y<<','<<q.z<<','<<q.w<<'\n';
349 preview.Update(1.0/60,drive);
350 }
351 }
352 if(argc==3 && std::string(argv[1])=="--diagnostics") {
353 std::filesystem::create_directories(argv[2]);
357 for(int frame=0;frame<360;++frame) {
359 if(frame==330) capture.Reset("diagnostic, \"reset\"");
360 double dt=frame==350?.3:1.0/60;
361 capture.Update(dt,input);trace.Write(dt,input,capture,{frame/60.0});
362 }
363 trace.Flush();Check(!trace.Failed(),"diagnostic fixture capture succeeds");
364 }
365 std::printf("Rigid prop tests: %d failures\n",failures);return failures?1:0;
366}
Events Update(double dt, const Input &input)
void Update(double dt, const Motion &motion)
const std::vector< Triangle > & Scene() const
unsigned sweepBudgetStops
const Hull & Shape() const
Vec Gravity(Vec r, double gm)
void Solve(Body &b, std::vector< Contact > &contacts, const Material &material, double dt)
Vec Support(const Hull &h, const Pose &p, Vec d)
bool Near(const Body &b, const Triangle &t, double extra)
Distance Query(const Hull &h, const Pose &p, const Triangle &t)
Vec Acceleration(const Body &b, const Motion &m)
Matrix * MatrixRotationX(Matrix *out, float value)
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Matrix * MatrixRotationY(Matrix *out, float value)
Matrix * MatrixRotationYawPitchRoll(Matrix *out, float yaw, float pitch, float roll)
constexpr double KmPerUnit
std::vector< RigidProp::Triangle > Footwell()
bool Matches(unsigned char type, bool c2a, Vec nominalKm, Vec ypr, unsigned primitives)
std::vector< Vec > vertices
static Quat Ypr(double yaw, double pitch, double roll)
Vec inverseOrientationRates