Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions demo/ChipmunkDemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ DrawInfo()
cpFloat ke = 0.0f;
for(int i=0; i<bodies->num; i++){
cpBody *body = (cpBody *)bodies->arr[i];
if(body->m == INFINITY || body->i == INFINITY) continue;
if(isinf(body->m) || isinf(body->i)) continue;

ke += body->m*cpvdot(body->v, body->v) + body->i*body->w*body->w;
}
Expand Down Expand Up @@ -475,7 +475,7 @@ Click(const sapp_event *event)
cpPointQueryInfo info = {0};
cpShape *shape = cpSpacePointQueryNearest(space, mouse_pos, radius, GRAB_FILTER, &info);

if(shape && cpBodyGetMass(cpShapeGetBody(shape)) < INFINITY){
if(shape && isfinite(cpBodyGetMass(cpShapeGetBody(shape)))){
// Use the closest point on the surface if the click is outside of the shape.
cpVect nearest = (info.distance > 0.0f ? info.point : mouse_pos);

Expand Down
11 changes: 10 additions & 1 deletion include/chipmunk/chipmunk_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,16 @@ cpConstraintNext(cpConstraint *node, cpBody *body)
static inline cpArbiter *
cpArbiterNext(cpArbiter *node, cpBody *body)
{
return (node->body_a == body ? node->thread_a.next : node->thread_b.next);
// Check both body_a and body_b to handle cases where they may have been swapped
// after the arbiter was threaded into the body's list
if(node->body_a == body){
return node->thread_a.next;
} else if(node->body_b == body){
return node->thread_b.next;
} else {
// This should never happen in normal operation, but return NULL to prevent infinite loops
return NULL;
}
}

#define CP_BODY_FOREACH_ARBITER(bdy, var)\
Expand Down
3 changes: 2 additions & 1 deletion include/chipmunk/cpBB.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
/// Return true if the bounding box intersects the line segment with ends @c a and @c b.
static inline cpBool cpBBIntersectsSegment(cpBB bb, cpVect a, cpVect b)
{
return (cpBBSegmentQuery(bb, a, b) != INFINITY);
cpFloat result = cpBBSegmentQuery(bb, a, b);
return isfinite(result);
}

/// Clamp a vector to a bounding box.
Expand Down
15 changes: 8 additions & 7 deletions src/cpBody.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include <float.h>
#include <math.h>
#include <stdarg.h>

#include "chipmunk/chipmunk_private.h"
Expand Down Expand Up @@ -104,7 +105,7 @@ cpBodyFree(cpBody *body)
#define cpAssertSaneBody(body)
#else
static void cpv_assert_nan(cpVect v, const char *message){cpAssertHard(v.x == v.x && v.y == v.y, message);}
static void cpv_assert_infinite(cpVect v, const char *message){cpAssertHard(cpfabs(v.x) != INFINITY && cpfabs(v.y) != INFINITY, message);}
static void cpv_assert_infinite(cpVect v, const char *message){cpAssertHard(!isinf(cpfabs(v.x)) && !isinf(cpfabs(v.y)), message);}
static void cpv_assert_sane(cpVect v, const char *message){cpv_assert_nan(v, message); cpv_assert_infinite(v, message);}

static void
Expand All @@ -119,9 +120,9 @@ cpBodyFree(cpBody *body)
cpv_assert_sane(body->v, "Body's velocity is invalid.");
cpv_assert_sane(body->f, "Body's force is invalid.");

cpAssertHard(body->a == body->a && cpfabs(body->a) != INFINITY, "Body's angle is invalid.");
cpAssertHard(body->w == body->w && cpfabs(body->w) != INFINITY, "Body's angular velocity is invalid.");
cpAssertHard(body->t == body->t && cpfabs(body->t) != INFINITY, "Body's torque is invalid.");
cpAssertHard(body->a == body->a && !isinf(cpfabs(body->a)), "Body's angle is invalid.");
cpAssertHard(body->w == body->w && !isinf(cpfabs(body->w)), "Body's angular velocity is invalid.");
cpAssertHard(body->t == body->t && !isinf(cpfabs(body->t)), "Body's torque is invalid.");
}

#define cpAssertSaneBody(body) cpBodySanityCheck(body)
Expand All @@ -136,9 +137,9 @@ cpBodyIsSleeping(const cpBody *body)
cpBodyType
cpBodyGetType(cpBody *body)
{
if(body->sleeping.idleTime == INFINITY){
if(isinf(body->sleeping.idleTime)){
return CP_BODY_TYPE_STATIC;
} else if(body->m == INFINITY){
} else if(isinf(body->m)){
return CP_BODY_TYPE_KINEMATIC;
} else {
return CP_BODY_TYPE_DYNAMIC;
Expand Down Expand Up @@ -254,7 +255,7 @@ void
cpBodySetMass(cpBody *body, cpFloat mass)
{
cpAssertHard(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC, "You cannot set the mass of kinematic or static bodies.");
cpAssertHard(0.0f <= mass && mass < INFINITY, "Mass must be positive and finite.");
cpAssertHard(0.0f <= mass && isfinite(mass), "Mass must be positive and finite.");

cpBodyActivate(body);
body->m = mass;
Expand Down
3 changes: 2 additions & 1 deletion src/cpSpaceComponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include <string.h>
#include <math.h>

#include "chipmunk/chipmunk_private.h"

Expand Down Expand Up @@ -220,7 +221,7 @@ ComponentActive(cpBody *root, cpFloat threshold)
void
cpSpaceProcessComponents(cpSpace *space, cpFloat dt)
{
cpBool sleep = (space->sleepTimeThreshold != INFINITY);
cpBool sleep = isfinite(space->sleepTimeThreshold);
cpArray *bodies = space->dynamicBodies;

#ifndef NDEBUG
Expand Down
4 changes: 3 additions & 1 deletion src/cpSpaceStep.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* SOFTWARE.
*/

#include <math.h>

#include "chipmunk/chipmunk_private.h"

//MARK: Post Step Callback Functions
Expand Down Expand Up @@ -269,7 +271,7 @@ cpSpaceCollideShapes(cpShape *a, cpShape *b, cpCollisionID id, cpSpace *space)
!(a->sensor || b->sensor) &&
// Don't process collisions between two infinite mass bodies.
// This includes collisions between two kinematic bodies, or a kinematic body and a static body.
!(a->body->m == INFINITY && b->body->m == INFINITY)
!(isinf(a->body->m) && isinf(b->body->m))
){
cpArrayPush(space->arbiters, arb);
} else {
Expand Down