1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
#include <Jolt/Jolt.h>
#include <Jolt/RegisterTypes.h> #include <Jolt/Core/Factory.h> #include <Jolt/Core/TempAllocator.h> #include <Jolt/Core/JobSystemThreadPool.h> #include <Jolt/Physics/PhysicsSettings.h> #include <Jolt/Physics/PhysicsSystem.h> #include <Jolt/Physics/Collision/Shape/BoxShape.h> #include <Jolt/Physics/Collision/Shape/SphereShape.h> #include <Jolt/Physics/Body/BodyCreationSettings.h> #include <Jolt/Physics/Body/BodyActivationListener.h>
#include <iostream> #include <cstdarg> #include <thread>
JPH_SUPPRESS_WARNINGS
using namespace JPH;
using namespace JPH::literals;
using namespace std;
static void TraceImpl(const char *inFMT, ...) { va_list list; va_start(list, inFMT); char buffer[1024]; vsnprintf(buffer, sizeof(buffer), inFMT, list); va_end(list);
cout << buffer << endl; }
#ifdef JPH_ENABLE_ASSERTS
static bool AssertFailedImpl(const char *inExpression, const char *inMessage, const char *inFile, uint inLine) { cout << inFile << ":" << inLine << ": (" << inExpression << ") " << (inMessage != nullptr? inMessage : "") << endl;
return true; };
#endif
namespace Layers { static constexpr ObjectLayer NON_MOVING = 0; static constexpr ObjectLayer MOVING = 1; static constexpr ObjectLayer NUM_LAYERS = 2; };
class ObjectLayerPairFilterImpl : public ObjectLayerPairFilter { public: virtual bool ShouldCollide(ObjectLayer inObject1, ObjectLayer inObject2) const override { switch (inObject1) { case Layers::NON_MOVING: return inObject2 == Layers::MOVING; case Layers::MOVING: return true; default: JPH_ASSERT(false); return false; } } };
namespace BroadPhaseLayers { static constexpr BroadPhaseLayer NON_MOVING(0); static constexpr BroadPhaseLayer MOVING(1); static constexpr uint NUM_LAYERS(2); };
class BPLayerInterfaceImpl final : public BroadPhaseLayerInterface { public: BPLayerInterfaceImpl() { mObjectToBroadPhase[Layers::NON_MOVING] = BroadPhaseLayers::NON_MOVING; mObjectToBroadPhase[Layers::MOVING] = BroadPhaseLayers::MOVING; }
virtual uint GetNumBroadPhaseLayers() const override { return BroadPhaseLayers::NUM_LAYERS; }
virtual BroadPhaseLayer GetBroadPhaseLayer(ObjectLayer inLayer) const override { JPH_ASSERT(inLayer < Layers::NUM_LAYERS); return mObjectToBroadPhase[inLayer]; }
#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED) virtual const char * GetBroadPhaseLayerName(BroadPhaseLayer inLayer) const override { switch ((BroadPhaseLayer::Type)inLayer) { case (BroadPhaseLayer::Type)BroadPhaseLayers::NON_MOVING: return "NON_MOVING"; case (BroadPhaseLayer::Type)BroadPhaseLayers::MOVING: return "MOVING"; default: JPH_ASSERT(false); return "INVALID"; } } #endif
private: BroadPhaseLayer mObjectToBroadPhase[Layers::NUM_LAYERS]; };
class ObjectVsBroadPhaseLayerFilterImpl : public ObjectVsBroadPhaseLayerFilter { public: virtual bool ShouldCollide(ObjectLayer inLayer1, BroadPhaseLayer inLayer2) const override { switch (inLayer1) { case Layers::NON_MOVING: return inLayer2 == BroadPhaseLayers::MOVING; case Layers::MOVING: return true; default: JPH_ASSERT(false); return false; } } };
class MyContactListener : public ContactListener { public: virtual ValidateResult OnContactValidate(const Body &inBody1, const Body &inBody2, RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult) override { cout << "Contact validate callback" << endl;
return ValidateResult::AcceptAllContactsForThisBodyPair; }
virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override { cout << "A contact was added" << endl; }
virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override { cout << "A contact was persisted" << endl; }
virtual void OnContactRemoved(const SubShapeIDPair &inSubShapePair) override { cout << "A contact was removed" << endl; } };
class MyBodyActivationListener : public BodyActivationListener { public: virtual void OnBodyActivated(const BodyID &inBodyID, uint64 inBodyUserData) override { cout << "A body got activated" << endl; }
virtual void OnBodyDeactivated(const BodyID &inBodyID, uint64 inBodyUserData) override { cout << "A body went to sleep" << endl; } };
int main(int argc, char** argv) { RegisterDefaultAllocator();
Trace = TraceImpl; JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
Factory::sInstance = new Factory();
RegisterTypes();
TempAllocatorImpl temp_allocator(10 * 1024 * 1024);
JobSystemThreadPool job_system(cMaxPhysicsJobs, cMaxPhysicsBarriers, thread::hardware_concurrency() - 1);
const uint cMaxBodies = 1024;
const uint cNumBodyMutexes = 0;
const uint cMaxBodyPairs = 1024;
const uint cMaxContactConstraints = 1024;
BPLayerInterfaceImpl broad_phase_layer_interface;
ObjectVsBroadPhaseLayerFilterImpl object_vs_broadphase_layer_filter;
ObjectLayerPairFilterImpl object_vs_object_layer_filter;
PhysicsSystem physics_system; physics_system.Init(cMaxBodies, cNumBodyMutexes, cMaxBodyPairs, cMaxContactConstraints, broad_phase_layer_interface, object_vs_broadphase_layer_filter, object_vs_object_layer_filter);
MyBodyActivationListener body_activation_listener; physics_system.SetBodyActivationListener(&body_activation_listener);
MyContactListener contact_listener; physics_system.SetContactListener(&contact_listener);
BodyInterface &body_interface = physics_system.GetBodyInterface();
BoxShapeSettings floor_shape_settings(Vec3(100.0f, 1.0f, 100.0f)); floor_shape_settings.SetEmbedded();
ShapeSettings::ShapeResult floor_shape_result = floor_shape_settings.Create(); ShapeRefC floor_shape = floor_shape_result.Get();
BodyCreationSettings floor_settings(floor_shape, RVec3(0.0_r, -1.0_r, 0.0_r), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
Body *floor = body_interface.CreateBody(floor_settings);
body_interface.AddBody(floor->GetID(), EActivation::DontActivate);
BodyCreationSettings sphere_settings(new SphereShape(0.5f), RVec3(0.0_r, 2.0_r, 0.0_r), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING); BodyID sphere_id = body_interface.CreateAndAddBody(sphere_settings, EActivation::Activate);
body_interface.SetLinearVelocity(sphere_id, Vec3(0.0f, -5.0f, 0.0f));
const float cDeltaTime = 1.0f / 60.0f;
physics_system.OptimizeBroadPhase();
uint step = 0; while (body_interface.IsActive(sphere_id)) { ++step;
RVec3 position = body_interface.GetCenterOfMassPosition(sphere_id); Vec3 velocity = body_interface.GetLinearVelocity(sphere_id); cout << "Step " << step << ": Position = (" << position.GetX() << ", " << position.GetY() << ", " << position.GetZ() << "), Velocity = (" << velocity.GetX() << ", " << velocity.GetY() << ", " << velocity.GetZ() << ")" << endl;
const int cCollisionSteps = 1;
physics_system.Update(cDeltaTime, cCollisionSteps, &temp_allocator, &job_system); }
body_interface.RemoveBody(sphere_id);
body_interface.DestroyBody(sphere_id);
body_interface.RemoveBody(floor->GetID()); body_interface.DestroyBody(floor->GetID());
UnregisterTypes();
delete Factory::sInstance; Factory::sInstance = nullptr;
return 0; }
|