BoxComponent.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "BoxComponent.h"
  9. #include "Actor.h"
  10. #include "Game.h"
  11. #include "PhysWorld.h"
  12. BoxComponent::BoxComponent(Actor* owner, int updateOrder)
  13. :Component(owner, updateOrder)
  14. ,mObjectBox(Vector3::Zero, Vector3::Zero)
  15. ,mWorldBox(Vector3::Zero, Vector3::Zero)
  16. ,mShouldRotate(true)
  17. {
  18. mOwner->GetGame()->GetPhysWorld()->AddBox(this);
  19. }
  20. BoxComponent::~BoxComponent()
  21. {
  22. mOwner->GetGame()->GetPhysWorld()->RemoveBox(this);
  23. }
  24. void BoxComponent::OnUpdateWorldTransform()
  25. {
  26. // Reset to object space box
  27. mWorldBox = mObjectBox;
  28. // Scale
  29. mWorldBox.mMin *= mOwner->GetScale();
  30. mWorldBox.mMax *= mOwner->GetScale();
  31. // Rotate (if we want to)
  32. if (mShouldRotate)
  33. {
  34. mWorldBox.Rotate(mOwner->GetRotation());
  35. }
  36. // Translate
  37. mWorldBox.mMin += mOwner->GetPosition();
  38. mWorldBox.mMax += mOwner->GetPosition();
  39. }