| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #ifndef LOVE_PHYSICS_BOX2D_JOINT_H |
| 22 | #define LOVE_PHYSICS_BOX2D_JOINT_H |
| 23 | |
| 24 | // LOVE |
| 25 | #include "common/runtime.h" |
| 26 | #include "physics/Joint.h" |
| 27 | |
| 28 | // Box2D |
| 29 | #include <Box2D/Box2D.h> |
| 30 | |
| 31 | namespace love |
| 32 | { |
| 33 | namespace physics |
| 34 | { |
| 35 | namespace box2d |
| 36 | { |
| 37 | |
| 38 | // Forward declarations. |
| 39 | class Body; |
| 40 | class World; |
| 41 | |
| 42 | /** |
| 43 | * This struct is stored in a void pointer in the Box2D Joint class. For now, all |
| 44 | * we need is a Lua reference to arbitrary data, but we might need more later. |
| 45 | **/ |
| 46 | struct jointudata |
| 47 | { |
| 48 | // Reference to arbitrary data. |
| 49 | Reference *ref = nullptr; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * A Joint acts as positioning constraints on Bodies. |
| 54 | * A Joint can be used to prevent Bodies from going to |
| 55 | * far apart, or coming too close together. |
| 56 | **/ |
| 57 | class Joint : public love::physics::Joint |
| 58 | { |
| 59 | public: |
| 60 | friend class GearJoint; |
| 61 | |
| 62 | /** |
| 63 | * This constructor will connect one end of the joint to body1, |
| 64 | * and the other one to the default ground body. |
| 65 | * |
| 66 | * This constructor is mainly used by MouseJoint. |
| 67 | **/ |
| 68 | Joint(Body *body1); |
| 69 | |
| 70 | /** |
| 71 | * Create a joint between body1 and body2. |
| 72 | **/ |
| 73 | Joint(Body *body1, Body *body2); |
| 74 | |
| 75 | virtual ~Joint(); |
| 76 | |
| 77 | /** |
| 78 | * Returns true if the joint is active in a Box2D world. |
| 79 | **/ |
| 80 | bool isValid() const; |
| 81 | |
| 82 | /** |
| 83 | * Gets the type of joint. |
| 84 | **/ |
| 85 | Type getType() const; |
| 86 | |
| 87 | virtual Body *getBodyA() const; |
| 88 | virtual Body *getBodyB() const; |
| 89 | |
| 90 | /** |
| 91 | * Gets the anchor positions of the Joint in world |
| 92 | * coordinates. This is useful for debugdrawing the joint. |
| 93 | **/ |
| 94 | int getAnchors(lua_State *L); |
| 95 | |
| 96 | /** |
| 97 | * Gets the reaction force on body2 at the joint anchor. |
| 98 | **/ |
| 99 | int getReactionForce(lua_State *L); |
| 100 | |
| 101 | /** |
| 102 | * Gets the reaction torque on body2. |
| 103 | **/ |
| 104 | float getReactionTorque(float dt); |
| 105 | |
| 106 | bool isActive() const; |
| 107 | |
| 108 | bool getCollideConnected() const; |
| 109 | |
| 110 | /** |
| 111 | * This function stores an in-C reference to arbitrary Lua data in the Box2D |
| 112 | * Joint object. |
| 113 | **/ |
| 114 | int setUserData(lua_State *L); |
| 115 | |
| 116 | /** |
| 117 | * Gets the data set with setUserData. If no data is set, nil is returned. |
| 118 | **/ |
| 119 | int getUserData(lua_State *L); |
| 120 | |
| 121 | /** |
| 122 | * Joints require pointers to a Box2D joint objects at |
| 123 | * different polymorphic levels, which is why these function |
| 124 | * were created. |
| 125 | **/ |
| 126 | |
| 127 | /** |
| 128 | * Destroys the joint. This function was created just to |
| 129 | * get some cinsistency. |
| 130 | **/ |
| 131 | void destroyJoint(bool implicit = false); |
| 132 | |
| 133 | protected: |
| 134 | |
| 135 | /** |
| 136 | * Creates a Joint, and ensures that the parent class |
| 137 | * gets a copy of the pointer. |
| 138 | **/ |
| 139 | b2Joint *createJoint(b2JointDef *def); |
| 140 | |
| 141 | World *world; |
| 142 | |
| 143 | jointudata *udata; |
| 144 | |
| 145 | private: |
| 146 | |
| 147 | // A Joint must be destroyed *before* the bodies it acts upon, |
| 148 | // and the world they reside in. We therefore need refs |
| 149 | // parents and associations to prevent wrong destruction order. |
| 150 | Body *body1, * body2; |
| 151 | |
| 152 | |
| 153 | // The Box2D joint object. |
| 154 | b2Joint *joint; |
| 155 | |
| 156 | }; // Joint |
| 157 | |
| 158 | } // box2d |
| 159 | } // physics |
| 160 | } // love |
| 161 | |
| 162 | #endif // LOVE_PHYSICS_BOX2D_JOINT_H |
| 163 | |