Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset velocity and constraint jAcc when body type changes to DYNAMIC #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions include/chipmunk/chipmunk_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,14 @@ typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
typedef void (*cpConstraintResetAccImpl)(cpConstraint *constraint);

typedef struct cpConstraintClass {
cpConstraintPreStepImpl preStep;
cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
cpConstraintApplyImpulseImpl applyImpulse;
cpConstraintGetImpulseImpl getImpulse;
cpConstraintResetAccImpl resetAcc;
} cpConstraintClass;

struct cpConstraint {
Expand Down
17 changes: 17 additions & 0 deletions src/cpBody.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ cpBodySetType(cpBody *body, cpBodyType type)
body->m_inv = body->i_inv = INFINITY;

cpBodyAccumulateMassFromShapes(body);

// Check any constraints, and if there are
// constraints with another non-DYNAMIC body:
// Reset any accumulated force in constraints attached to the body.
// Reset velocity of the body. These will be NaN or Inf otherwise.
cpConstraint* constraint = body->constraintList;
while (constraint) {
constraint->klass->resetAcc(constraint);
cpBody* a = cpConstraintGetBodyA(constraint);
a->v = cpvzero;
a->w = 0.0f;
cpBody* b = cpConstraintGetBodyB(constraint);
b->v = cpvzero;
b->w = 0.0f;

constraint = cpConstraintNext(constraint, body);
}
} else {
body->m = body->i = INFINITY;
body->m_inv = body->i_inv = 0.0f;
Expand Down
7 changes: 7 additions & 0 deletions src/cpDampedRotarySpring.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ getImpulse(cpDampedRotarySpring *spring)
return spring->jAcc;
}

static void
resetAcc(cpDampedRotarySpring *spring)
{
spring->jAcc = 0.0f;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpDampedRotarySpring *
Expand Down
7 changes: 7 additions & 0 deletions src/cpDampedSpring.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,18 @@ getImpulse(cpDampedSpring *spring)
return spring->jAcc;
}

static void
resetAcc(cpDampedSpring *spring)
{
spring->jAcc = 0.0f;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpDampedSpring *
Expand Down
7 changes: 7 additions & 0 deletions src/cpGearJoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ getImpulse(cpGearJoint *joint)
return cpfabs(joint->jAcc);
}

static void
resetAcc(cpGearJoint *joint)
{
joint->jAcc = 0.0f;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpGearJoint *
Expand Down
7 changes: 7 additions & 0 deletions src/cpGrooveJoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,18 @@ getImpulse(cpGrooveJoint *joint)
return cpvlength(joint->jAcc);
}

static void
resetAcc(cpGrooveJoint *joint)
{
joint->jAcc = cpvzero;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpGrooveJoint *
Expand Down
8 changes: 7 additions & 1 deletion src/cpPinJoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,20 @@ getImpulse(cpPinJoint *joint)
return cpfabs(joint->jnAcc);
}

static void
resetAcc(cpPinJoint *joint)
{
joint->jnAcc = 0.0f;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};


cpPinJoint *
cpPinJointAlloc(void)
{
Expand Down
11 changes: 9 additions & 2 deletions src/cpPivotJoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,23 @@ applyImpulse(cpPivotJoint *joint, cpFloat dt)
}

static cpFloat
getImpulse(cpConstraint *joint)
getImpulse(cpPivotJoint *joint)
{
return cpvlength(((cpPivotJoint *)joint)->jAcc);
return cpvlength(joint->jAcc);
}

static void
resetAcc(cpPivotJoint *joint)
{
joint->jAcc = cpvzero;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpPivotJoint *
Expand Down
7 changes: 7 additions & 0 deletions src/cpRatchetJoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,18 @@ getImpulse(cpRatchetJoint *joint)
return cpfabs(joint->jAcc);
}

static void
resetAcc(cpRatchetJoint *joint)
{
joint->jAcc = 0.0f;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpRatchetJoint *
Expand Down
7 changes: 7 additions & 0 deletions src/cpRotaryLimitJoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ getImpulse(cpRotaryLimitJoint *joint)
return cpfabs(joint->jAcc);
}

static void
resetAcc(cpRotaryLimitJoint *joint)
{
joint->jAcc = 0.0f;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpRotaryLimitJoint *
Expand Down
7 changes: 7 additions & 0 deletions src/cpSimpleMotor.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,18 @@ getImpulse(cpSimpleMotor *joint)
return cpfabs(joint->jAcc);
}

static void
resetAcc(cpSimpleMotor *joint)
{
joint->jAcc = 0.0f;
}

static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
(cpConstraintResetAccImpl)resetAcc,
};

cpSimpleMotor *
Expand Down
10 changes: 8 additions & 2 deletions src/cpSlideJoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ applyImpulse(cpSlideJoint *joint, cpFloat dt)
}

static cpFloat
getImpulse(cpConstraint *joint)
getImpulse(cpSlideJoint *joint)
{
return cpfabs(((cpSlideJoint *)joint)->jnAcc);
return cpfabs(joint->jnAcc);
}

static void
resetAcc(cpSlideJoint *joint)
{
joint->jnAcc = 0.0f;
}

static const cpConstraintClass klass = {
Expand Down