godot_motion_state.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*************************************************************************/
  2. /* godot_motion_state.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_MOTION_STATE_H
  31. #define GODOT_MOTION_STATE_H
  32. #include "rigid_body_bullet.h"
  33. #include <LinearMath/btMotionState.h>
  34. /**
  35. @author AndreaCatania
  36. */
  37. class RigidBodyBullet;
  38. // This class is responsible to move kinematic actor
  39. // and sincronize rendering engine with Bullet
  40. /// DOC:
  41. /// http://www.bulletphysics.org/mediawiki-1.5.8/index.php/MotionStates#What.27s_a_MotionState.3F
  42. class GodotMotionState : public btMotionState {
  43. /// This data is used to store the new world position for kinematic body
  44. btTransform bodyKinematicWorldTransf;
  45. /// This data is used to store last world position
  46. btTransform bodyCurrentWorldTransform;
  47. RigidBodyBullet *owner;
  48. public:
  49. GodotMotionState(RigidBodyBullet *p_owner) :
  50. bodyKinematicWorldTransf(btMatrix3x3(1., 0., 0., 0., 1., 0., 0., 0., 1.), btVector3(0., 0., 0.)),
  51. bodyCurrentWorldTransform(btMatrix3x3(1., 0., 0., 0., 1., 0., 0., 0., 1.), btVector3(0., 0., 0.)),
  52. owner(p_owner) {}
  53. /// IMPORTANT DON'T USE THIS FUNCTION TO KNOW THE CURRENT BODY TRANSFORM
  54. /// This class is used internally by Bullet
  55. /// Use GodotMotionState::getCurrentWorldTransform to know current position
  56. ///
  57. /// This function is used by Bullet to get the position of object in the world
  58. /// if the body is kinematic Bullet will move the object to this location
  59. /// if the body is static Bullet doesn't move at all
  60. virtual void getWorldTransform(btTransform &worldTrans) const {
  61. worldTrans = bodyKinematicWorldTransf;
  62. }
  63. /// IMPORTANT: to move the body use: moveBody
  64. /// IMPORTANT: DON'T CALL THIS FUNCTION, IT IS CALLED BY BULLET TO UPDATE RENDERING ENGINE
  65. ///
  66. /// This function is called each time by Bullet and set the current position of body
  67. /// inside the physics world.
  68. /// Don't allow Godot rendering scene takes world transform from this object because
  69. /// the correct transform is set by Bullet only after the last step when there are sub steps
  70. /// This function must update Godot transform rendering scene for this object.
  71. virtual void setWorldTransform(const btTransform &worldTrans) {
  72. bodyCurrentWorldTransform = worldTrans;
  73. owner->scratch();
  74. }
  75. public:
  76. /// Use this function to move kinematic body
  77. /// -- or set initial transform before body creation.
  78. void moveBody(const btTransform &newWorldTransform) {
  79. bodyKinematicWorldTransf = newWorldTransform;
  80. }
  81. /// It returns the current body transform from last Bullet update
  82. const btTransform &getCurrentWorldTransform() const {
  83. return bodyCurrentWorldTransform;
  84. }
  85. };
  86. #endif