/* Simple test of cpDampedRotarySpringGetDamping(). */ void test_cpDampedRotarySpringGetDamping(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 13.7, -2.36); CU_ASSERT(cpDampedRotarySpringGetDamping(spring1) == -2.36); cpConstraint *spring2 = cpDampedRotarySpringNew(body1, body2, -11.7, 8, 0); CU_ASSERT(cpDampedRotarySpringGetDamping(spring2) == 0); cpConstraint *spring3 = cpDampedRotarySpringNew(body1, body2, 0, -1.21, 15); CU_ASSERT(cpDampedRotarySpringGetDamping(spring3) == 15); cpConstraint *spring4 = cpDampedRotarySpringNew(body1, body2, 16.38, 0, 6.95); CU_ASSERT(cpDampedRotarySpringGetDamping(spring4) == 6.95); }
/* Simple test of cpDampedRotarySpringGetStiffness(). */ void test_cpDampedRotarySpringGetStiffness(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 13.7, 1); CU_ASSERT(cpDampedRotarySpringGetStiffness(spring1) == 13.7); cpConstraint *spring2 = cpDampedRotarySpringNew(body1, body2, -11.7, 8, 1); CU_ASSERT(cpDampedRotarySpringGetStiffness(spring2) == 8); cpConstraint *spring3 = cpDampedRotarySpringNew(body1, body2, 0, -1.21, 1); CU_ASSERT(cpDampedRotarySpringGetStiffness(spring3) == -1.21); cpConstraint *spring4 = cpDampedRotarySpringNew(body1, body2, 16.38, 0, 1); CU_ASSERT(cpDampedRotarySpringGetStiffness(spring4) == 0); }
/* Simple test of cpDampedRotarySpringGetRestAngle(). */ void test_cpDampedRotarySpringGetRestAngle(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 1, 1); CU_ASSERT(cpDampedRotarySpringGetRestAngle(spring1) == 7); cpConstraint *spring2 = cpDampedRotarySpringNew(body1, body2, -11.7, 1, 1); CU_ASSERT(cpDampedRotarySpringGetRestAngle(spring2) == -11.7); cpConstraint *spring3 = cpDampedRotarySpringNew(body1, body2, 0, 1, 1); CU_ASSERT(cpDampedRotarySpringGetRestAngle(spring3) == 0); cpConstraint *spring4 = cpDampedRotarySpringNew(body1, body2, 16.38, 1, 1); CU_ASSERT(cpDampedRotarySpringGetRestAngle(spring4) ==16.38); }
/* Simple test of cpDampedRotarySpringGetSpringTorqueFunc(). */ void test_cpDampedRotarySpringGetSpringTorqueFunc(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 13.7, -2.36); ((cpDampedRotarySpring*)spring1)->springTorqueFunc = (cpDampedRotarySpringTorqueFunc)testSpringTorque; CU_ASSERT(cpDampedRotarySpringGetSpringTorqueFunc(spring1) == (cpDampedRotarySpringTorqueFunc)testSpringTorque); }
/* Simple test of cpConstraintIsDampedRotarySpring(). */ void test_cpConstraintIsDampedRotarySpring(void) { cpConstraint *isDampedRotarySpring = cpDampedRotarySpringNew(body1, body2, 1, 1,1); cpConstraint *isNotDampedRotarySpring = cpGrooveJointNew(body1, body2, cpv(0, 0), cpv(0, 0), cpv(3, 3)); CU_ASSERT(cpConstraintIsDampedRotarySpring(isDampedRotarySpring)); CU_ASSERT_FALSE(cpConstraintIsDampedRotarySpring(isNotDampedRotarySpring)); }
/* Simple test of cpDampedRotarySpringSetSpringTorqueFunc(). */ void test_cpDampedRotarySpringSetSpringTorqueFunc(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 1, 1); cpDampedRotarySpringSetSpringTorqueFunc(spring1, (cpDampedRotarySpringTorqueFunc)testSpringTorque); CU_ASSERT(((cpDampedRotarySpring*)spring1)->springTorqueFunc == (cpDampedRotarySpringTorqueFunc)testSpringTorque); }
SGPhysicsConstraint* SG_CALL sgPhysicsConstraintCreateRotSpringRads(SGPhysicsBody* body1, SGPhysicsBody* body2, float rads, float st, float damp) { SGPhysicsConstraint* constr = sgPhysicsConstraintCreate(body1, body2, SG_CONSTRAINT_ROTSPRING); if(!constr) return NULL; constr->handle = cpDampedRotarySpringNew(body1->handle, body2->handle, rads, st, damp); _postCreate(constr); return constr; }
WorldConstraint_t *worldConstr_createDampedRotarySpringJoint(WorldEntity_t *a, WorldEntity_t *b, GLMFloat aRestAngle, GLMFloat aStiffness, GLMFloat aDamping) { dynamo_assert(a->world == b->world, "Entities are not in the same world"); WorldConstraint_t *ret = obj_create_autoreleased(&Class_WorldConstraint); ret->world = a->world; ret->a = obj_retain(a); ret->b = obj_retain(b); ret->type = kWorldJointType_DampedRotarySpring; ret->cpConstraint = cpDampedRotarySpringNew(a->cpBody, b->cpBody, aRestAngle, aStiffness, aDamping); cpSpaceAddConstraint(ret->world->cpSpace, ret->cpConstraint); return ret; }
/* Simple test of cpDampedRotarySpringSetStiffness(). */ void test_cpDampedRotarySpringSetStiffness(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 1, 1); cpDampedRotarySpringSetStiffness(spring1, 0.778); cpDampedRotarySpring *springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->stiffness == 0.778); cpDampedRotarySpringSetStiffness(spring1, -14.28); springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->stiffness == -14.28); cpDampedRotarySpringSetStiffness(spring1, 31); springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->stiffness == 31); }
/* Simple test of cpDampedRotarySpringSetRestAngle(). */ void test_cpDampedRotarySpringSetRestAngle(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 1, 1); cpDampedRotarySpringSetRestAngle(spring1, M_PI_2); cpDampedRotarySpring *springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->restAngle == M_PI_2); cpDampedRotarySpringSetRestAngle(spring1, -M_PI); springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->restAngle == -M_PI); cpDampedRotarySpringSetRestAngle(spring1, 8); springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->restAngle == 8); }
/* Simple test of cpDampedRotarySpringSetDamping(). */ void test_cpDampedRotarySpringSetDamping(void) { cpConstraint *spring1 = cpDampedRotarySpringNew(body1, body2, 7, 1, 1); cpDampedRotarySpringSetDamping(spring1, 54.36); cpDampedRotarySpring *springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->damping == 54.36); cpDampedRotarySpringSetDamping(spring1, -89.11); springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->damping == -89.11); cpDampedRotarySpringSetDamping(spring1, 12); springCast = (cpDampedRotarySpring*)spring1; CU_ASSERT(springCast->damping == 12); }
cpConstraint *cpSpaceSerializer::createRotarySpringJoint(TiXmlElement *elm) { cpConstraint *constraint; cpBody *a; cpBody *b; createBodies(elm, &a, &b); cpFloat restAngle = createValue<cpFloat>("restAngle", elm); cpFloat stiffness = createValue<cpFloat>("stiffness", elm); cpFloat damping = createValue<cpFloat>("damping", elm); constraint = cpDampedRotarySpringNew(a, b, restAngle, stiffness, damping); return constraint; }
bool PhysicsJointRotarySpring::createConstraints() { do { auto joint = cpDampedRotarySpringNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::float2cpfloat(_bodyB->getRotation() - _bodyA->getRotation()), PhysicsHelper::float2cpfloat(_stiffness), PhysicsHelper::float2cpfloat(_damping)); CC_BREAK_IF(joint == nullptr); _cpConstraints.push_back(joint); return true; } while (false); return false; }
bool PhysicsJointRotarySpring::init(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping) { do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); auto constraint = cpDampedRotarySpringNew(a->getCPBody(), b->getCPBody(), PhysicsHelper::float2cpfloat(_bodyB->getRotation() - _bodyA->getRotation()), PhysicsHelper::float2cpfloat(stiffness), PhysicsHelper::float2cpfloat(damping)); CC_BREAK_IF(constraint == nullptr); _cpConstraints.push_back(constraint); return true; } while (false); return false; }
static cpSpace * init(void) { space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -100)); cpSpaceSetSleepTimeThreshold(space, 0.5f); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,120), cpv(320,120), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,0), cpv(320,0), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-120), cpv(320,-120), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-240), cpv(-160,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(0,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(160,-240), cpv(160,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpVect boxOffset; cpBody *body1, *body2; cpVect posA = cpv( 50, 60); cpVect posB = cpv(110, 60); #define POS_A cpvadd(boxOffset, posA) #define POS_B cpvadd(boxOffset, posB) // Pin Joints - Link shapes with a solid bar or pin. // Keeps the anchor points the same distance apart from when the joint was created. boxOffset = cpv(-320, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpPinJointNew(body1, body2, cpv(15,0), cpv(-15,0))); // Slide Joints - Like pin joints but with a min/max distance. // Can be used for a cheap approximation of a rope. boxOffset = cpv(-160, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpSlideJointNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 40.0f)); // Pivot Joints - Holds the two anchor points together. Like a swivel. boxOffset = cpv(0, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpPivotJointNew(body1, body2, cpvadd(boxOffset, cpv(80,60)))); // cpPivotJointNew() takes it's anchor parameter in world coordinates. The anchors are calculated from that // cpPivotJointNew2() lets you specify the two anchor points explicitly // Groove Joints - Like a pivot joint, but one of the anchors is a line segment that the pivot can slide in boxOffset = cpv(160, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpGrooveJointNew(body1, body2, cpv(30,30), cpv(30,-30), cpv(-30,0))); // Damped Springs boxOffset = cpv(-320, -120); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpDampedSpringNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 5.0f, 0.3f)); // Damped Rotary Springs boxOffset = cpv(-160, -120); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); cpSpaceAddConstraint(space, cpDampedRotarySpringNew(body1, body2, 0.0f, 3000.0f, 60.0f)); // Rotary Limit Joint boxOffset = cpv(0, -120); body1 = addLever(posA, boxOffset); body2 = addLever(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Hold their rotation within 90 degrees of each other. cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body1, body2, -M_PI_2, M_PI_2)); // Ratchet Joint - A rotary ratchet, like a socket wrench boxOffset = cpv(160, -120); body1 = addLever(posA, boxOffset); body2 = addLever(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Ratchet every 90 degrees cpSpaceAddConstraint(space, cpRatchetJointNew(body1, body2, 0.0f, M_PI_2)); // Gear Joint - Maintain a specific angular velocity ratio boxOffset = cpv(-320, 0); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Force one to sping 2x as fast as the other cpSpaceAddConstraint(space, cpGearJointNew(body1, body2, 0.0f, 2.0f)); // Simple Motor - Maintain a specific angular relative velocity boxOffset = cpv(-160, 0); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Make them spin at 1/2 revolution per second in relation to each other. cpSpaceAddConstraint(space, cpSimpleMotorNew(body1, body2, M_PI)); // Make a car with some nice soft suspension boxOffset = cpv(0, 0); cpBody *wheel1 = addWheel(posA, boxOffset); cpBody *wheel2 = addWheel(posB, boxOffset); cpBody *chassis = addChassis(cpv(80, 100), boxOffset); cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel1, cpv(-30, -10), cpv(-30, -40), cpvzero)); cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel2, cpv( 30, -10), cpv( 30, -40), cpvzero)); cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel1, cpv(-30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel2, cpv( 30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); return space; }