static void playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { int jumpState = (ChipmunkDemoKeyboard.y > 0.0f); // Grab the grounding normal from last frame cpVect groundNormal = cpvzero; cpBodyEachArbiter(playerBody, (cpBodyArbiterIteratorFunc)SelectPlayerGroundNormal, &groundNormal); grounded = (groundNormal.y > 0.0); if(groundNormal.y < 0.0f) remainingBoost = 0.0f; // Do a normal-ish update cpBool boost = (jumpState && remainingBoost > 0.0f); cpVect g = (boost ? cpvzero : gravity); cpBodyUpdateVelocity(body, g, damping, dt); // Target horizontal speed for air/ground control cpFloat target_vx = PLAYER_VELOCITY*ChipmunkDemoKeyboard.x; // Update the surface velocity and friction // Note that the "feet" move in the opposite direction of the player. cpVect surface_v = cpv(-target_vx, 0.0); playerShape->surfaceV = surface_v; playerShape->u = (grounded ? PLAYER_GROUND_ACCEL/GRAVITY : 0.0); // Apply air control if not grounded if(!grounded){ // Smoothly accelerate the velocity playerBody->v.x = cpflerpconst(playerBody->v.x, target_vx, PLAYER_AIR_ACCEL*dt); } body->v.y = cpfclamp(body->v.y, -FALL_VELOCITY, INFINITY); }
static void _update_collisions(PhysicsInfo *info) { if (info->collisions) return; /* gather collisions */ info->collisions = array_new(Collision); cpBodyEachArbiter(info->body, _add_collision, info->collisions); }
void cBody::EachArbiter( ArbiterIteratorFunc Func, void * data ) { cArbiterIterator it( this, data, Func ); cpBodyEachArbiter( mBody, &BodyArbiterIteratorFunc, (void*)&it ); }
bool DynamicObject::isGrounded() { m_grounded = false; cpBodyEachArbiter(m_pBody, (cpBodyArbiterIteratorFunc)updateGrounded, &m_grounded); return m_grounded; }
void Body::eachArbiter(BodyArbiterIteratorFunc func) { cpBodyEachArbiter(body,*BodyEachArbiter,&func); }
void Body::eachArbiter(cpBodyArbiterIteratorFunc func,void *data) { cpBodyEachArbiter(body,func,data); }