JitterMoveEvent.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Events/JitterMoveEvent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/Components/MoveComponent.h>
  8. #include <AnKi/Util/Functions.h>
  9. namespace anki {
  10. Error JitterMoveEvent::init(Second startTime, Second duration, SceneNode* node)
  11. {
  12. ANKI_ASSERT(node);
  13. Event::init(startTime, duration);
  14. m_associatedNodes.emplaceBack(getAllocator(), node);
  15. const MoveComponent& move = node->getFirstComponentOfType<MoveComponent>();
  16. m_originalPos = move.getLocalTransform().getOrigin();
  17. return Error::NONE;
  18. }
  19. void JitterMoveEvent::setPositionLimits(const Vec4& posMin, const Vec4& posMax)
  20. {
  21. for(U i = 0; i < 3; i++)
  22. {
  23. m_newPos[i] = getRandomRange(posMin[i], posMax[i]);
  24. }
  25. m_newPos[3] = 0.0;
  26. m_newPos += m_originalPos;
  27. }
  28. Error JitterMoveEvent::update(Second prevUpdateTime, Second crntTime)
  29. {
  30. SceneNode* node = m_associatedNodes[0];
  31. MoveComponent& move = node->getFirstComponentOfType<MoveComponent>();
  32. Transform trf = move.getLocalTransform();
  33. F32 factor = F32(sin(getDelta(crntTime) * PI));
  34. trf.getOrigin() = linearInterpolate(m_originalPos, m_newPos, factor);
  35. move.setLocalTransform(trf);
  36. return Error::NONE;
  37. }
  38. } // end namespace anki