PlayerNode.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2009-2020, 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/PlayerNode.h>
  6. #include <anki/scene/SceneGraph.h>
  7. #include <anki/scene/components/MoveComponent.h>
  8. #include <anki/scene/components/PlayerControllerComponent.h>
  9. #include <anki/physics/PhysicsPlayerController.h>
  10. #include <anki/physics/PhysicsWorld.h>
  11. #include <anki/input/Input.h>
  12. namespace anki
  13. {
  14. /// Feedback component.
  15. class PlayerNode::FeedbackComponent final : public SceneComponent
  16. {
  17. public:
  18. FeedbackComponent()
  19. : SceneComponent(SceneComponentType::NONE)
  20. {
  21. }
  22. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  23. {
  24. updated = false;
  25. PlayerControllerComponent& playerc = node.getFirstComponentOfType<PlayerControllerComponent>();
  26. const Input& in = node.getSceneGraph().getInput();
  27. const F32 ang = toRad(7.0f);
  28. F32 y = in.getMousePosition().y();
  29. F32 x = in.getMousePosition().x();
  30. if(playerc.getTimestamp() == node.getGlobalTimestamp() || y != 0.0 || x != 0.0)
  31. {
  32. MoveComponent& move = node.getFirstComponentOfType<MoveComponent>();
  33. // Set origin
  34. Vec4 origin = playerc.getTransform().getOrigin();
  35. origin.y() += 1.9f;
  36. // Set rotation
  37. Mat3x4 rot(Vec3(0.0f), Euler(ang * y * 11.25f, ang * x * -20.0f, 0.0f));
  38. rot = move.getLocalRotation().combineTransformations(rot);
  39. Vec3 newz = rot.getColumn(2).getNormalized();
  40. Vec3 newx = Vec3(0.0, 1.0, 0.0).cross(newz);
  41. Vec3 newy = newz.cross(newx);
  42. rot.setColumns(newx, newy, newz, Vec3(0.0));
  43. rot.reorthogonalize();
  44. // Update move
  45. move.setLocalTransform(Transform(origin, rot, 1.0));
  46. }
  47. return Error::NONE;
  48. }
  49. };
  50. /// Feedback component.
  51. class PlayerNode::FeedbackComponent2 final : public SceneComponent
  52. {
  53. public:
  54. FeedbackComponent2()
  55. : SceneComponent(SceneComponentType::NONE)
  56. {
  57. }
  58. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  59. {
  60. updated = false;
  61. PlayerControllerComponent& playerc = node.getFirstComponentOfType<PlayerControllerComponent>();
  62. MoveComponent& move = node.getFirstComponentOfType<MoveComponent>();
  63. const Input& in = node.getSceneGraph().getInput();
  64. const F32 speed = 0.5;
  65. Vec4 moveVec(0.0);
  66. if(in.getKey(KeyCode::W))
  67. {
  68. moveVec.z() += 1.0f;
  69. }
  70. if(in.getKey(KeyCode::A))
  71. {
  72. moveVec.x() -= 1.0f;
  73. }
  74. if(in.getKey(KeyCode::S))
  75. {
  76. moveVec.z() -= 1.0f;
  77. }
  78. if(in.getKey(KeyCode::D))
  79. {
  80. moveVec.x() += 1.0f;
  81. }
  82. Vec4 dir = -move.getLocalRotation().getZAxis().xyz0();
  83. dir.y() = 0.0f;
  84. dir.normalize();
  85. playerc.setVelocity(moveVec.z() * speed, moveVec.x() * speed, 0.0, dir);
  86. return Error::NONE;
  87. }
  88. };
  89. PlayerNode::PlayerNode(SceneGraph* scene, CString name)
  90. : SceneNode(scene, name)
  91. {
  92. }
  93. PlayerNode::~PlayerNode()
  94. {
  95. }
  96. Error PlayerNode::init(const Vec4& position)
  97. {
  98. // Create physics object
  99. PhysicsPlayerControllerInitInfo init;
  100. init.m_position = position;
  101. m_player = getSceneGraph().getPhysicsWorld().newInstance<PhysicsPlayerController>(init);
  102. m_player->setUserData(this);
  103. // Player controller component
  104. newComponent<PlayerControllerComponent>(m_player);
  105. // Feedback component
  106. newComponent<FeedbackComponent>();
  107. // Move component
  108. newComponent<MoveComponent>();
  109. // Feedback component #2
  110. newComponent<FeedbackComponent2>();
  111. return Error::NONE;
  112. }
  113. } // end namespace anki