HighSpeedTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. Body &enclosing = *mBodyInterface->CreateBody(enclosing_settings);
  125. mBodyInterface->AddBody(enclosing.GetID(), EActivation::Activate);
  126. // Fast moving sphere in box
  127. CreateDynamicObject(offset + Vec3(0, 0.5f, 0), Vec3(-speed, 0, -0.5f * speed), new SphereShape(radius));
  128. }
  129. offset += Vec3(0, 0, 5);
  130. {
  131. // Two boxes on a collision course
  132. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  133. CreateDynamicObject(offset + Vec3(-1, 0.5f, 0), Vec3(speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  134. }
  135. offset += Vec3(0, 0, 5);
  136. {
  137. // Two boxes on a collision course, off center
  138. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  139. CreateDynamicObject(offset + Vec3(-1, 0.5f, radius), Vec3(speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  140. }
  141. offset += Vec3(0, 0, 5);
  142. {
  143. // Two boxes on a collision course, one discrete
  144. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  145. CreateDynamicObject(offset + Vec3(-1, 0.5f, 0), Vec3(60.0f, 0, 0), new BoxShape(Vec3::sReplicate(radius)), EMotionQuality::Discrete);
  146. }
  147. offset += Vec3(0, 0, 5);
  148. {
  149. // Two boxes on a collision course, one inactive
  150. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  151. CreateDynamicObject(offset + Vec3(0, 0.5f, 0), Vec3::sZero(), new BoxShape(Vec3::sReplicate(radius)));
  152. }
  153. offset += Vec3(0, 0, 5);
  154. {
  155. // Two boxes on a collision course, one inactive and discrete
  156. CreateDynamicObject(offset + Vec3(1, 0.5f, 0), Vec3(-speed, 0, 0), new BoxShape(Vec3::sReplicate(radius)));
  157. CreateDynamicObject(offset + Vec3(0, 0.5f, 0), Vec3::sZero(), new BoxShape(Vec3::sReplicate(radius)), EMotionQuality::Discrete);
  158. }
  159. offset += Vec3(0, 0, 5);
  160. {
  161. // Create long thin shape
  162. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  163. box_settings.SetEmbedded();
  164. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  165. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  166. body_settings.mRestitution = 0.0f;
  167. body_settings.mFriction = 1.0f;
  168. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  169. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  170. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  171. }
  172. offset += Vec3(0, 0, 5);
  173. {
  174. // Create long thin shape under 45 degrees
  175. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  176. box_settings.SetEmbedded();
  177. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  178. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  179. body_settings.mRestitution = 0.0f;
  180. body_settings.mFriction = 1.0f;
  181. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  182. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  183. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  184. }
  185. offset += Vec3(0, 0, 5);
  186. {
  187. // Create long thin shape with restitution
  188. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  189. box_settings.SetEmbedded();
  190. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  191. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  192. body_settings.mRestitution = 1.0f;
  193. body_settings.mFriction = 1.0f;
  194. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  195. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  196. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  197. }
  198. offset += Vec3(0, 0, 5);
  199. {
  200. // Create long thin shape under 45 degrees with restitution
  201. BoxShapeSettings box_settings(Vec3(0.05f, 0.8f, 0.03f), 0.015f);
  202. box_settings.SetEmbedded();
  203. BodyCreationSettings body_settings(&box_settings, offset + Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  204. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  205. body_settings.mRestitution = 1.0f;
  206. body_settings.mFriction = 1.0f;
  207. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  208. body.SetLinearVelocity(Vec3(0, -100.0f, 0));
  209. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  210. }
  211. }
  212. void HighSpeedTest::CreateFastSmallConvexObjects()
  213. {
  214. // Create small convex hull
  215. Array<Vec3> vertices = {
  216. Vec3(-0.044661f, 0.001230f, 0.003877f),
  217. Vec3(-0.024743f, -0.042562f, 0.003877f),
  218. Vec3(-0.012336f, -0.021073f, 0.048484f),
  219. Vec3(0.016066f, 0.028121f, -0.049904f),
  220. Vec3(-0.023734f, 0.043275f, -0.024153f),
  221. Vec3(0.020812f, 0.036341f, -0.019530f),
  222. Vec3(0.012495f, 0.021936f, 0.045288f),
  223. Vec3(0.026750f, 0.001230f, 0.049273f),
  224. Vec3(0.045495f, 0.001230f, -0.022077f),
  225. Vec3(0.022193f, -0.036274f, -0.021126f),
  226. Vec3(0.022781f, -0.037291f, 0.029558f),
  227. Vec3(0.014691f, -0.023280f, 0.052897f),
  228. Vec3(-0.012187f, -0.020815f, -0.040214f),
  229. Vec3(0.000541f, 0.001230f, -0.056224f),
  230. Vec3(-0.039882f, 0.001230f, -0.019461f),
  231. Vec3(0.000541f, 0.001230f, 0.056022f),
  232. Vec3(-0.020614f, -0.035411f, -0.020551f),
  233. Vec3(-0.019485f, 0.035916f, 0.027001f),
  234. Vec3(-0.023968f, 0.043680f, 0.003877f),
  235. Vec3(-0.020051f, 0.001230f, 0.039543f),
  236. Vec3(0.026213f, 0.001230f, -0.040589f),
  237. Vec3(-0.010797f, 0.020868f, 0.043152f),
  238. Vec3(-0.012378f, 0.023607f, -0.040876f)
  239. };
  240. ConvexHullShapeSettings convex_settings(vertices);
  241. convex_settings.SetEmbedded();
  242. BodyCreationSettings body_settings(&convex_settings, RVec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  243. body_settings.mMotionQuality = EMotionQuality::LinearCast;
  244. // Create many instances with high velocity
  245. default_random_engine rnd;
  246. uniform_real_distribution<float> restitution_distrib(0.0f, 0.1f);
  247. uniform_real_distribution<float> velocity_distrib(-10.0f, 10.0f);
  248. for (int x = -25; x < 25; ++x)
  249. for (int y = -25 ; y < 25; ++y)
  250. {
  251. // Cast a ray to find the terrain
  252. RVec3 origin(Real(x), 100.0_r, Real(y));
  253. Vec3 direction(0, -100.0f, 0);
  254. RRayCast ray { origin, direction };
  255. RayCastResult hit;
  256. if (mPhysicsSystem->GetNarrowPhaseQuery().CastRay(ray, hit, SpecifiedBroadPhaseLayerFilter(BroadPhaseLayers::NON_MOVING), SpecifiedObjectLayerFilter(Layers::NON_MOVING)))
  257. {
  258. // Place 10m above terrain
  259. body_settings.mPosition = ray.GetPointOnRay(hit.mFraction) + RVec3(0, 10, 0);
  260. body_settings.mRotation = Quat::sRandom(rnd);
  261. body_settings.mRestitution = restitution_distrib(rnd);
  262. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  263. body.SetLinearVelocity(Vec3(velocity_distrib(rnd), -100.0f, velocity_distrib(rnd)));
  264. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  265. }
  266. }
  267. }
  268. void HighSpeedTest::CreateConvexOnLargeTriangles()
  269. {
  270. // Create floor
  271. CreateLargeTriangleFloor();
  272. CreateFastSmallConvexObjects();
  273. }
  274. void HighSpeedTest::CreateConvexOnTerrain1()
  275. {
  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. CreateFastSmallConvexObjects();
  285. }
  286. void HighSpeedTest::Initialize()
  287. {
  288. switch (sSelectedScene)
  289. {
  290. case 0:
  291. CreateSimpleScene();
  292. break;
  293. case 1:
  294. CreateConvexOnLargeTriangles();
  295. break;
  296. case 2:
  297. CreateConvexOnTerrain1();
  298. break;
  299. default:
  300. JPH_ASSERT(false);
  301. break;
  302. }
  303. }
  304. void HighSpeedTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  305. {
  306. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  307. UIElement *scene_name = inUI->CreateMenu();
  308. for (uint i = 0; i < size(sScenes); ++i)
  309. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSelectedScene = i; RestartTest(); });
  310. inUI->ShowMenu(scene_name);
  311. });
  312. }