BallMove.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "BallMove.h"
  9. #include "Actor.h"
  10. #include "Game.h"
  11. #include "PhysWorld.h"
  12. #include "TargetActor.h"
  13. #include "BallActor.h"
  14. BallMove::BallMove(Actor* owner)
  15. :MoveComponent(owner)
  16. {
  17. }
  18. void BallMove::Update(float deltaTime)
  19. {
  20. const float segmentLength = 30.0f;
  21. PhysWorld* phys = mOwner->GetGame()->GetPhysWorld();
  22. // Construct segment in direction of travel
  23. Vector3 start = mOwner->GetPosition();
  24. Vector3 dir = mOwner->GetForward();
  25. Vector3 end = start + dir * segmentLength;
  26. // Create line segment
  27. LineSegment l(start, end);
  28. // Test segment vs world
  29. PhysWorld::CollisionInfo info;
  30. if (phys->SegmentCast(l, info))
  31. {
  32. // If we collided, reflect the ball about the normal
  33. dir = Vector3::Reflect(dir, info.mNormal);
  34. mOwner->RotateToNewForward(dir);
  35. // Did we hit a target?
  36. TargetActor* target = dynamic_cast<TargetActor*>(info.mActor);
  37. if (target)
  38. {
  39. static_cast<BallActor*>(mOwner)->HitTarget();
  40. }
  41. }
  42. MoveComponent::Update(deltaTime);
  43. }