HighSpeedTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/HighSpeedTest.h>
  5. #include <Jolt/Physics/Collision/RayCast.h>
  6. #include <Jolt/Physics/Collision/CastResult.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  9. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  10. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  11. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  12. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  13. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  14. #include <Jolt/Physics/PhysicsScene.h>
  15. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  16. #include <Application/DebugUI.h>
  17. #include <Utils/Log.h>
  18. #include <Layers.h>
  19. JPH_IMPLEMENT_RTTI_VIRTUAL(HighSpeedTest)
  20. {
  21. JPH_ADD_BASE_CLASS(HighSpeedTest, Test)
  22. }
  23. const char *HighSpeedTest::sScenes[] =
  24. {
  25. "Simple",
  26. "Convex Hull On Large Triangles",
  27. "Convex Hull On Terrain1",
  28. };
  29. int HighSpeedTest::sSelectedScene = 0;
  30. void HighSpeedTest::CreateDominoBlocks(Vec3Arg inOffset, int inNumWalls, float inDensity, float inRadius)
  31. {
  32. BodyCreationSettings box_settings;
  33. Ref<BoxShape> box_shape = new BoxShape(Vec3(0.9f, 1.0f, 0.1f));
  34. box_shape->SetDensity(inDensity); // Make box more heavy so the bouncing ball keeps a higher velocity
  35. box_settings.SetShape(box_shape);
  36. box_settings.mObjectLayer = Layers::MOVING;
  37. // U shaped set of thin boxes
  38. for (int i = 0; i < inNumWalls; ++i)
  39. {
  40. box_settings.mPosition = inOffset + Vec3(2.0f * i, 1, -1.1f - inRadius);
  41. mBodyInterface->CreateAndAddBody(box_settings, EActivation::DontActivate);
  42. box_settings.mPosition = inOffset + Vec3(2.0f * i, 1, +1.1f + inRadius);
  43. mBodyInterface->CreateAndAddBody(box_settings, EActivation::DontActivate);
  44. }
  45. box_settings.mPosition = inOffset + Vec3(-1.1f - inRadius, 1, 0);
  46. box_settings.mRotation = Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI);
  47. mBodyInterface->CreateAndAddBody(box_settings, EActivation::DontActivate);
  48. }
  49. void HighSpeedTest::CreateDynamicObject(Vec3Arg inPosition, Vec3Arg inVelocity, Shape *inShape, EMotionQuality inMotionQuality)
  50. {
  51. BodyCreationSettings creation_settings;
  52. creation_settings.SetShape(inShape);
  53. creation_settings.mFriction = 0.0f;
  54. creation_settings.mRestitution = 1.0f;
  55. creation_settings.mLinearDamping = 0.0f;
  56. creation_settings.mAngularDamping = 0.0f;
  57. creation_settings.mMotionQuality = inMotionQuality;
  58. creation_settings.mObjectLayer = Layers::MOVING;
  59. creation_settings.mPosition = inPosition;
  60. Body &body = *mBodyInterface->CreateBody(creation_settings);
  61. body.SetLinearVelocity(inVelocity);
  62. mBodyInterface->AddBody(body.GetID(), inVelocity.IsNearZero()? EActivation::DontActivate : EActivation::Activate);
  63. }
  64. void HighSpeedTest::CreateSimpleScene()
  65. {
  66. // Floor
  67. CreateFloor();
  68. const float radius = 0.1f;
  69. const int num_walls = 5;
  70. const float density = 2000.0f;
  71. const float speed = 240.0f;
  72. Vec3 offset(0, 0, -30);
  73. {
  74. // U shaped set of thin walls
  75. TriangleList triangles;
  76. for (int i = 0; i < num_walls; ++i)
  77. {
  78. 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)));
  79. 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)));
  80. }
  81. triangles.push_back(Triangle(Float3(-1-radius,0,-1), Float3(-1-radius,2,0), Float3(-1-radius,0,1)));
  82. Body &walls = *mBodyInterface->CreateBody(BodyCreationSettings(new MeshShapeSettings(triangles), offset, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  83. walls.SetRestitution(1.0f);
  84. walls.SetFriction(0.0f);
  85. mBodyInterface->AddBody(walls.GetID(), EActivation::DontActivate);
  86. // Fast moving sphere against mesh
  87. CreateDynamicObject(offset + Vec3(2.0f * num_walls - 1, 1, 0), Vec3(-speed, 0, -speed), new SphereShape(radius));
  88. }
  89. offset += Vec3(0, 0, 5);
  90. {
  91. // Create wall of domino blocks
  92. CreateDominoBlocks(offset, num_walls, density, radius);
  93. // Fast moving sphere against domino blocks
  94. CreateDynamicObject(offset + Vec3(2.0f * num_walls - 1, 1, 0), Vec3(-speed, 0, -speed), new SphereShape(radius));
  95. }
  96. offset += Vec3(0, 0, 5);
  97. {
  98. // Create wall of domino blocks
  99. CreateDominoBlocks(offset, num_walls, density, radius);
  100. // Fast moving scaled box against domino blocks
  101. 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)));
  102. }
  103. offset += Vec3(0, 0, 5);
  104. {
  105. // Fast moving box stuck in ground moving, one moving up, one moving down
  106. CreateDynamicObject(offset + Vec3(-1, 0, 0), Vec3(0, speed, 0), new BoxShape(Vec3::sReplicate(radius)));
  107. CreateDynamicObject(offset + Vec3(1, 0, 0), Vec3(0, -speed, 0), new BoxShape(Vec3::sReplicate(radius)));
  108. }
  109. offset += Vec3(0, 0, 5);
  110. {
  111. // Single shape that has 4 walls to surround fast moving sphere
  112. BodyCreationSettings enclosing_settings;
  113. Ref<BoxShapeSettings> box_shape = new BoxShapeSettings(Vec3(1.0f, 1.0f, 0.1f));
  114. Ref<StaticCompoundShapeSettings> enclosing_shape = new StaticCompoundShapeSettings();
  115. enclosing_shape->AddShape(Vec3(0, 0, 1), Quat::sIdentity(), box_shape);
  116. enclosing_shape->AddShape(Vec3(0, 0, -1), Quat::sIdentity(), box_shape);
  117. enclosing_shape->AddShape(Vec3(1, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  118. enclosing_shape->AddShape(Vec3(-1, 0, 0), Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI), box_shape);
  119. enclosing_settings.SetShapeSettings(enclosing_shape);
  120. enclosing_settings.mMotionType = EMotionType::Kinematic;
  121. enclosing_settings.mObjectLayer = Layers::MOVING;
  122. enclosing_settings.mPosition = offset + Vec3(0, 1, 0);
  123. Body &enclosing = *mBodyInterface->CreateBody(enclosing_settings);
  124. mBodyInterface->AddBody(enclosing.GetID(), 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, Vec3::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. Vec3 origin(float(x), 100.0f, float(y));
  252. Vec3 direction(0, -100.0f, 0);
  253. RayCastResult hit;
  254. if (mPhysicsSystem->GetNarrowPhaseQuery().CastRay({ origin, direction }, hit, SpecifiedBroadPhaseLayerFilter(BroadPhaseLayers::NON_MOVING), SpecifiedObjectLayerFilter(Layers::NON_MOVING)))
  255. {
  256. // Place 10m above terrain
  257. body_settings.mPosition = origin + hit.mFraction * direction + Vec3(0, 10.0f, 0);
  258. body_settings.mRotation = Quat::sRandom(rnd);
  259. body_settings.mRestitution = restitution_distrib(rnd);
  260. Body &body = *mPhysicsSystem->GetBodyInterface().CreateBody(body_settings);
  261. body.SetLinearVelocity(Vec3(velocity_distrib(rnd), -100.0f, velocity_distrib(rnd)));
  262. mPhysicsSystem->GetBodyInterface().AddBody(body.GetID(), EActivation::Activate);
  263. }
  264. }
  265. }
  266. void HighSpeedTest::CreateConvexOnLargeTriangles()
  267. {
  268. // Create floor
  269. CreateLargeTriangleFloor();
  270. CreateFastSmallConvexObjects();
  271. }
  272. void HighSpeedTest::CreateConvexOnTerrain1()
  273. {
  274. // Load scene
  275. Ref<PhysicsScene> scene;
  276. if (!ObjectStreamIn::sReadObject("Assets/terrain1.bof", scene))
  277. FatalError("Failed to load scene");
  278. for (BodyCreationSettings &body : scene->GetBodies())
  279. body.mObjectLayer = Layers::NON_MOVING;
  280. scene->FixInvalidScales();
  281. scene->CreateBodies(mPhysicsSystem);
  282. CreateFastSmallConvexObjects();
  283. }
  284. void HighSpeedTest::Initialize()
  285. {
  286. switch (sSelectedScene)
  287. {
  288. case 0:
  289. CreateSimpleScene();
  290. break;
  291. case 1:
  292. CreateConvexOnLargeTriangles();
  293. break;
  294. case 2:
  295. CreateConvexOnTerrain1();
  296. break;
  297. default:
  298. JPH_ASSERT(false);
  299. break;
  300. }
  301. }
  302. void HighSpeedTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  303. {
  304. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  305. UIElement *scene_name = inUI->CreateMenu();
  306. for (uint i = 0; i < size(sScenes); ++i)
  307. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSelectedScene = i; RestartTest(); });
  308. inUI->ShowMenu(scene_name);
  309. });
  310. }