2
0

MeshShapeTest.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/Shapes/MeshShapeTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  8. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  9. #include <Jolt/Geometry/Triangle.h>
  10. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  11. #include <Layers.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(MeshShapeTest)
  13. {
  14. JPH_ADD_BASE_CLASS(MeshShapeTest, Test)
  15. }
  16. void MeshShapeTest::Initialize()
  17. {
  18. // Create regular grid of triangles
  19. uint32 max_material_index = 0;
  20. TriangleList triangles;
  21. for (int x = -10; x < 10; ++x)
  22. for (int z = -10; z < 10; ++z)
  23. {
  24. float x1 = 10.0f * x;
  25. float z1 = 10.0f * z;
  26. float x2 = x1 + 10.0f;
  27. float z2 = z1 + 10.0f;
  28. Float3 v1 = Float3(x1, 0, z1);
  29. Float3 v2 = Float3(x2, 0, z1);
  30. Float3 v3 = Float3(x1, 0, z2);
  31. Float3 v4 = Float3(x2, 0, z2);
  32. uint32 material_index = uint32((Vec3(v1) + Vec3(v2) + Vec3(v3) + Vec3(v4)).Length() / 40.0f);
  33. max_material_index = max(max_material_index, material_index);
  34. triangles.push_back(Triangle(v1, v3, v4, material_index));
  35. triangles.push_back(Triangle(v1, v4, v2, material_index));
  36. }
  37. // Create materials
  38. PhysicsMaterialList materials;
  39. for (uint i = 0; i <= max_material_index; ++i)
  40. materials.push_back(new PhysicsMaterialSimple("Material " + ConvertToString(i), Color::sGetDistinctColor(i)));
  41. // Floor
  42. Body &floor = *mBodyInterface->CreateBody(BodyCreationSettings(new MeshShapeSettings(triangles, std::move(materials)), RVec3::sZero(), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Static, Layers::NON_MOVING));
  43. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  44. // 1 body with zero friction to test active edge detection
  45. Ref<BoxShape> box_shape = new BoxShape(Vec3(2.0f, 2.0f, 2.0f), cDefaultConvexRadius, new PhysicsMaterialSimple("Box Material", Color::sYellow));
  46. Body &body = *mBodyInterface->CreateBody(BodyCreationSettings(box_shape, RVec3(0, 55.0f, -50.0f), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  47. body.SetFriction(0.0f);
  48. mBodyInterface->AddBody(body.GetID(), EActivation::Activate);
  49. }