2
0

ConeConstraintTest.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/Constraints/ConeConstraintTest.h>
  6. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  7. #include <Jolt/Physics/Collision/GroupFilterTable.h>
  8. #include <Jolt/Physics/Constraints/ConeConstraint.h>
  9. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  10. #include <Layers.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(ConeConstraintTest)
  12. {
  13. JPH_ADD_BASE_CLASS(ConeConstraintTest, Test)
  14. }
  15. void ConeConstraintTest::Initialize()
  16. {
  17. // Floor
  18. CreateFloor();
  19. float half_cylinder_height = 2.5f;
  20. const int cChainLength = 5;
  21. // Build a collision group filter that disables collision between adjacent bodies
  22. Ref<GroupFilterTable> group_filter = new GroupFilterTable(cChainLength);
  23. for (CollisionGroup::SubGroupID i = 0; i < cChainLength - 1; ++i)
  24. group_filter->DisableCollision(i, i + 1);
  25. // Bodies attached through cone constraints
  26. for (int j = 0; j < 2; ++j)
  27. {
  28. Body *prev = nullptr;
  29. Quat rotation = Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI);
  30. RVec3 position(0, 20.0f, 10.0f * j);
  31. for (int i = 0; i < cChainLength; ++i)
  32. {
  33. position += Vec3(2.0f * half_cylinder_height, 0, 0);
  34. Body &segment = *mBodyInterface->CreateBody(BodyCreationSettings(new CapsuleShape(half_cylinder_height, 1), position, Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI * i) * rotation, i == 0? EMotionType::Static : EMotionType::Dynamic, i == 0? Layers::NON_MOVING : Layers::MOVING));
  35. segment.SetCollisionGroup(CollisionGroup(group_filter, CollisionGroup::GroupID(j), CollisionGroup::SubGroupID(i)));
  36. mBodyInterface->AddBody(segment.GetID(), EActivation::Activate);
  37. if (prev != nullptr)
  38. {
  39. ConeConstraintSettings settings;
  40. settings.mPoint1 = settings.mPoint2 = position + Vec3(-half_cylinder_height, 0, 0);
  41. settings.mTwistAxis1 = settings.mTwistAxis2 = Vec3(1, 0, 0);
  42. if (j == 0)
  43. settings.mHalfConeAngle = 0.0f;
  44. else
  45. settings.mHalfConeAngle = DegreesToRadians(20);
  46. mPhysicsSystem->AddConstraint(settings.Create(*prev, segment));
  47. }
  48. prev = &segment;
  49. }
  50. }
  51. }