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); }
// set gravity (of previously chosen objects) to point towards black holes void orbit(cpBody * body, cpVect gravity, cpFloat damping, cpFloat dt) { extern struct objnode objroot[]; struct objnode *objx; struct cpBody *hole; cpVect bpos, hpos, unitv, gvect; cpFloat hmass, g, distsq; objx = objroot; while ((objx = objx->next) != NULL) { if (objx->bhole == true && objx->b != body) { hole = objx->b; bpos = cpBodyGetPos(body); hpos = cpBodyGetPos(hole); distsq = cpvdistsq(hpos, bpos); distsq = (distsq == 0) ? 1e-50 : distsq; // don't divide by zero hmass = cpBodyGetMass(hole); g = hmass * BGRAV * (1 / distsq); g = (distsq < RDSQ) ? g * -REPFS : g; // shoot close balls away unitv = cpvmult(cpvsub(hpos, bpos), (1 / sqrt(distsq))); gvect = cpvmult(unitv, g); cpBodyUpdateVelocity(body, gvect, damping, dt); } } }
static void playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { cpBodyUpdateVelocity(body, gravity, damping, dt); body->v.y = cpfmax(body->v.y, -700); body->v.x = cpfclamp(body->v.x, -400, 400); }
static void planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { // Gravitational acceleration is proportional to the inverse square of // distance, and directed toward the origin. The central planet is assumed // to be massive enough that it affects the satellites but not vice versa. cpVect p = cpBodyGetPosition(body); cpFloat sqdist = cpvlengthsq(p); cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist))); cpBodyUpdateVelocity(body, g, damping, dt); }
static void internalBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { cpBodyUpdateVelocity(body, cpvzero, damping, dt); // Skip kinematic bodies. if(cpBodyGetType(body) == CP_BODY_TYPE_KINEMATIC) return; cpAssertSoft(body->m > 0.0f && body->i > 0.0f, "Body's mass and moment must be positive to simulate. (Mass: %f Moment: f)", body->m, body->i); cocos2d::PhysicsBody *physicsBody = static_cast<cocos2d::PhysicsBody*>(body->userData); if(physicsBody->isGravityEnabled()) body->v = cpvclamp(cpvadd(cpvmult(body->v, damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), dt)), physicsBody->getVelocityLimit()); else body->v = cpvclamp(cpvadd(cpvmult(body->v, damping), cpvmult(cpvmult(body->f, body->m_inv), dt)), physicsBody->getVelocityLimit()); cpFloat w_limit = physicsBody->getAngularVelocityLimit(); body->w = cpfclamp(body->w*damping + body->t*body->i_inv*dt, -w_limit, w_limit); // Reset forces. body->f = cpvzero; //to check body sanity cpBodySetTorque(body, 0.0f); }
void cBody::UpdateVelocity( cVect gravity, cpFloat damping, cpFloat dt ) { cpBodyUpdateVelocity( mBody, tocpv( gravity ), damping, dt ); }
void bmx_cpbody_updatevelocity(cpBody * body, cpVect * gravity, cpFloat damping, cpFloat dt) { cpBodyUpdateVelocity(body, *gravity, damping, dt); }
static inline void cpBodyUpdateVelocityWithoutGravity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { cpBodyUpdateVelocity(body, cpvzero, damping, dt); }
// From cpBody.h void wrBodyUpdateVelocity(cpBody *b, cpVect *g, cpFloat d, cpFloat dt) { cpBodyUpdateVelocity(b, *g, d, dt); }
static VALUE rb_cpBodyUpdateVelocity(VALUE self, VALUE g, VALUE dmp, VALUE dt) { cpBodyUpdateVelocity(BODY(self), *VGET(g), NUM2DBL(dmp), NUM2DBL(dt)); return self; }
void Body::updateVelocity(const cp::Vect& gravity,cpFloat damping,cpFloat dt) { cpBodyUpdateVelocity(body,gravity,damping,dt); }