HighSpeedTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/General/HighSpeedTest.h>
  6. #include <Jolt/Physics/Collision/RayCast.h>
  7. #include <Jolt/Physics/Collision/CastResult.h>
  8. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  9. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  10. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  11. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  12. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  13. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  14. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  15. #include <Jolt/Physics/PhysicsScene.h>
  16. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  17. #include <Application/DebugUI.h>
  18. #include <Utils/Log.h>
  19. #include <Layers.h>
  20. JPH_IMPLEMENT_RTTI_VIRTUAL(HighSpeedTest)
  21. {
  22. JPH_ADD_BASE_CLASS(HighSpeedTest, Test)
  23. }
  24. const char *HighSpeedTest::sScenes[] =
  25. {
  26. "Simple",
  27. "Convex Hull On Large Triangles",
  28. "Convex Hull On Terrain1",
  29. };
  30. int HighSpeedTest::sSelectedScene = 0;
  31. void HighSpeedTest::CreateDominoBlocks(RVec3Arg inOffset, int inNumWalls, float inDensity, float inRadius)
  32. {
  33. BodyCreationSettings box_settings;
  34. Ref<BoxShape> box_shape = new BoxShape(Vec3(0.9f, 1.0f, 0.1f));
  35. box_shape->SetDensity(inDensity); // Make box more heavy so the bouncing ball keeps a higher velocity
  36. box_settings.SetShape(box_shape);
  37. box_settings.mObjectLayer = Layers::MOVING;
  38. // U shaped set of thin boxes
  39. for (int i = 0; i < inNumWalls; ++i)
  40. {
  41. box_settings.mPosition = inOffset + Vec3(2.0f * i, 1, -1.1f - inRadius);
  42. mBodyInterface->CreateAndAddBody(box_settings, EActivation::DontActivate);
  43. box_settings.mPosition = inOffset + Vec3(2.0f * i, 1, +1.1f + inRadius);
  44. mBodyInterface->CreateAndAddBody(box_settings, EActivation::DontActivate);
  45. }
  46. box_settings.mPosition = inOffset + Vec3(-1.1f - inRadius, 1, 0);
  47. box_settings.mRotation = Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI);
  48. mBodyInterface->CreateAndAddBody(box_settings, EActivation::DontActivate);
  49. }
  50. void HighSpeedTest::CreateDynamicObject(RVec3Arg inPosition, Vec3Arg inVelocity, Shape *inShape, EMotionQuality inMotionQuality)
  51. {
  52. BodyCreationSettings creation_settings;
  53. creation_settings.SetShape(inShape);
  54. creation_settings.mFriction = 0.0f;
  55. creation_settings.mRestitution = 1.0f;
  56. creation_settings.mLinearDamping = 0.0f;
  57. creation_settings.mAngularDamping = 0.0f;
  58. creation_settings.mMotionQuality = inMotionQuality;
  59. creation_settings.mObjectLayer = Layers::MOVING;
  60. creation_settings.mPosition = inPosition;
  61. Body &body = *mBodyInterface->CreateBody(creation_settings);
  62. body.SetLinearVelocity(inVelocity);
  63. mBodyInterface->AddBody(body.GetID(), inVelocity.IsNearZero()? EActivation::DontActivate : EActivation::Activate);
  64. }
  65. void HighSpeedTest::CreateSimpleScene()
  66. {
  67. // Floor
  68. CreateFloor();
  69. const float radius = 0.1f;
  70. const int num_walls = 5;
  71. const float density = 2000.0f;
  72. const float speed = 240.0f;
  73. RVec3 offset(0, 0, -30);
  74. {
  75. // U shaped set of thin walls
  76. TriangleList triangles;
  77. for (int i = 0; i < num_walls; ++i)
  78. {
  79. triangles.push_back(Triangle(Float3(2.0f*i-1,0,-1-radius), Float3(2.0f*i+1,0,-1-radius), Float3(2.0f*i,2,-1-radius)));
  80. triangles.push_back(Triangle(Float3(2.0f*i-1,0,1+radius), Float3(2.0f*i,2,1+radius), Float3(2.0f*i+1,0,1+radius)));
  81. }
  82. triangles.push_back(Triangle(Float3(-1-radius,0,-1), Float3(-1-radius,2,0), Float3(-1-radius,0,1)));
  83. Body &walls = *mBodyInterface->CreateBody(BodyCreationSettings(new MeshShapeSettings(triangles), offset, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  84. walls.SetRestitution(1.0f);
  85. walls.SetFriction(0.0f);
  86. mBodyInterface->AddBody(walls.GetID(), EActivation::DontActivate);
  87. // Fast moving sphere against mesh
  88. CreateDynamicObject(offset + Vec3(2.0f * num_walls - 1, 1, 0), Vec3(-speed, 0, -speed), new SphereShape(radius));
  89. }
  90. offset += Vec3(0, 0, 5);
  91. {
  92. // Create wall of domino blocks
  93. CreateDominoBlocks(offset, num_walls, density, radius);
  94. // Fast moving sphere against domino blocks
  95. CreateDynamicObject(offset + Vec3(2.0f * num_walls - 1, 1, 0), Vec3(-speed, 0, -speed), new SphereShape(radius));
  96. }
  97. offset += Vec3(0, 0, 5);
  98. {
  99. // Create wall of domino blocks
  100. CreateDominoBlocks(offset, num_walls, density, radius);
  101. // Fast moving scaled box against domino blocks
  102. CreateDynamicObject(offset + Vec3(2.0f * num_walls - 1, 1, 0), Vec3(-speed, 0, -speed), new ScaledShape(new BoxShape(Vec3::sReplicate(0.5f * radius), 0.01f), Vec3::sReplicate(2.0f)));
  103. }
  104. offset += Vec3(0, 0, 5);
  105. {
  106. // Fast moving box stuck in ground moving, one moving up, one moving down
  107. CreateDynamicObject(offset + Vec3(-1, 0, 0), Vec3(0, speed, 0), new BoxShape(Vec3::sReplicate(radius)));
  108. CreateDynamicObject(offset + Vec3(1, 0, 0), Vec3(0, -speed, 0), new BoxShape(Vec3::sReplicate(radius)));
  109. }
  110. offset += Vec3(0, 0, 5);
  111. {
  112. // Single shape that has 4 walls to surround fast moving sphere
  113. BodyCreationSettings enclosing_settings;
  114. Ref<BoxShapeSettings> box_shape = new BoxShapeSettings(Vec3(1.0f, 1.0f, 0.1f));
  115. Ref<StaticCompoundShapeSettings> enclosing_shape = new StaticCompoundShapeSettings();
  116. enclosing_shape->AddShape(Vec3(0, 0, 1), Quat::sIdentity(), box_shape);
  117. enclosing_shape->AddShape(Vec3(0, 0, -1), Quat::sIdentity(), box_shape);
  118. enclosing_shape->AddShape(Vec3(1, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  119. enclosing_shape->AddShape(Vec3(-1, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  120. enclosing_settings.SetShapeSettings(enclosing_shape);
  121. enclosing_settings.mMotionType = EMotionType::Kinematic;
  122. enclosing_settings.mObjectLayer = Layers::MOVING;
  123. enclosing_settings.mPosition = offset + Vec3(0, 1, 0);
  124. mBodyInterface->CreateAndAddBody(enclosing_settings, EActivation::Activate);
  125. // Fast moving sphere in box
  126. CreateDynamicObject(offset + Vec3(0, 0.5f, 0), Vec3(-speed, 0, -0.5f * speed), new SphereShape(radius));
  127. }
  128. offset += Vec3(0, 0, 5);
  129. {
  130. // Two boxes on a collision course
  131. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  132. CreateDynamicObject(offset + Vec3(-1, 0.5f, 0), Vec3(speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  133. }
  134. offset += Vec3(0, 0, 5);
  135. {
  136. // Two boxes on a collision course, off center
  137. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  138. CreateDynamicObject(offset + Vec3(-1, 0.5f, radius), Vec3(speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  139. }
  140. offset += Vec3(0, 0, 5);
  141. {
  142. // Two boxes on a collision course, one discrete
  143. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  144. CreateDynamicObject(offset + Vec3(-1, 0.5f, 0), Vec3(60.0f, 0, 0), new BoxShape(Vec3::sReplicate(radius)), EMotionQuality::Discrete);
  145. }
  146. offset += Vec3(0, 0, 5);
  147. {
  148. // Two boxes on a collision course, one inactive
  149. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  150. CreateDynamicObject(offset + Vec3(0, 0.5f, 0), Vec3::sZero(), new BoxShape(Vec3::sReplicate(radius)));
  151. }
  152. offset += Vec3(0, 0, 5);
  153. {
  154. // Two boxes on a collision course, one inactive and discrete
  155. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  156. CreateDynamicObject(offset + Vec3(0, 0.5f, 0), Vec3::sZero(), new BoxShape(Vec3::sReplicate(radius)), EMotionQuality::Discrete);
  157. }
  158. offset += Vec3(0, 0, 5);
  159. {
  160. // Create long thin shape
  161. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  162. box_settings.SetEmbedded();
  163. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  164. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  165. body_settings.mRestitution = 0.0f;
  166. body_settings.mFriction = 1.0f;
  167. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  168. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  169. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  170. }
  171. offset += Vec3(0, 0, 5);
  172. {
  173. // Create long thin shape under 45 degrees
  174. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  175. box_settings.SetEmbedded();
  176. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  177. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  178. body_settings.mRestitution = 0.0f;
  179. body_settings.mFriction = 1.0f;
  180. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  181. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  182. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  183. }
  184. offset += Vec3(0, 0, 5);
  185. {
  186. // Create long thin shape with restitution
  187. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  188. box_settings.SetEmbedded();
  189. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  190. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  191. body_settings.mRestitution = 1.0f;
  192. body_settings.mFriction = 1.0f;
  193. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  194. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  195. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  196. }
  197. offset += Vec3(0, 0, 5);
  198. {
  199. // Create long thin shape under 45 degrees with restitution
  200. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  201. box_settings.SetEmbedded();
  202. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  203. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  204. body_settings.mRestitution = 1.0f;
  205. body_settings.mFriction = 1.0f;
  206. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  207. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  208. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  209. }
  210. }
  211. void HighSpeedTest::CreateFastSmallConvexObjects()
  212. {
  213. // Create small convex hull
  214. Array<Vec3> vertices = {
  215. Vec3(-0.044661f, 0.001230f, 0.003877f),
  216. Vec3(-0.024743f, -0.042562f, 0.003877f),
  217. Vec3(-0.012336f, -0.021073f, 0.048484f),
  218. Vec3(0.016066f, 0.028121f, -0.049904f),
  219. Vec3(-0.023734f, 0.043275f, -0.024153f),
  220. Vec3(0.020812f, 0.036341f, -0.019530f),
  221. Vec3(0.012495f, 0.021936f, 0.045288f),
  222. Vec3(0.026750f, 0.001230f, 0.049273f),
  223. Vec3(0.045495f, 0.001230f, -0.022077f),
  224. Vec3(0.022193f, -0.036274f, -0.021126f),
  225. Vec3(0.022781f, -0.037291f, 0.029558f),
  226. Vec3(0.014691f, -0.023280f, 0.052897f),
  227. Vec3(-0.012187f, -0.020815f, -0.040214f),
  228. Vec3(0.000541f, 0.001230f, -0.056224f),
  229. Vec3(-0.039882f, 0.001230f, -0.019461f),
  230. Vec3(0.000541f, 0.001230f, 0.056022f),
  231. Vec3(-0.020614f, -0.035411f, -0.020551f),
  232. Vec3(-0.019485f, 0.035916f, 0.027001f),
  233. Vec3(-0.023968f, 0.043680f, 0.003877f),
  234. Vec3(-0.020051f, 0.001230f, 0.039543f),
  235. Vec3(0.026213f, 0.001230f, -0.040589f),
  236. Vec3(-0.010797f, 0.020868f, 0.043152f),
  237. Vec3(-0.012378f, 0.023607f, -0.040876f)
  238. };
  239. ConvexHullShapeSettings convex_settings(vertices);
  240. convex_settings.SetEmbedded();
  241. BodyCreationSettings body_settings(&convex_settings, RVec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  242. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  243. // Create many instances with high velocity
  244. default_random_engine rnd;
  245. uniform_real_distribution<float> restitution_distrib(0.0f, 0.1f);
  246. uniform_real_distribution<float> velocity_distrib(-10.0f, 10.0f);
  247. for (int x = -25; x < 25; ++x)
  248. for (int y = -25 ; y < 25; ++y)
  249. {
  250. // Cast a ray to find the terrain
  251. RVec3 origin(Real(x), 100.0_r, Real(y));
  252. Vec3 direction(0, -100.0f, 0);
  253. RRayCast ray { origin, direction };
  254. RayCastResult hit;
  255. if (mPhysicsSystem->GetNarrowPhaseQuery().CastRay(ray, hit, SpecifiedBroadPhaseLayerFilter(BroadPhaseLayers::NON_MOVING), SpecifiedObjectLayerFilter(Layers::NON_MOVING)))
  256. {
  257. // Place 10m above terrain
  258. body_settings.mPosition = ray.GetPointOnRay(hit.mFraction) + RVec3(0, 10, 0);
  259. body_settings.mRotation = Quat::sRandom(rnd);
  260. body_settings.mRestitution = restitution_distrib(rnd);
  261. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  262. body.SetLinearVelocity(Vec3(velocity_distrib(rnd), -100.0f, velocity_distrib(rnd)));
  263. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  264. }
  265. }
  266. }
  267. void HighSpeedTest::CreateConvexOnLargeTriangles()
  268. {
  269. // Create floor
  270. CreateLargeTriangleFloor();
  271. CreateFastSmallConvexObjects();
  272. }
  273. void HighSpeedTest::CreateConvexOnTerrain1()
  274. {
  275. #ifdef JPH_OBJECT_STREAM
  276. // Load scene
  277. Ref<PhysicsScene> scene;
  278. if (!ObjectStreamIn::sReadObject("Assets/terrain1.bof", scene))
  279. FatalError("Failed to load scene");
  280. for (BodyCreationSettings &body : scene->GetBodies())
  281. body.mObjectLayer = Layers::NON_MOVING;
  282. scene->FixInvalidScales();
  283. scene->CreateBodies(mPhysicsSystem);
  284. #else
  285. CreateFloor();
  286. #endif // JPH_OBJECT_STREAM
  287. CreateFastSmallConvexObjects();
  288. }
  289. void HighSpeedTest::Initialize()
  290. {
  291. switch (sSelectedScene)
  292. {
  293. case 0:
  294. CreateSimpleScene();
  295. break;
  296. case 1:
  297. CreateConvexOnLargeTriangles();
  298. break;
  299. case 2:
  300. CreateConvexOnTerrain1();
  301. break;
  302. default:
  303. JPH_ASSERT(false);
  304. break;
  305. }
  306. }
  307. void HighSpeedTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  308. {
  309. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  310. UIElement *scene_name = inUI->CreateMenu();
  311. for (uint i = 0; i < size(sScenes); ++i)
  312. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSelectedScene = i; RestartTest(); });
  313. inUI->ShowMenu(scene_name);
  314. });
  315. }