|
| 1 | +// The installed Jolt Physics wrapper: a floor, a stack of boxes and a ball thrown at it; stepped until the stack |
| 2 | +// falls, a ray cast down onto it, the world saved and restored to the uninterrupted run's bits, and the same bits |
| 3 | +// with one worker and with three; the adapted constants. |
| 4 | +import std; |
| 5 | +import mcpplibs.mwrap.jolt; |
| 6 | + |
| 7 | +namespace jolt = mcpplibs::mwrap::jolt; |
| 8 | + |
| 9 | +namespace { |
| 10 | + |
| 11 | +constexpr JPH::ObjectLayer still = 0, moving = 1; |
| 12 | + |
| 13 | +bool map(JPH::BroadPhaseLayerInterfaceTable& broad, JPH::ObjectLayerPairFilterTable& pairs) { |
| 14 | + broad.MapObjectToBroadPhaseLayer(still, JPH::BroadPhaseLayer(0)); |
| 15 | + broad.MapObjectToBroadPhaseLayer(moving, JPH::BroadPhaseLayer(1)); |
| 16 | + pairs.EnableCollision(moving, still); |
| 17 | + pairs.EnableCollision(moving, moving); |
| 18 | + return true; |
| 19 | +} |
| 20 | +struct Layers { |
| 21 | + JPH::BroadPhaseLayerInterfaceTable broad{2, 2}; |
| 22 | + JPH::ObjectLayerPairFilterTable pairs{2}; |
| 23 | + bool mapped = map(broad, pairs); |
| 24 | + JPH::ObjectVsBroadPhaseLayerFilterTable object_vs_broad{broad, 2, pairs, 2}; |
| 25 | +}; |
| 26 | + |
| 27 | +struct Scene { |
| 28 | + JPH::TempAllocatorImpl temp{4 * 1024 * 1024}; |
| 29 | + std::unique_ptr<JPH::JobSystem> jobs; |
| 30 | + JPH::PhysicsSystem system; |
| 31 | + std::vector<JPH::BodyID> bodies; |
| 32 | +}; |
| 33 | + |
| 34 | +void make(Scene& scene, const Layers& layers, int workers) { |
| 35 | + if (workers == 0) scene.jobs = std::make_unique<JPH::JobSystemSingleThreaded>(jolt::cMaxPhysicsJobs); |
| 36 | + else scene.jobs = std::make_unique<JPH::JobSystemThreadPool>(jolt::cMaxPhysicsJobs, jolt::cMaxPhysicsBarriers, workers); |
| 37 | + scene.system.Init(256, 0, 256, 256, layers.broad, layers.object_vs_broad, layers.pairs); |
| 38 | + JPH::BodyInterface& bodies = scene.system.GetBodyInterface(); |
| 39 | + scene.bodies.push_back(bodies.CreateAndAddBody( |
| 40 | + JPH::BodyCreationSettings(new JPH::BoxShape(JPH::Vec3(20, 1, 20)), JPH::RVec3(0, -1, 0), JPH::Quat::sIdentity(), JPH::EMotionType::Static, still), |
| 41 | + JPH::EActivation::DontActivate)); |
| 42 | + for (int level = 0; level < 5; ++level) |
| 43 | + scene.bodies.push_back(bodies.CreateAndAddBody(JPH::BodyCreationSettings(new JPH::BoxShape(JPH::Vec3(0.5f, 0.5f, 0.5f)), JPH::RVec3(0, 0.5f + level * 1.0f, 0), |
| 44 | + JPH::Quat::sIdentity(), JPH::EMotionType::Dynamic, moving), |
| 45 | + JPH::EActivation::Activate)); |
| 46 | + JPH::BodyCreationSettings ball(new JPH::SphereShape(0.5f), JPH::RVec3(-8, 2, 0), JPH::Quat::sIdentity(), JPH::EMotionType::Dynamic, moving); |
| 47 | + ball.mLinearVelocity = JPH::Vec3(20, 1, 0); |
| 48 | + scene.bodies.push_back(bodies.CreateAndAddBody(ball, JPH::EActivation::Activate)); |
| 49 | +} |
| 50 | + |
| 51 | +void step(Scene& scene, int steps) { |
| 52 | + for (int index = 0; index < steps; ++index) scene.system.Update(1.0f / 60.0f, 1, &scene.temp, scene.jobs.get()); |
| 53 | +} |
| 54 | + |
| 55 | +std::uint64_t bits(const Scene& scene) { |
| 56 | + std::uint64_t hash = 0xcbf29ce484222325ull; |
| 57 | + const JPH::BodyInterface& bodies = scene.system.GetBodyInterface(); |
| 58 | + for (const JPH::BodyID body : scene.bodies) { |
| 59 | + const JPH::RVec3 at = bodies.GetPosition(body); |
| 60 | + const JPH::Quat turn = bodies.GetRotation(body); |
| 61 | + for (const float value : {float(at.GetX()), float(at.GetY()), float(at.GetZ()), turn.GetX(), turn.GetY(), turn.GetZ(), turn.GetW()}) { |
| 62 | + hash ^= std::bit_cast<std::uint32_t>(value); |
| 63 | + hash *= 0x100000001b3ull; |
| 64 | + } |
| 65 | + } |
| 66 | + return hash; |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace |
| 70 | + |
| 71 | +int main() { |
| 72 | + JPH::RegisterDefaultAllocator(); |
| 73 | + JPH::Factory::sInstance = new JPH::Factory(); |
| 74 | + JPH::RegisterTypes(); |
| 75 | + int failed = 0; |
| 76 | + { |
| 77 | + const Layers layers; |
| 78 | + Scene straight; |
| 79 | + make(straight, layers, 0); |
| 80 | + JPH::RayCastResult hit; |
| 81 | + if (!straight.system.GetNarrowPhaseQuery().CastRay(JPH::RRayCast{JPH::RVec3(0, 20, 0), JPH::Vec3(0, -40, 0)}, hit) || hit.mBodyID != straight.bodies[5]) failed = 1; |
| 82 | + step(straight, 180); |
| 83 | + const std::uint64_t expected = bits(straight); |
| 84 | + if (straight.system.GetBodyInterface().GetPosition(straight.bodies[5]).GetY() > 3.0f && !failed) failed = 2; // the top box fell |
| 85 | + |
| 86 | + Scene rolled; |
| 87 | + make(rolled, layers, 0); |
| 88 | + step(rolled, 60); |
| 89 | + JPH::StateRecorderImpl saved; |
| 90 | + rolled.system.SaveState(saved); |
| 91 | + step(rolled, 60); |
| 92 | + saved.Rewind(); |
| 93 | + if (!rolled.system.RestoreState(saved) && !failed) failed = 3; |
| 94 | + step(rolled, 120); |
| 95 | + if (bits(rolled) != expected && !failed) failed = 4; |
| 96 | + |
| 97 | + Scene threaded; |
| 98 | + make(threaded, layers, 3); |
| 99 | + step(threaded, 180); |
| 100 | + if (bits(threaded) != expected && !failed) failed = 5; |
| 101 | + if ((jolt::cMaxPhysicsJobs != 2048 || jolt::sAllSubShapeTypes.size() != static_cast<std::size_t>(jolt::NumSubShapeTypes)) && !failed) failed = 6; |
| 102 | + std::println("Jolt Physics 5.6.0 through import mcpplibs.mwrap.jolt: the stack knocked down, restored and run again to {:#018x}, the same with 3 workers{}", |
| 103 | + expected, failed ? std::format(" — check {} failed", failed) : ""); |
| 104 | + } |
| 105 | + JPH::UnregisterTypes(); |
| 106 | + delete JPH::Factory::sInstance; |
| 107 | + JPH::Factory::sInstance = nullptr; |
| 108 | + return failed; |
| 109 | +} |
0 commit comments