1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining a copy 4 * of this software and associated documentation files (the "Software"), to deal 5 * in the Software without restriction, including without limitation the rights 6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 * copies of the Software, and to permit persons to whom the Software is 8 * furnished to do so, subject to the following conditions: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 * SOFTWARE. 20 */ 21 22 /// @defgroup cpBody cpBody 23 /// Chipmunk's rigid body type. Rigid bodies hold the physical properties of an object like 24 /// it's mass, and position and velocity of it's center of gravity. They don't have an shape on their own. 25 /// They are given a shape by creating collision shapes (cpShape) that point to the body. 26 /// @{ 27 28 typedef enum cpBodyType { 29 /// A dynamic body is one that is affected by gravity, forces, and collisions. 30 /// This is the default body type. 31 CP_BODY_TYPE_DYNAMIC, 32 /// A kinematic body is an infinite mass, user controlled body that is not affected by gravity, forces or collisions. 33 /// Instead the body only moves based on it's velocity. 34 /// Dynamic bodies collide normally with kinematic bodies, though the kinematic body will be unaffected. 35 /// Collisions between two kinematic bodies, or a kinematic body and a static body produce collision callbacks, but no collision response. 36 CP_BODY_TYPE_KINEMATIC, 37 /// A static body is a body that never (or rarely) moves. If you move a static body, you must call one of the cpSpaceReindex*() functions. 38 /// Chipmunk uses this information to optimize the collision detection. 39 /// Static bodies do not produce collision callbacks when colliding with other static bodies. 40 CP_BODY_TYPE_STATIC, 41 } cpBodyType; 42 43 /// Rigid body velocity update function type. 44 typedef void (*cpBodyVelocityFunc)(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt); 45 /// Rigid body position update function type. 46 typedef void (*cpBodyPositionFunc)(cpBody *body, cpFloat dt); 47 48 /// Allocate a cpBody. 49 CP_EXPORT cpBody* cpBodyAlloc(void); 50 /// Initialize a cpBody. 51 CP_EXPORT cpBody* cpBodyInit(cpBody *body, cpFloat mass, cpFloat moment); 52 /// Allocate and initialize a cpBody. 53 CP_EXPORT cpBody* cpBodyNew(cpFloat mass, cpFloat moment); 54 55 /// Allocate and initialize a cpBody, and set it as a kinematic body. 56 CP_EXPORT cpBody* cpBodyNewKinematic(void); 57 /// Allocate and initialize a cpBody, and set it as a static body. 58 CP_EXPORT cpBody* cpBodyNewStatic(void); 59 60 /// Destroy a cpBody. 61 CP_EXPORT void cpBodyDestroy(cpBody *body); 62 /// Destroy and free a cpBody. 63 CP_EXPORT void cpBodyFree(cpBody *body); 64 65 // Defined in cpSpace.c 66 /// Wake up a sleeping or idle body. 67 CP_EXPORT void cpBodyActivate(cpBody *body); 68 /// Wake up any sleeping or idle bodies touching a static body. 69 CP_EXPORT void cpBodyActivateStatic(cpBody *body, cpShape *filter); 70 71 /// Force a body to fall asleep immediately. 72 CP_EXPORT void cpBodySleep(cpBody *body); 73 /// Force a body to fall asleep immediately along with other bodies in a group. 74 CP_EXPORT void cpBodySleepWithGroup(cpBody *body, cpBody *group); 75 76 /// Returns true if the body is sleeping. 77 CP_EXPORT cpBool cpBodyIsSleeping(const cpBody *body); 78 79 /// Get the type of the body. 80 CP_EXPORT cpBodyType cpBodyGetType(cpBody *body); 81 /// Set the type of the body. 82 CP_EXPORT void cpBodySetType(cpBody *body, cpBodyType type); 83 84 /// Get the space this body is added to. 85 CP_EXPORT cpSpace* cpBodyGetSpace(const cpBody *body); 86 87 /// Get the mass of the body. 88 CP_EXPORT cpFloat cpBodyGetMass(const cpBody *body); 89 /// Set the mass of the body. 90 CP_EXPORT void cpBodySetMass(cpBody *body, cpFloat m); 91 92 /// Get the moment of inertia of the body. 93 CP_EXPORT cpFloat cpBodyGetMoment(const cpBody *body); 94 /// Set the moment of inertia of the body. 95 CP_EXPORT void cpBodySetMoment(cpBody *body, cpFloat i); 96 97 /// Set the position of a body. 98 CP_EXPORT cpVect cpBodyGetPosition(const cpBody *body); 99 /// Set the position of the body. 100 CP_EXPORT void cpBodySetPosition(cpBody *body, cpVect pos); 101 102 /// Get the offset of the center of gravity in body local coordinates. 103 CP_EXPORT cpVect cpBodyGetCenterOfGravity(const cpBody *body); 104 /// Set the offset of the center of gravity in body local coordinates. 105 CP_EXPORT void cpBodySetCenterOfGravity(cpBody *body, cpVect cog); 106 107 /// Get the velocity of the body. 108 CP_EXPORT cpVect cpBodyGetVelocity(const cpBody *body); 109 /// Set the velocity of the body. 110 CP_EXPORT void cpBodySetVelocity(cpBody *body, cpVect velocity); 111 112 /// Get the force applied to the body for the next time step. 113 CP_EXPORT cpVect cpBodyGetForce(const cpBody *body); 114 /// Set the force applied to the body for the next time step. 115 CP_EXPORT void cpBodySetForce(cpBody *body, cpVect force); 116 117 /// Get the angle of the body. 118 CP_EXPORT cpFloat cpBodyGetAngle(const cpBody *body); 119 /// Set the angle of a body. 120 CP_EXPORT void cpBodySetAngle(cpBody *body, cpFloat a); 121 122 /// Get the angular velocity of the body. 123 CP_EXPORT cpFloat cpBodyGetAngularVelocity(const cpBody *body); 124 /// Set the angular velocity of the body. 125 CP_EXPORT void cpBodySetAngularVelocity(cpBody *body, cpFloat angularVelocity); 126 127 /// Get the torque applied to the body for the next time step. 128 CP_EXPORT cpFloat cpBodyGetTorque(const cpBody *body); 129 /// Set the torque applied to the body for the next time step. 130 CP_EXPORT void cpBodySetTorque(cpBody *body, cpFloat torque); 131 132 /// Get the rotation vector of the body. (The x basis vector of it's transform.) 133 CP_EXPORT cpVect cpBodyGetRotation(const cpBody *body); 134 135 /// Get the user data pointer assigned to the body. 136 CP_EXPORT cpDataPointer cpBodyGetUserData(const cpBody *body); 137 /// Set the user data pointer assigned to the body. 138 CP_EXPORT void cpBodySetUserData(cpBody *body, cpDataPointer userData); 139 140 /// Set the callback used to update a body's velocity. 141 CP_EXPORT void cpBodySetVelocityUpdateFunc(cpBody *body, cpBodyVelocityFunc velocityFunc); 142 /// Set the callback used to update a body's position. 143 /// NOTE: It's not generally recommended to override this unless you call the default position update function. 144 CP_EXPORT void cpBodySetPositionUpdateFunc(cpBody *body, cpBodyPositionFunc positionFunc); 145 146 /// Default velocity integration function.. 147 CP_EXPORT void cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt); 148 /// Default position integration function. 149 CP_EXPORT void cpBodyUpdatePosition(cpBody *body, cpFloat dt); 150 151 /// Convert body relative/local coordinates to absolute/world coordinates. 152 CP_EXPORT cpVect cpBodyLocalToWorld(const cpBody *body, const cpVect point); 153 /// Convert body absolute/world coordinates to relative/local coordinates. 154 CP_EXPORT cpVect cpBodyWorldToLocal(const cpBody *body, const cpVect point); 155 156 /// Apply a force to a body. Both the force and point are expressed in world coordinates. 157 CP_EXPORT void cpBodyApplyForceAtWorldPoint(cpBody *body, cpVect force, cpVect point); 158 /// Apply a force to a body. Both the force and point are expressed in body local coordinates. 159 CP_EXPORT void cpBodyApplyForceAtLocalPoint(cpBody *body, cpVect force, cpVect point); 160 161 /// Apply an impulse to a body. Both the impulse and point are expressed in world coordinates. 162 CP_EXPORT void cpBodyApplyImpulseAtWorldPoint(cpBody *body, cpVect impulse, cpVect point); 163 /// Apply an impulse to a body. Both the impulse and point are expressed in body local coordinates. 164 CP_EXPORT void cpBodyApplyImpulseAtLocalPoint(cpBody *body, cpVect impulse, cpVect point); 165 166 /// Get the velocity on a body (in world units) at a point on the body in world coordinates. 167 CP_EXPORT cpVect cpBodyGetVelocityAtWorldPoint(const cpBody *body, cpVect point); 168 /// Get the velocity on a body (in world units) at a point on the body in local coordinates. 169 CP_EXPORT cpVect cpBodyGetVelocityAtLocalPoint(const cpBody *body, cpVect point); 170 171 /// Get the amount of kinetic energy contained by the body. 172 CP_EXPORT cpFloat cpBodyKineticEnergy(const cpBody *body); 173 174 /// Body/shape iterator callback function type. 175 typedef void (*cpBodyShapeIteratorFunc)(cpBody *body, cpShape *shape, void *data); 176 /// Call @c func once for each shape attached to @c body and added to the space. 177 CP_EXPORT void cpBodyEachShape(cpBody *body, cpBodyShapeIteratorFunc func, void *data); 178 179 /// Body/constraint iterator callback function type. 180 typedef void (*cpBodyConstraintIteratorFunc)(cpBody *body, cpConstraint *constraint, void *data); 181 /// Call @c func once for each constraint attached to @c body and added to the space. 182 CP_EXPORT void cpBodyEachConstraint(cpBody *body, cpBodyConstraintIteratorFunc func, void *data); 183 184 /// Body/arbiter iterator callback function type. 185 typedef void (*cpBodyArbiterIteratorFunc)(cpBody *body, cpArbiter *arbiter, void *data); 186 /// Call @c func once for each arbiter that is currently active on the body. 187 CP_EXPORT void cpBodyEachArbiter(cpBody *body, cpBodyArbiterIteratorFunc func, void *data); 188 189 ///@} 190