2010-09-04 18:18:14 +08:00
|
|
|
/* Copyright (c) 2007 Scott Lembcke
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <float.h>
|
2012-03-21 14:53:57 +08:00
|
|
|
#include <math.h>
|
2010-09-04 18:18:14 +08:00
|
|
|
|
2011-05-21 14:58:22 +08:00
|
|
|
#include "chipmunk_private.h"
|
2011-07-11 10:34:03 +08:00
|
|
|
#include "constraints/util.h"
|
2011-05-21 14:58:22 +08:00
|
|
|
|
|
|
|
// initialized in cpInitChipmunk()
|
|
|
|
cpBody cpStaticBodySingleton;
|
2010-09-04 18:18:14 +08:00
|
|
|
|
|
|
|
cpBody*
|
|
|
|
cpBodyAlloc(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return (cpBody *)cpcalloc(1, sizeof(cpBody));
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
2011-07-11 10:34:03 +08:00
|
|
|
cpBody *
|
2010-09-04 18:18:14 +08:00
|
|
|
cpBodyInit(cpBody *body, cpFloat m, cpFloat i)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
body->space = NULL;
|
|
|
|
body->shapeList = NULL;
|
|
|
|
body->arbiterList = NULL;
|
|
|
|
body->constraintList = NULL;
|
|
|
|
|
|
|
|
body->velocity_func = cpBodyUpdateVelocity;
|
|
|
|
body->position_func = cpBodyUpdatePosition;
|
|
|
|
|
|
|
|
cpComponentNode node = {NULL, NULL, 0.0f};
|
|
|
|
body->node = node;
|
|
|
|
|
|
|
|
body->p = cpvzero;
|
|
|
|
body->v = cpvzero;
|
|
|
|
body->f = cpvzero;
|
|
|
|
|
|
|
|
body->w = 0.0f;
|
|
|
|
body->t = 0.0f;
|
|
|
|
|
|
|
|
body->v_bias = cpvzero;
|
|
|
|
body->w_bias = 0.0f;
|
|
|
|
|
|
|
|
body->v_limit = (cpFloat)INFINITY;
|
|
|
|
body->w_limit = (cpFloat)INFINITY;
|
|
|
|
|
|
|
|
body->data = NULL;
|
|
|
|
|
|
|
|
// Setters must be called after full initialization so the sanity checks don't assert on garbage data.
|
|
|
|
cpBodySetMass(body, m);
|
|
|
|
cpBodySetMoment(body, i);
|
|
|
|
cpBodySetAngle(body, 0.0f);
|
|
|
|
|
|
|
|
return body;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpBody*
|
|
|
|
cpBodyNew(cpFloat m, cpFloat i)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return cpBodyInit(cpBodyAlloc(), m, i);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
2011-05-21 14:58:22 +08:00
|
|
|
cpBody *
|
|
|
|
cpBodyInitStatic(cpBody *body)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpBodyInit(body, (cpFloat)INFINITY, (cpFloat)INFINITY);
|
|
|
|
body->node.idleTime = (cpFloat)INFINITY;
|
|
|
|
|
|
|
|
return body;
|
2011-05-21 14:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpBody *
|
2012-03-21 14:53:57 +08:00
|
|
|
cpBodyNewStatic(void)
|
2011-05-21 14:58:22 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return cpBodyInitStatic(cpBodyAlloc());
|
2011-05-21 14:58:22 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 18:18:14 +08:00
|
|
|
void cpBodyDestroy(cpBody *body){}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodyFree(cpBody *body)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if(body){
|
|
|
|
cpBodyDestroy(body);
|
|
|
|
cpfree(body);
|
|
|
|
}
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 14:53:57 +08:00
|
|
|
static void cpv_assert_nan(cpVect v, char *message){cpAssertSoft(v.x == v.x && v.y == v.y, message);}
|
|
|
|
static void cpv_assert_infinite(cpVect v, char *message){cpAssertSoft(cpfabs(v.x) != INFINITY && cpfabs(v.y) != INFINITY, message);}
|
|
|
|
static void cpv_assert_sane(cpVect v, char *message){cpv_assert_nan(v, message); cpv_assert_infinite(v, message);}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodySanityCheck(cpBody *body)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpAssertSoft(body->m == body->m && body->m_inv == body->m_inv, "Body's mass is invalid.");
|
|
|
|
cpAssertSoft(body->i == body->i && body->i_inv == body->i_inv, "Body's moment is invalid.");
|
|
|
|
|
|
|
|
cpv_assert_sane(body->p, "Body's position is invalid.");
|
|
|
|
cpv_assert_sane(body->v, "Body's velocity is invalid.");
|
|
|
|
cpv_assert_sane(body->f, "Body's force is invalid.");
|
|
|
|
|
|
|
|
cpAssertSoft(body->a == body->a && cpfabs(body->a) != INFINITY, "Body's angle is invalid.");
|
|
|
|
cpAssertSoft(body->w == body->w && cpfabs(body->w) != INFINITY, "Body's angular velocity is invalid.");
|
|
|
|
cpAssertSoft(body->t == body->t && cpfabs(body->t) != INFINITY, "Body's torque is invalid.");
|
|
|
|
|
|
|
|
cpv_assert_sane(body->rot, "Internal error: Body's rotation vector is invalid.");
|
|
|
|
|
|
|
|
cpAssertSoft(body->v_limit == body->v_limit, "Body's velocity limit is invalid.");
|
|
|
|
cpAssertSoft(body->w_limit == body->w_limit, "Body's angular velocity limit is invalid.");
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-09-04 18:18:14 +08:00
|
|
|
void
|
|
|
|
cpBodySetMass(cpBody *body, cpFloat mass)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpAssertHard(mass > 0.0f, "Mass must be positive and non-zero.");
|
|
|
|
|
|
|
|
cpBodyActivate(body);
|
|
|
|
body->m = mass;
|
|
|
|
body->m_inv = 1.0f/mass;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodySetMoment(cpBody *body, cpFloat moment)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpAssertHard(moment > 0.0f, "Moment of Inertia must be positive and non-zero.");
|
|
|
|
|
|
|
|
cpBodyActivate(body);
|
|
|
|
body->i = moment;
|
|
|
|
body->i_inv = 1.0f/moment;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-21 14:53:57 +08:00
|
|
|
cpBodyAddShape(cpBody *body, cpShape *shape)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpShape *next = body->shapeList;
|
|
|
|
if(next) next->prev = shape;
|
|
|
|
|
|
|
|
shape->next = next;
|
|
|
|
body->shapeList = shape;
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodyRemoveShape(cpBody *body, cpShape *shape)
|
|
|
|
{
|
|
|
|
cpShape *prev = shape->prev;
|
|
|
|
cpShape *next = shape->next;
|
|
|
|
|
|
|
|
if(prev){
|
2012-04-19 14:35:52 +08:00
|
|
|
prev->next = next;
|
2012-03-21 14:53:57 +08:00
|
|
|
} else {
|
2012-04-19 14:35:52 +08:00
|
|
|
body->shapeList = next;
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(next){
|
2012-04-19 14:35:52 +08:00
|
|
|
next->prev = prev;
|
|
|
|
}
|
2012-03-21 14:53:57 +08:00
|
|
|
|
|
|
|
shape->prev = NULL;
|
|
|
|
shape->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cpConstraint *
|
|
|
|
filterConstraints(cpConstraint *node, cpBody *body, cpConstraint *filter)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if(node == filter){
|
|
|
|
return cpConstraintNext(node, body);
|
|
|
|
} else if(node->a == body){
|
|
|
|
node->next_a = filterConstraints(node->next_a, body, filter);
|
|
|
|
} else {
|
|
|
|
node->next_b = filterConstraints(node->next_b, body, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodyRemoveConstraint(cpBody *body, cpConstraint *constraint)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
body->constraintList = filterConstraints(body->constraintList, body, constraint);
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodySetPos(cpBody *body, cpVect pos)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpBodyActivate(body);
|
|
|
|
cpBodyAssertSane(body);
|
|
|
|
body->p = pos;
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
setAngle(cpBody *body, cpFloat angle)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
body->a = angle;//fmod(a, (cpFloat)M_PI*2.0f);
|
|
|
|
body->rot = cpvforangle(angle);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-21 14:53:57 +08:00
|
|
|
cpBodySetAngle(cpBody *body, cpFloat angle)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpBodyActivate(body);
|
|
|
|
cpBodyAssertSane(body);
|
|
|
|
setAngle(body, angle);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
body->v = cpvclamp(cpvadd(cpvmult(body->v, damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), dt)), body->v_limit);
|
|
|
|
|
|
|
|
cpFloat w_limit = body->w_limit;
|
|
|
|
body->w = cpfclamp(body->w*damping + body->t*body->i_inv*dt, -w_limit, w_limit);
|
|
|
|
|
|
|
|
cpBodySanityCheck(body);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodyUpdatePosition(cpBody *body, cpFloat dt)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
body->p = cpvadd(body->p, cpvmult(cpvadd(body->v, body->v_bias), dt));
|
|
|
|
setAngle(body, body->a + (body->w + body->w_bias)*dt);
|
|
|
|
|
|
|
|
body->v_bias = cpvzero;
|
|
|
|
body->w_bias = 0.0f;
|
|
|
|
|
|
|
|
cpBodySanityCheck(body);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodyResetForces(cpBody *body)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpBodyActivate(body);
|
|
|
|
body->f = cpvzero;
|
|
|
|
body->t = 0.0f;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-21 14:53:57 +08:00
|
|
|
cpBodyApplyForce(cpBody *body, cpVect force, cpVect r)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpBodyActivate(body);
|
|
|
|
body->f = cpvadd(body->f, force);
|
|
|
|
body->t += cpvcross(r, force);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
2011-07-11 10:34:03 +08:00
|
|
|
void
|
|
|
|
cpBodyApplyImpulse(cpBody *body, const cpVect j, const cpVect r)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpBodyActivate(body);
|
|
|
|
apply_impulse(body, j, r);
|
2011-07-11 10:34:03 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 14:53:57 +08:00
|
|
|
static inline cpVect
|
|
|
|
cpBodyGetVelAtPoint(cpBody *body, cpVect r)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return cpvadd(body->v, cpvmult(cpvperp(r), body->w));
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpVect
|
|
|
|
cpBodyGetVelAtWorldPoint(cpBody *body, cpVect point)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return cpBodyGetVelAtPoint(body, cpvsub(point, body->p));
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpVect
|
|
|
|
cpBodyGetVelAtLocalPoint(cpBody *body, cpVect point)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return cpBodyGetVelAtPoint(body, cpvrotate(point, body->rot));
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
2011-07-11 10:34:03 +08:00
|
|
|
|
2010-09-04 18:18:14 +08:00
|
|
|
void
|
2012-03-21 14:53:57 +08:00
|
|
|
cpBodyEachShape(cpBody *body, cpBodyShapeIteratorFunc func, void *data)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpShape *shape = body->shapeList;
|
|
|
|
while(shape){
|
|
|
|
cpShape *next = shape->next;
|
|
|
|
func(body, shape, data);
|
|
|
|
shape = next;
|
|
|
|
}
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
2010-09-04 18:18:14 +08:00
|
|
|
|
2012-03-21 14:53:57 +08:00
|
|
|
void
|
|
|
|
cpBodyEachConstraint(cpBody *body, cpBodyConstraintIteratorFunc func, void *data)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpConstraint *constraint = body->constraintList;
|
|
|
|
while(constraint){
|
|
|
|
cpConstraint *next = cpConstraintNext(constraint, body);
|
|
|
|
func(body, constraint, data);
|
|
|
|
constraint = next;
|
|
|
|
}
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpBodyEachArbiter(cpBody *body, cpBodyArbiterIteratorFunc func, void *data)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpArbiter *arb = body->arbiterList;
|
|
|
|
while(arb){
|
|
|
|
cpArbiter *next = cpArbiterNext(arb, body);
|
|
|
|
|
|
|
|
arb->swappedColl = (body == arb->body_b);
|
|
|
|
func(body, arb, data);
|
|
|
|
|
|
|
|
arb = next;
|
|
|
|
}
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|