JitterMoveEvent.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. {
  11. Error JitterMoveEvent::init(Second startTime, Second duration, SceneNode* node)
  12. {
  13. ANKI_ASSERT(node);
  14. Event::init(startTime, duration);
  15. m_associatedNodes.emplaceBack(getAllocator(), node);
  16. const MoveComponent& move = node->getFirstComponentOfType<MoveComponent>();
  17. m_originalPos = move.getLocalTransform().getOrigin();
  18. return Error::NONE;
  19. }
  20. void JitterMoveEvent::setPositionLimits(const Vec4& posMin, const Vec4& posMax)
  21. {
  22. for(U i = 0; i < 3; i++)
  23. {
  24. m_newPos[i] = getRandomRange(posMin[i], posMax[i]);
  25. }
  26. m_newPos[3] = 0.0;
  27. m_newPos += m_originalPos;
  28. }
  29. Error JitterMoveEvent::update(Second prevUpdateTime, Second crntTime)
  30. {
  31. SceneNode* node = m_associatedNodes[0];
  32. MoveComponent& move = node->getFirstComponentOfType<MoveComponent>();
  33. Transform trf = move.getLocalTransform();
  34. F32 factor = F32(sin(getDelta(crntTime) * PI));
  35. trf.getOrigin() = linearInterpolate(m_originalPos, m_newPos, factor);
  36. move.setLocalTransform(trf);
  37. return Error::NONE;
  38. }
  39. } // end namespace anki