raycastColliderComponent.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "T3D/components/collision/raycastColliderComponent.h"
  2. #include "T3D/physics/physicsPlugin.h"
  3. IMPLEMENT_CO_DATABLOCK_V1(RaycastColliderComponent);
  4. RaycastColliderComponent::RaycastColliderComponent() :
  5. mUseVelocity(false),
  6. mOwnerPhysicsComponent(nullptr),
  7. mRayDirection(VectorF::Zero),
  8. mRayLength(1),
  9. mPhysicsWorld(nullptr),
  10. mOldPosition(Point3F::Zero),
  11. mMask(-1)
  12. {
  13. }
  14. RaycastColliderComponent::~RaycastColliderComponent()
  15. {
  16. }
  17. bool RaycastColliderComponent::onAdd()
  18. {
  19. if (!Parent::onAdd())
  20. return false;
  21. if (PHYSICSMGR)
  22. mPhysicsWorld = PHYSICSMGR->getWorld(isServerObject() ? "server" : "client");
  23. return true;
  24. }
  25. void RaycastColliderComponent::onRemove()
  26. {
  27. Parent::onRemove();
  28. }
  29. void RaycastColliderComponent::initPersistFields()
  30. {
  31. Parent::initPersistFields();
  32. }
  33. void RaycastColliderComponent::onComponentAdd()
  34. {
  35. PhysicsComponent* physComp = mOwner->getComponent<PhysicsComponent>();
  36. if (physComp)
  37. {
  38. mOwnerPhysicsComponent = physComp;
  39. }
  40. }
  41. void RaycastColliderComponent::onComponentRemove()
  42. {
  43. mOwnerPhysicsComponent = nullptr;
  44. }
  45. void RaycastColliderComponent::componentAddedToOwner(Component *comp)
  46. {
  47. Parent::componentAddedToOwner(comp);
  48. PhysicsComponent* physComp = dynamic_cast<PhysicsComponent*>(comp);
  49. if (physComp)
  50. {
  51. mOwnerPhysicsComponent = physComp;
  52. }
  53. }
  54. void RaycastColliderComponent::componentRemovedFromOwner(Component *comp)
  55. {
  56. Parent::componentRemovedFromOwner(comp);
  57. if (mOwnerPhysicsComponent != nullptr && mOwnerPhysicsComponent->getId() == comp->getId())
  58. {
  59. mOwnerPhysicsComponent = nullptr;
  60. }
  61. }
  62. void RaycastColliderComponent::processTick()
  63. {
  64. Parent::processTick();
  65. // Raycast the abstract PhysicsWorld if a PhysicsPlugin exists.
  66. bool hit = false;
  67. Point3F start = mOldPosition;
  68. Point3F end;
  69. if (mUseVelocity)
  70. {
  71. //our end is the new position
  72. end = mOwner->getPosition();
  73. }
  74. else
  75. {
  76. end = start + (mRayDirection * mRayLength);
  77. }
  78. RayInfo rInfo;
  79. if (mPhysicsWorld)
  80. hit = mPhysicsWorld->castRay(start, end, &rInfo, Point3F::Zero);
  81. else
  82. hit = mOwner->getContainer()->castRay(start, end, mMask, &rInfo);
  83. if (hit)
  84. {
  85. }
  86. if (mUseVelocity)
  87. mOldPosition = end;
  88. }
  89. void RaycastColliderComponent::interpolateTick(F32 dt)
  90. {
  91. Parent::interpolateTick(dt);
  92. }
  93. void RaycastColliderComponent::advanceTime(F32 dt)
  94. {
  95. Parent::advanceTime(dt);
  96. }