CSceneNodeAnimatorFlyCircle.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CSceneNodeAnimatorFlyCircle.h"
  5. namespace irr
  6. {
  7. namespace scene
  8. {
  9. //! constructor
  10. CSceneNodeAnimatorFlyCircle::CSceneNodeAnimatorFlyCircle(u32 time,
  11. const core::vector3df& center, f32 radius, f32 speed,
  12. const core::vector3df& direction, f32 radiusEllipsoid)
  13. : Center(center), Direction(direction), Radius(radius),
  14. RadiusEllipsoid(radiusEllipsoid), Speed(speed), StartTime(time)
  15. {
  16. #ifdef _DEBUG
  17. setDebugName("CSceneNodeAnimatorFlyCircle");
  18. #endif
  19. init();
  20. }
  21. void CSceneNodeAnimatorFlyCircle::init()
  22. {
  23. Direction.normalize();
  24. if (Direction.Y != 0)
  25. VecV = core::vector3df(50,0,0).crossProduct(Direction).normalize();
  26. else
  27. VecV = core::vector3df(0,50,0).crossProduct(Direction).normalize();
  28. VecU = VecV.crossProduct(Direction).normalize();
  29. }
  30. //! animates a scene node
  31. void CSceneNodeAnimatorFlyCircle::animateNode(ISceneNode* node, u32 timeMs)
  32. {
  33. if ( 0 == node )
  34. return;
  35. f32 time;
  36. // Check for the condition where the StartTime is in the future.
  37. if(StartTime > timeMs)
  38. time = ((s32)timeMs - (s32)StartTime) * Speed;
  39. else
  40. time = (timeMs-StartTime) * Speed;
  41. // node->setPosition(Center + Radius * ((VecU*cosf(time)) + (VecV*sinf(time))));
  42. f32 r2 = RadiusEllipsoid == 0.f ? Radius : RadiusEllipsoid;
  43. node->setPosition(Center + (Radius*cosf(time)*VecU) + (r2*sinf(time)*VecV ) );
  44. }
  45. //! Writes attributes of the scene node animator.
  46. void CSceneNodeAnimatorFlyCircle::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
  47. {
  48. out->addVector3d("Center", Center);
  49. out->addFloat("Radius", Radius);
  50. out->addFloat("Speed", Speed);
  51. out->addVector3d("Direction", Direction);
  52. out->addFloat("RadiusEllipsoid", RadiusEllipsoid);
  53. }
  54. //! Reads attributes of the scene node animator.
  55. void CSceneNodeAnimatorFlyCircle::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
  56. {
  57. Center = in->getAttributeAsVector3d("Center");
  58. Radius = in->getAttributeAsFloat("Radius");
  59. Speed = in->getAttributeAsFloat("Speed");
  60. Direction = in->getAttributeAsVector3d("Direction");
  61. StartTime = 0;
  62. if (Direction.equals(core::vector3df(0,0,0)))
  63. Direction.set(0,1,0); // irrlicht 1.1 backwards compatibility
  64. else
  65. Direction.normalize();
  66. RadiusEllipsoid = in->getAttributeAsFloat("RadiusEllipsoid");
  67. init();
  68. }
  69. ISceneNodeAnimator* CSceneNodeAnimatorFlyCircle::createClone(ISceneNode* node, ISceneManager* newManager)
  70. {
  71. CSceneNodeAnimatorFlyCircle * newAnimator =
  72. new CSceneNodeAnimatorFlyCircle(StartTime, Center, Radius, Speed, Direction, RadiusEllipsoid);
  73. return newAnimator;
  74. }
  75. } // end namespace scene
  76. } // end namespace irr