static cpSpace * init(void) { staticBody = cpBodyNew(INFINITY, INFINITY); cpResetShapeIdCounter(); space = cpSpaceNew(); cpSpaceResizeActiveHash(space, 30.0f, 1000); space->iterations = 10; cpShape *shape; // Create segments around the edge of the screen. shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; for(int i=0; i<50; i++){ cpBody *body = add_box(10.0, 1.0); cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, body, cpvzero, cpvzero)); pivot->biasCoef = 0.0f; // disable joint correction pivot->maxForce = 1000.0f; // emulate linear friction cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(staticBody, body, 0.0f, 1.0f)); gear->biasCoef = 0.0f; // disable joint correction gear->maxForce = 5000.0f; // emulate angular friction } // We joint the tank to the control body and control the tank indirectly by modifying the control body. tankControlBody = cpBodyNew(INFINITY, INFINITY); tankBody = add_box(15.0, 10.0); cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero)); pivot->biasCoef = 0.0f; // disable joint correction pivot->maxForce = 10000.0f; // emulate linear friction cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(tankControlBody, tankBody, 0.0f, 1.0f)); gear->biasCoef = 1.0f; // limit angular correction rate gear->maxBias = 1.0f; // limit angular correction rate gear->maxForce = 500000.0f; // emulate angular friction return space; }
/* Simple test of cpGearJointGetPhase(). */ void test_cpGearJointGetPhase(void) { cpConstraint *gear1 = cpGearJointNew(body1, body2, 4, 7); CU_ASSERT(cpGearJointGetPhase(gear1)==4); cpConstraint *gear2 = cpGearJointNew(body1, body2, -12.7, 7); CU_ASSERT(cpGearJointGetPhase(gear2) == -12.7); cpConstraint *gear3 = cpGearJointNew(body1, body2, 0, 7); CU_ASSERT(cpGearJointGetPhase(gear3) == 0); cpConstraint *gear4 = cpGearJointNew(body1, body2, 98.5, 7); CU_ASSERT(cpGearJointGetPhase(gear4) == 98.5); }
/* Simple test of cpGearJointGetRatio(). */ void test_cpGearJointGetRatio(void) { cpConstraint *gear1 = cpGearJointNew(body1, body2, 4, -2); CU_ASSERT(cpGearJointGetRatio(gear1) == -2); cpConstraint *gear2 = cpGearJointNew(body1, body2, -12.7, 17.59); CU_ASSERT(cpGearJointGetRatio(gear2) == 17.59); cpConstraint *gear3 = cpGearJointNew(body1, body2, 0, -36.8); CU_ASSERT(cpGearJointGetRatio(gear3) == -36.8); cpConstraint *gear4 = cpGearJointNew(body1, body2, 98.5, 0); CU_ASSERT(cpGearJointGetRatio(gear4) == 0); }
bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr) { do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); getBodyNode(a)->setPosition(anchr); getBodyNode(b)->setPosition(anchr); // add a pivot joint to fixed two body together cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), PhysicsHelper::point2cpv(anchr)); CC_BREAK_IF(joint == nullptr); m_pInfo->add(joint); // add a gear joint to make two body have the same rotation. joint = cpGearJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), 0, 1); CC_BREAK_IF(joint == nullptr); m_pInfo->add(joint); setCollisionEnable(false); return true; } while (false); return false; }
bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr) { do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); getBodyNode(a)->setPosition(anchr); getBodyNode(b)->setPosition(anchr); // add a pivot joint to fixed two body together auto constraint = cpPivotJointNew(a->getCPBody(), b->getCPBody(), PhysicsHelper::point2cpv(anchr)); CC_BREAK_IF(constraint == nullptr); _cpConstraints.push_back(constraint); // add a gear joint to make two body have the same rotation. constraint = cpGearJointNew(a->getCPBody(), b->getCPBody(), 0, 1); CC_BREAK_IF(constraint == nullptr); _cpConstraints.push_back(constraint); setCollisionEnable(false); return true; } while (false); return false; }
bool PhysicsJointFixed::createConstraints() { do { _bodyA->getNode()->setPosition(_anchr); _bodyB->getNode()->setPosition(_anchr); // add a pivot joint to fixed two body together auto joint = cpPivotJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::point2cpv(_anchr)); CC_BREAK_IF(joint == nullptr); _cpConstraints.push_back(joint); // add a gear joint to make two body have the same rotation. joint = cpGearJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), 0, 1); CC_BREAK_IF(joint == nullptr); _cpConstraints.push_back(joint); _collisionEnable = false; return true; } while (false); return false; }
void CDynamics2DVelocityControl::AttachTo(cpBody* pt_body) { /* If we are already controlling a body, detach from it first */ if(m_ptControlledBody != NULL) { Detach(); } /* Set the wanted body as the new controlled one */ m_ptControlledBody = pt_body; /* Add linear constraint */ m_ptLinearConstraint = cpSpaceAddConstraint(m_cDyn2DEngine.GetPhysicsSpace(), cpPivotJointNew2(m_ptControlledBody, m_ptControlBody, cpvzero, cpvzero)); m_ptLinearConstraint->maxBias = 0.0f; /* disable joint correction */ m_ptLinearConstraint->maxForce = m_fMaxForce; /* limit the dragging force */ /* Add angular constraint */ m_ptAngularConstraint = cpSpaceAddConstraint(m_cDyn2DEngine.GetPhysicsSpace(), cpGearJointNew(m_ptControlledBody, m_ptControlBody, 0.0f, 1.0f)); m_ptAngularConstraint->maxBias = 0.0f; /* disable joint correction */ m_ptAngularConstraint->maxForce = m_fMaxTorque; /* limit the torque */ }
/* Simple test of cpConstraintIsGearJoint(). */ void test_cpConstraintIsGearJoint(void) { cpConstraint *isGear = cpGearJointNew(body1, body2, 1, 1); cpConstraint *isNotGear = cpGrooveJointNew(body1, body2, cpv(0, 0), cpv(0, 0), cpv(3, 3)); CU_ASSERT(cpConstraintIsGearJoint(isGear)); CU_ASSERT_FALSE(cpConstraintIsGearJoint(isNotGear)); }
Flatland::GearJoint::GearJoint( Body & body1, Body & body2, double ratio, double phase ) { _constraint = cpGearJointNew( body1, body2, phase, ratio ); }
SGPhysicsConstraint* SG_CALL sgPhysicsConstraintCreateGear(SGPhysicsBody* body1, SGPhysicsBody* body2, float phase, float ratio) { SGPhysicsConstraint* constr = sgPhysicsConstraintCreate(body1, body2, SG_CONSTRAINT_GEAR); if(!constr) return NULL; constr->handle = cpGearJointNew(body1->handle, body2->handle, phase, ratio); _postCreate(constr); return constr; }
void CDynamics2DStretchableObjectModel::SetAngularFriction(Real f_max_bias, Real f_max_force) { m_ptAngularFriction = cpSpaceAddConstraint(GetDynamics2DEngine().GetPhysicsSpace(), cpGearJointNew(GetDynamics2DEngine().GetGroundBody(), GetBody(), 0.0f, 1.0f)); m_ptAngularFriction->maxBias = f_max_bias; m_ptAngularFriction->maxForce = f_max_force; // emulate angular friction }
WorldConstraint_t *worldConstr_createGearJoint(WorldEntity_t *a, WorldEntity_t *b, GLMFloat aPhase, GLMFloat aRatio) { 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_Gear; ret->cpConstraint = cpGearJointNew(a->cpBody, b->cpBody, aPhase, aRatio); cpSpaceAddConstraint(ret->world->cpSpace, ret->cpConstraint); return ret; }
/* Simple test of cpGearJointSetRatio(). */ void test_cpGearJointSetRatio(void) { cpConstraint *gear1 = cpGearJointNew(body1, body2, 4, 7); cpGearJointSetRatio(gear1, 63.25); cpGearJoint *gearCast = (cpGearJoint*)gear1; CU_ASSERT(gearCast->ratio == 63.25); cpGearJointSetRatio(gear1, -7.14); gearCast = (cpGearJoint*)gear1; CU_ASSERT(gearCast->ratio == -7.14); cpGearJointSetRatio(gear1, 92); gearCast = (cpGearJoint*)gear1; CU_ASSERT(gearCast->ratio == 92); }
/* Simple test of cpGearJointSetPhase(). */ void test_cpGearJointSetPhase(void) { cpConstraint *gear1 = cpGearJointNew(body1, body2, 4, 7); cpGearJointSetPhase(gear1, 5.87); cpGearJoint *gearCast = (cpGearJoint*)gear1; CU_ASSERT(gearCast->phase == 5.87); cpGearJointSetPhase(gear1, -18.3); gearCast = (cpGearJoint*)gear1; CU_ASSERT(gearCast->phase == -18.3); cpGearJointSetPhase(gear1, 19); gearCast = (cpGearJoint*)gear1; CU_ASSERT(gearCast->phase == 19); }
bool PhysicsJointGear::createConstraints() { do { auto joint = cpGearJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::float2cpfloat(_phase), PhysicsHelper::float2cpfloat(_ratio)); CC_BREAK_IF(joint == nullptr); _cpConstraints.push_back(joint); return true; } while (false); return false; }
cpConstraint *cpSpaceSerializer::createGearJoint(TiXmlElement *elm) { cpConstraint *constraint; cpBody *a; cpBody *b; createBodies(elm, &a, &b); cpFloat phase = createValue<cpFloat>("phase", elm); cpFloat ratio = createValue<cpFloat>("ratio", elm); constraint = cpGearJointNew(a, b, phase, ratio); //((cpGearJoint*)constraint)->jAcc = createValue<cpFloat>("jAcc", elm); return constraint; }
static void make_leg(cpSpace *space, cpFloat side, cpFloat offset, cpBody *chassis, cpBody *crank, cpVect anchor) { cpVect a, b; cpShape *shape; cpFloat leg_mass = 1.0f; // make leg a = cpvzero, b = cpv(0.0f, side); cpBody *upper_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f))); cpBodySetPosition(upper_leg, cpv(offset, 0.0f)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(upper_leg, a, b, seg_radius)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); cpSpaceAddConstraint(space, cpPivotJointNew2(chassis, upper_leg, cpv(offset, 0.0f), cpvzero)); // lower leg a = cpvzero, b = cpv(0.0f, -1.0f*side); cpBody *lower_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f))); cpBodySetPosition(lower_leg, cpv(offset, -side)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(lower_leg, a, b, seg_radius)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); shape = cpSpaceAddShape(space, cpCircleShapeNew(lower_leg, seg_radius*2.0f, b)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 1.0f); cpSpaceAddConstraint(space, cpPinJointNew(chassis, lower_leg, cpv(offset, 0.0f), cpvzero)); cpSpaceAddConstraint(space, cpGearJointNew(upper_leg, lower_leg, 0.0f, 1.0f)); cpConstraint *constraint; cpFloat diag = cpfsqrt(side*side + offset*offset); constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, upper_leg, anchor, cpv(0.0f, side))); cpPinJointSetDist(constraint, diag); constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, lower_leg, anchor, cpvzero)); cpPinJointSetDist(constraint, diag); }
bool PhysicsJointGear::init(PhysicsBody* a, PhysicsBody* b, float phase, float ratio) { do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); auto constraint = cpGearJointNew(a->getCPBody(), b->getCPBody(), PhysicsHelper::float2cpfloat(phase), PhysicsHelper::float2cpfloat(ratio)); CC_BREAK_IF(constraint == nullptr); _cpConstraints.push_back(constraint); return true; } while (false); return false; }
void GeometrySpringDynamic::initSpring() { GeometrySpringGetAngles getAngles( this ); // int linkFromAbsoluteAngle = getAngles.getLinkFromAngle(); // int linkToAbsoluteAngle = getAngles.getLinkToAngle(); // float angle = M_PI / 180.0 * angleInt; const GeometryLinkDynamic * linkFrom = getDynamicLinkFrom(); const GeometryLinkDynamic * linkTo = getDynamicLinkTo(); cpBody * bodyFrom = linkFrom->getBody(); cpBody * bodyTo = linkTo->getBody(); GetRotateAngleFunctor getAngle( this ); int rotateAngleInt = getAngle.getAngle(); float rotateAngle = M_PI / 180.0 * rotateAngleInt; m_ConstraintGear = cpSpaceAddConstraint( m_Space, cpGearJointNew( bodyFrom, bodyTo, rotateAngle, 1.0f ) ); }
void physics_add_top_down_friction(cpBody *body, cpBody *control, float friction, cpConstraint **out_pivot, cpConstraint **out_gear) { //emulates linear friction cpConstraint *pivot = cpSpaceAddConstraint(game.space, cpPivotJointNew2( control, body, cpvzero, cpvzero )); cpConstraintSetErrorBias(pivot, cpBodyGetMass(body) * friction); cpConstraintSetMaxForce(pivot, cpBodyGetMass(body) * friction); //emulates angular friction cpConstraint *gear = cpSpaceAddConstraint(game.space, cpGearJointNew( control, body, 0, 1 )); cpConstraintSetMaxBias(gear, 0); cpConstraintSetMaxForce(gear, friction / cpBodyGetMass(body) / 10); if (out_pivot) *out_pivot = pivot; if (out_gear) *out_gear = gear; }
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; }
CDynamics2DCylinderEntity::CDynamics2DCylinderEntity(CDynamics2DEngine& c_engine, CCylinderEntity& c_entity) : CDynamics2DEntity(c_engine, c_entity.GetEmbodiedEntity()), m_cCylinderEntity(c_entity), m_fMass(c_entity.GetMass()), m_ptShape(NULL), m_ptBody(NULL) { /* Get the radius of the entity */ Real fRadius = c_entity.GetRadius(); m_fHalfHeight = c_entity.GetHeight() * 0.5f; /* Create a circle object in the physics space */ const CVector3& cPosition = GetEmbodiedEntity().GetPosition(); if(c_entity.IsMovable()) { /* The cylinder is movable */ /* Create the body */ m_ptBody = cpSpaceAddBody(m_cEngine.GetPhysicsSpace(), cpBodyNew(m_fMass, cpMomentForCircle(m_fMass, 0, fRadius + fRadius, cpvzero))); m_ptBody->p = cpv(cPosition.GetX(), cPosition.GetY()); CRadians cXAngle, cYAngle, cZAngle; GetEmbodiedEntity().GetOrientation().ToEulerAngles(cZAngle, cYAngle, cXAngle); cpBodySetAngle(m_ptBody, cZAngle.GetValue()); /* Create the geometry */ m_ptShape = cpSpaceAddShape(m_cEngine.GetPhysicsSpace(), cpCircleShapeNew(m_ptBody, fRadius, cpvzero)); /* This object is grippable */ m_ptShape->collision_type = CDynamics2DEngine::SHAPE_GRIPPABLE; m_ptShape->data = reinterpret_cast<void*>(&c_entity); /* No elasticity */ m_ptShape->e = 0.0; /* Lots surface contact friction to help pushing */ m_ptShape->u = 0.7; /* Friction with ground */ m_ptLinearFriction = cpSpaceAddConstraint(m_cEngine.GetPhysicsSpace(), cpPivotJointNew2(m_cEngine.GetGroundBody(), m_ptBody, cpvzero, cpvzero)); m_ptLinearFriction->biasCoef = 0.0f; // disable joint correction m_ptLinearFriction->maxForce = 1.0f; // emulate linear friction m_ptAngularFriction = cpSpaceAddConstraint(m_cEngine.GetPhysicsSpace(), cpGearJointNew(m_cEngine.GetGroundBody(), m_ptBody, 0.0f, 1.0f)); m_ptAngularFriction->biasCoef = 0.0f; // disable joint correction m_ptAngularFriction->maxForce = 5.0f; // emulate angular friction } else { /* The cylinder is not movable */ /* Create the geometry */ m_ptShape = cpSpaceAddStaticShape(m_cEngine.GetPhysicsSpace(), cpCircleShapeNew(m_cEngine.GetGroundBody(), fRadius, cpv(cPosition.GetX(), cPosition.GetY()))); /* This object is normal */ m_ptShape->collision_type = CDynamics2DEngine::SHAPE_NORMAL; m_ptShape->data = reinterpret_cast<void*>(&c_entity); /* No elasticity */ m_ptShape->e = 0.0; /* Little contact friction to help sliding away */ m_ptShape->u = 0.1; } }
static cpSpace * init(void) { space = cpSpaceNew(); space->gravity = cpv(0, -600); cpBody *staticBody = &space->staticBody; cpShape *shape; // beveling all of the line segments slightly helps prevent things from getting stuck on cracks shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-256,300), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f)); shape->e = 0.0f; shape->u = 0.0f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; cpVect verts[] = { cpv(-30,-80), cpv(-30, 80), cpv( 30, 64), cpv( 30,-80), }; cpBody *plunger = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); plunger->p = cpv(-160,-80); shape = cpSpaceAddShape(space, cpPolyShapeNew(plunger, 4, verts, cpvzero)); shape->e = 1.0f; shape->u = 0.5f; shape->layers = 1; // add balls to hopper for(int i=0; i<numBalls; i++) balls[i] = add_ball(cpv(-224 + i,80 + 64*i)); // add small gear cpBody *smallGear = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 80, 0, cpvzero))); smallGear->p = cpv(-160,-160); cpBodySetAngle(smallGear, (cpFloat)-M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(smallGear, 80.0f, cpvzero)); shape->layers = 0; cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, smallGear, cpv(-160,-160), cpvzero)); // add big gear cpBody *bigGear = cpSpaceAddBody(space, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 160, 0, cpvzero))); bigGear->p = cpv(80,-160); cpBodySetAngle(bigGear, (cpFloat)M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(bigGear, 160.0f, cpvzero)); shape->layers = 0; cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, bigGear, cpv(80,-160), cpvzero)); // connect the plunger to the small gear. cpSpaceAddConstraint(space, cpPinJointNew(smallGear, plunger, cpv(80,0), cpv(0,0))); // connect the gears. cpSpaceAddConstraint(space, cpGearJointNew(smallGear, bigGear, (cpFloat)-M_PI_2, -2.0f)); // feeder mechanism cpFloat bottom = -300.0f; cpFloat top = 32.0f; cpBody *feeder = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForSegment(1.0f, cpv(-224.0f, bottom), cpv(-224.0f, top)))); feeder->p = cpv(-224, (bottom + top)/2.0f); cpFloat len = top - bottom; cpSpaceAddShape(space, cpSegmentShapeNew(feeder, cpv(0.0f, len/2.0f), cpv(0.0f, -len/2.0f), 20.0f)); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, feeder, cpv(-224.0f, bottom), cpv(0.0f, -len/2.0f))); cpVect anchr = cpBodyWorld2Local(feeder, cpv(-224.0f, -160.0f)); cpSpaceAddConstraint(space, cpPinJointNew(feeder, smallGear, anchr, cpv(0.0f, 80.0f))); // motorize the second gear motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(staticBody, bigGear, 3.0f)); return space; }
CDynamics2DBoxEntity::CDynamics2DBoxEntity(CDynamics2DEngine& c_engine, CBoxEntity& c_entity) : CDynamics2DEntity(c_engine, c_entity.GetEmbodiedEntity()), m_cBoxEntity(c_entity), m_fMass(c_entity.GetMass()), m_ptShape(NULL), m_ptBody(NULL) { /* Get the size of the entity */ CVector3 cHalfSize = c_entity.GetSize() * 0.5f; m_fHalfHeight = cHalfSize.GetZ(); /* Create a polygonal object in the physics space */ /* Start defining the vertices NOTE: points must be defined in a clockwise winding */ cpVect tVertices[] = { cpv(-cHalfSize.GetX(), -cHalfSize.GetY()), cpv(-cHalfSize.GetX(), cHalfSize.GetY()), cpv( cHalfSize.GetX(), cHalfSize.GetY()), cpv( cHalfSize.GetX(), -cHalfSize.GetY()) }; const CVector3& cPosition = GetEmbodiedEntity().GetPosition(); CRadians cXAngle, cYAngle, cZAngle; GetEmbodiedEntity().GetOrientation().ToEulerAngles(cZAngle, cYAngle, cXAngle); if(c_entity.GetEmbodiedEntity().IsMovable()) { /* The box is movable */ /* Create the body */ m_ptBody = cpSpaceAddBody(m_cEngine.GetPhysicsSpace(), cpBodyNew(m_fMass, cpMomentForPoly(m_fMass, 4, tVertices, cpvzero))); m_ptBody->p = cpv(cPosition.GetX(), cPosition.GetY()); cpBodySetAngle(m_ptBody, cZAngle.GetValue()); /* Create the geometry */ m_ptShape = cpSpaceAddShape(m_cEngine.GetPhysicsSpace(), cpPolyShapeNew(m_ptBody, 4, tVertices, cpvzero)); /* This object is grippable */ m_ptShape->collision_type = CDynamics2DEngine::SHAPE_GRIPPABLE; m_ptShape->data = reinterpret_cast<void*>(&c_entity); /* No elasticity */ m_ptShape->e = 0.0; /* Lots contact friction to help pushing */ m_ptShape->u = 0.7; /* Friction with ground */ m_ptLinearFriction = cpSpaceAddConstraint(m_cEngine.GetPhysicsSpace(), cpPivotJointNew2(m_cEngine.GetGroundBody(), m_ptBody, cpvzero, cpvzero)); m_ptLinearFriction->maxBias = 0.0f; // disable joint correction m_ptLinearFriction->maxForce = 1.49f; // emulate linear friction (this is just slightly smaller than FOOTBOT_MAX_FORCE) m_ptAngularFriction = cpSpaceAddConstraint(m_cEngine.GetPhysicsSpace(), cpGearJointNew(m_cEngine.GetGroundBody(), m_ptBody, 0.0f, 1.0f)); m_ptAngularFriction->maxBias = 0.0f; // disable joint correction m_ptAngularFriction->maxForce = 1.49f; // emulate angular friction (this is just slightly smaller than FOOTBOT_MAX_TORQUE) } else { /* The box is not movable */ /* Manually rotate the vertices */ cpVect tRot = cpvforangle(cZAngle.GetValue()); tVertices[0] = cpvrotate(tVertices[0], tRot); tVertices[1] = cpvrotate(tVertices[1], tRot); tVertices[2] = cpvrotate(tVertices[2], tRot); tVertices[3] = cpvrotate(tVertices[3], tRot); /* Create the geometry */ m_ptShape = cpSpaceAddStaticShape(m_cEngine.GetPhysicsSpace(), cpPolyShapeNew(m_cEngine.GetGroundBody(), 4, tVertices, cpv(cPosition.GetX(), cPosition.GetY()))); /* This object is normal */ m_ptShape->collision_type = CDynamics2DEngine::SHAPE_NORMAL; m_ptShape->data = reinterpret_cast<void*>(&c_entity); /* No elasticity */ m_ptShape->e = 0.0; /* Little contact friction to help sliding away */ m_ptShape->u = 0.1; } }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Use the arrow keys to control the machine."; space = cpSpaceNew(); cpSpaceSetGravity(space, cpv(0, -600)); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // beveling all of the line segments slightly helps prevent things from getting stuck on cracks shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-256,300), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpVect verts[] = { cpv(-30,-80), cpv(-30, 80), cpv( 30, 64), cpv( 30,-80), }; cpBody *plunger = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); cpBodySetPos(plunger, cpv(-160,-80)); shape = cpSpaceAddShape(space, cpPolyShapeNew(plunger, 4, verts, cpvzero)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, 1); // add balls to hopper for(int i=0; i<numBalls; i++) balls[i] = add_ball(cpv(-224 + i,80 + 64*i)); // add small gear cpBody *smallGear = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 80, 0, cpvzero))); cpBodySetPos(smallGear, cpv(-160,-160)); cpBodySetAngle(smallGear, -M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(smallGear, 80.0f, cpvzero)); cpShapeSetLayers(shape, 0); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, smallGear, cpv(-160,-160), cpvzero)); // add big gear cpBody *bigGear = cpSpaceAddBody(space, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 160, 0, cpvzero))); cpBodySetPos(bigGear, cpv(80,-160)); cpBodySetAngle(bigGear, M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(bigGear, 160.0f, cpvzero)); cpShapeSetLayers(shape, 0); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, bigGear, cpv(80,-160), cpvzero)); // connect the plunger to the small gear. cpSpaceAddConstraint(space, cpPinJointNew(smallGear, plunger, cpv(80,0), cpv(0,0))); // connect the gears. cpSpaceAddConstraint(space, cpGearJointNew(smallGear, bigGear, -M_PI_2, -2.0f)); // feeder mechanism cpFloat bottom = -300.0f; cpFloat top = 32.0f; cpBody *feeder = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForSegment(1.0f, cpv(-224.0f, bottom), cpv(-224.0f, top)))); cpBodySetPos(feeder, cpv(-224, (bottom + top)/2.0f)); cpFloat len = top - bottom; cpSpaceAddShape(space, cpSegmentShapeNew(feeder, cpv(0.0f, len/2.0f), cpv(0.0f, -len/2.0f), 20.0f)); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, feeder, cpv(-224.0f, bottom), cpv(0.0f, -len/2.0f))); cpVect anchr = cpBodyWorld2Local(feeder, cpv(-224.0f, -160.0f)); cpSpaceAddConstraint(space, cpPinJointNew(feeder, smallGear, anchr, cpv(0.0f, 80.0f))); // motorize the second gear motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(staticBody, bigGear, 3.0f)); return space; }