ConeConstraintTest.cpp 2.1 KB

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