void EnemyPatroller::update(double dt) { if(!dying) { //setCurrentAnimation("walk"); const int speed_gravity = 960; const double vision_range = 320; const double vision_min_range = 32; if(isGrounded()) { if (facing_direction == Facing::Left) { if(body->v.x > -walk_speed) cpBodyApplyImpulse(body, cpv(-500, 0), cpv(0, 0)); if(!leftBumper->isGrounded()) facing_direction = Facing::Right; } else if (facing_direction == Facing::Right) { if(body->v.x < walk_speed) cpBodyApplyImpulse(body, cpv(500, 0), cpv(0, 0)); if(!rightBumper->isGrounded()) facing_direction = Facing::Left; } } updateSpriteFacing(); } }
void update_friction(int n) { // kill lateral velocity const cpFloat max_lateral_impulse = 300; cpVect impulse = cpvmult(cpvneg(lateral_velocity(n)), cpBodyGetMass(tire[n])); //printf("%f\n", cpvlength(impulse)); if(cpvlength(impulse) > max_lateral_impulse) impulse = cpvmult(impulse, max_lateral_impulse / cpvlength(impulse)); cpBodyApplyImpulse(tire[n], impulse, cpvzero); // TODO - kill angular velocity? cpFloat inertia = cpBodyGetMoment(tire[n]); cpFloat av = cpBodyGetAngVel(tire[n]); if(av != 0) cpBodySetAngVel(tire[n], av / 1.2); // apply drag cpVect forward_normal = forward_velocity(n); cpFloat forward_speed = cpvlength(forward_normal); if(forward_speed < 1) { cpBodySetVel(tire[n], cpvzero); } else { forward_normal = cpvnormalize(forward_normal); cpFloat drag = -1 * forward_speed; cpBodyApplyImpulse(tire[n], cpvmult(forward_normal, drag), cpvzero); } }
void cpArbiterApplyImpulse(cpArbiter *arb) { cpBody *a = arb->a->body; cpBody *b = arb->b->body; for(int i=0; i<arb->numContacts; i++){ cpContact *con = &arb->contacts[i]; cpVect n = con->n; cpVect r1 = con->r1; cpVect r2 = con->r2; // Calculate the relative bias velocities. cpVect vb1 = cpvadd(a->v_bias, cpvmult(cpvperp(r1), a->w_bias)); cpVect vb2 = cpvadd(b->v_bias, cpvmult(cpvperp(r2), b->w_bias)); cpFloat vbn = cpvdot(cpvsub(vb2, vb1), n); // Calculate and clamp the bias impulse. cpFloat jbn = (con->bias - vbn)*con->nMass; cpFloat jbnOld = con->jBias; con->jBias = cpfmax(jbnOld + jbn, 0.0f); jbn = con->jBias - jbnOld; // Apply the bias impulse. cpVect jb = cpvmult(n, jbn); cpBodyApplyBiasImpulse(a, cpvneg(jb), r1); cpBodyApplyBiasImpulse(b, jb, r2); // Calculate the relative velocity. cpVect v1 = cpvadd(a->v, cpvmult(cpvperp(r1), a->w)); cpVect v2 = cpvadd(b->v, cpvmult(cpvperp(r2), b->w)); cpVect vr = cpvsub(v2, v1); cpFloat vrn = cpvdot(vr, n); // Calculate and clamp the normal impulse. cpFloat jn = -(con->bounce + vrn)*con->nMass; cpFloat jnOld = con->jnAcc; con->jnAcc = cpfmax(jnOld + jn, 0.0f); jn = con->jnAcc - jnOld; // Calculate the relative tangent velocity. cpVect t = cpvperp(n); cpFloat vrt = cpvdot(cpvadd(vr, arb->target_v), t); // Calculate and clamp the friction impulse. cpFloat jtMax = arb->u*con->jnAcc; cpFloat jt = -vrt*con->tMass; cpFloat jtOld = con->jtAcc; con->jtAcc = cpfmin(cpfmax(jtOld + jt, -jtMax), jtMax); jt = con->jtAcc - jtOld; // Apply the final impulse. cpVect j = cpvadd(cpvmult(n, jn), cpvmult(t, jt)); cpBodyApplyImpulse(a, cpvneg(j), r1); cpBodyApplyImpulse(b, j, r2); } }
static void applyImpulse(cpPulleyJoint *joint) { cpBody* b1 = joint->constraint.a; cpBody* b2 = joint->constraint.b; cpVect r1 = joint->r1; cpVect r2 = joint->r2; // The magic and mystery below if (joint->state) { cpVect v1 = cpvadd(b1->v, cpv(-b1->w * r1.y, b1->w * r1.x)); cpVect v2 = cpvadd(b2->v, cpv(-b2->w * r2.y, b2->w * r2.x)); cpFloat Cdot = -cpvdot(joint->u1, v1) - joint->ratio * cpvdot(joint->u2, v2); cpFloat impulse = joint->pulleyMass * (-Cdot); cpFloat oldImpulse = joint->jnAcc; joint->jnAcc = cpfmax(0.0f, joint->jnAcc + impulse); impulse = joint->jnAcc - oldImpulse; cpVect P1 = cpvmult(joint->u1, -impulse); cpVect P2 = cpvmult(joint->u2, -joint->ratio * impulse); cpBodyApplyImpulse(b1, P1, r1); cpBodyApplyImpulse(b2, P2, r2); } if (joint->limitState1) { cpVect v1 = cpvadd(b1->v, cpv(-b1->w * r1.y, b1->w * r1.x)); cpFloat Cdot = -cpvdot(joint->u1, v1); cpFloat impulse = -joint->limitMass1 * Cdot; cpFloat oldImpulse = joint->jnAccLim1; joint->jnAccLim1 = cpfmax(0.0f, joint->jnAccLim1 + impulse); impulse = joint->jnAccLim1 - oldImpulse; cpVect P1 = cpvmult(joint->u1, -impulse); cpBodyApplyImpulse(b1, P1, r1); } if (joint->limitState2) { cpVect v2 = cpvadd(b2->v, cpv(-b2->w * r2.y, b2->w * r2.x)); cpFloat Cdot = -cpvdot(joint->u2, v2); cpFloat impulse = -joint->limitMass2 * Cdot; cpFloat oldImpulse = joint->jnAccLim2; joint->jnAccLim2 = cpfmax(0.0f, joint->jnAccLim2 + impulse); impulse = joint->jnAccLim2 - oldImpulse; cpVect P2 = cpvmult(joint->u2, -impulse); cpBodyApplyImpulse(b2, P2, r2); } }
void cpArbiterPreStep(cpArbiter *arb, cpFloat dt_inv) { cpShape *shapea = arb->a; cpShape *shapeb = arb->b; arb->e = shapea->e * shapeb->e; arb->u = shapea->u * shapeb->u; arb->target_v = cpvsub(shapeb->surface_v, shapea->surface_v); cpBody *a = shapea->body; cpBody *b = shapeb->body; for(int i=0; i<arb->numContacts; i++){ cpContact *con = &arb->contacts[i]; // Calculate the offsets. con->r1 = cpvsub(con->p, a->p); con->r2 = cpvsub(con->p, b->p); // Calculate the mass normal. cpFloat mass_sum = a->m_inv + b->m_inv; cpFloat r1cn = cpvcross(con->r1, con->n); cpFloat r2cn = cpvcross(con->r2, con->n); cpFloat kn = mass_sum + a->i_inv*r1cn*r1cn + b->i_inv*r2cn*r2cn; con->nMass = 1.0f/kn; // Calculate the mass tangent. cpVect t = cpvperp(con->n); cpFloat r1ct = cpvcross(con->r1, t); cpFloat r2ct = cpvcross(con->r2, t); cpFloat kt = mass_sum + a->i_inv*r1ct*r1ct + b->i_inv*r2ct*r2ct; con->tMass = 1.0f/kt; // Calculate the target bias velocity. con->bias = -cp_bias_coef*dt_inv*cpfmin(0.0f, con->dist + cp_collision_slop); con->jBias = 0.0f; // Calculate the target bounce velocity. cpVect v1 = cpvadd(a->v, cpvmult(cpvperp(con->r1), a->w)); cpVect v2 = cpvadd(b->v, cpvmult(cpvperp(con->r2), b->w)); con->bounce = cpvdot(con->n, cpvsub(v2, v1))*arb->e; // Apply the previous accumulated impulse. cpVect j = cpvadd(cpvmult(con->n, con->jnAcc), cpvmult(t, con->jtAcc)); cpBodyApplyImpulse(a, cpvneg(j), con->r1); cpBodyApplyImpulse(b, j, con->r2); } }
// adds an asteroid shape to the cpSpace cpBody * core_add_new_asteroid ( cpSpace *space, const int type, const double p1x, const double p1y, const double p2x, const double p2y, Color *color, const double orientation, const double friction, const double elasticity, const double density, const int index, cpVect impulse, cpVect offset ) { cpShape * shape; if ( type == CIRCLE_TYPE ) { shape = core_add_new_shape ( space, type, p1x, p1y, p2x, p2y, color, orientation, friction, elasticity, density, index ); } else { shape = core_add_single_segment_shape ( space, p1x, p1y, p2x, p2y, color, friction, elasticity, density, index ); } if ( shape == NULL ) return NULL; DrawShapeInfo * dsi = (DrawShapeInfo *) shape->data; dsi->space_shape_type = 1; // set collision type cpShapeSetCollisionType ( shape, ASTEROID_COLLISION_TYPE ); // apply impulse cpBody *body = cpShapeGetBody ( shape ); impulse = cpv ( impulse.x * IMPULSE_MULTIPLIER, impulse.y * IMPULSE_MULTIPLIER ); cpBodyApplyImpulse ( body, impulse, offset ); return body; }
void update_drive() { const cpFloat max_forward_speed = 150; const cpFloat max_backward_speed = -20; const cpFloat max_drive_force = 100; int i; for(i=0; i<1; i++) { cpFloat desired_speed = 0; // find desired speed if(controls.forward) desired_speed = max_forward_speed; else if(controls.back) desired_speed = max_backward_speed; // find speed cpVect forward_normal = cpvperp(cpvforangle(cpBodyGetAngle(tire[i]))); cpFloat speed = cpvdot(forward_velocity(i), forward_normal); // apply force cpFloat force = 0; if(desired_speed > speed) force = max_drive_force; else if(desired_speed < speed) force = -max_drive_force; else return; cpBodyApplyImpulse(tire[i], cpvmult(forward_normal, force), cpvzero); } }
void JumpAI(entity_t *ent) { vec2_t temp_vec2; cpVect cp_temp; if(!ent->mData || !ent) { printf("MoveAI given a null paramerter \n"); return; } //Standard Vars if(ent->mCollisionType != COLLISION_TYPE_RAGDOLL) { ent->mCollisionType = COLLISION_TYPE_RAGDOLL; SetCpCollisionType(ent); } //Move if(cpBodyKineticEnergy(ent->mPhysicsProperties->body) < 1 ) { temp_vec2.x = ent->mData->mVariables[AI_VAR_DIR_X]; temp_vec2.y = ent->mData->mVariables[AI_VAR_DIR_Y]; cp_temp = Vec2Cp(&temp_vec2); cp_temp = cpvnormalize(cp_temp); cp_temp = cpvmult(cp_temp, ent->mData->mVariables[AI_VAR_SPEED]); cpBodyApplyImpulse(ent->mPhysicsProperties->body, cp_temp, cpvzero); } StandarAI_Think(ent); }
__declspec( dllexport ) void explosion( const void * _in, int in_size, void * _out, int out_sz ) { int i; Variable *var; cpBody *body; cpVect bp; float angle; float dist; float divi; float finalPower; float power = PEEKFLOAT(INPUT_MEMBLOCK,0); cpVect position = PEEKVECT(INPUT_MEMBLOCK,4); for (i = 0;i != mVariableHandler.mSize;i++) { var = mVariableHandler.mPtrArray[i]; if (var == NULL)continue; if (var->mType != VarTypeBody) continue; body = (cpBody*)var->mPtr; if (cpBodyIsStatic(body)) continue; bp = cpvsub(position,cpBodyGetPos(body)); dist = cpvlength(bp)+0.1f; divi = (power/dist); finalPower = -min(power,divi*divi); angle = cpvtoangle(bp); cpBodyApplyImpulse(body,cpv(cosf(angle)*finalPower,sinf(angle)*finalPower),cpBodyWorld2Local(body,position)); } }
static int cpBody_applyImpulse (lua_State *L){ cpBody *b = check_cpBody(L, 1); cpVect j = check_cpVect(L, 2); cpVect r = check_cpVect(L, 4); cpBodyApplyImpulse(b, j, r); return 0; }
bool AnimationLayer::init() { bool bRet = false; do { CC_BREAK_IF(!Layer::init()); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("running.plist"); spriteSheet = SpriteBatchNode::create("running.png"); addChild(spriteSheet); //init running action Vector< SpriteFrame * > animFrames; for(int i = 0; i < 8; i++) { String str = String::createWithFormat("runner%d.png",i)->getCString(); SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str.getCString()); animFrames.pushBack(frame); } Animation *animation = Animation::createWithSpriteFrames(animFrames, 0.1); runningAction = RepeatForever::create(Animate::create(animation)); //sprite = Sprite::createWithSpriteFrameName("runner0.png"); //sprite->setPosition(ccp(80,85)); sprite = PhysicsSprite::createWithSpriteFrameName("runner0.png"); auto contentSize = sprite->getContentSize(); //初始化身体 this->body = cpBodyNew(1,cpMomentForBox(1, contentSize.width, contentSize.height)); this->body->p = cpv(GlobalUtils::g_runnerStartX, GlobalUtils::g_groundHeight + contentSize.height/2); cpBodyApplyImpulse(this->body,cpv(150,0), cpv(0,0)); cpSpaceAddBody(this->space, this->body); //init shape this->shape = cpBoxShapeNew(this->body,contentSize.width-14,contentSize.height); cpSpaceAddShape(this->space, this->shape); sprite->setCPBody(this->body); sprite->runAction(runningAction); spriteSheet->addChild(sprite); scheduleUpdate(); bRet = true; }while(0); return bRet; }
// adds a fresstyle shape with impulse to the cpSpace void core_add_freestyle_shape_with_impulse ( cpSpace * space, cpVect* verts , const int num_verts, Color *color, const double friction, const double elasticity, const double density, const int index, cpVect impulse, cpVect offset ) { cpBody * body = core_add_freestyle_shape ( space, verts , num_verts, color, friction, elasticity, density, index ); if ( body == NULL ) return; // apply impulse impulse = cpv ( impulse.x*IMPULSE_MULTIPLIER, impulse.y*IMPULSE_MULTIPLIER ); cpBodyApplyImpulse ( body, impulse, offset ); }
void cpArbiterApplyCachedImpulse(cpArbiter *arb) { cpShape *shapea = arb->a; cpShape *shapeb = arb->b; arb->u = shapea->u * shapeb->u; arb->target_v = cpvsub(shapeb->surface_v, shapea->surface_v); cpBody *a = shapea->body; cpBody *b = shapeb->body; for(int i=0; i<arb->numContacts; i++){ cpContact *con = &arb->contacts[i]; cpVect t = cpvperp(con->n); cpVect j = cpvadd(cpvmult(con->n, con->jnAcc), cpvmult(t, con->jtAcc)); cpBodyApplyImpulse(a, cpvneg(j), con->r1); cpBodyApplyImpulse(b, j, con->r2); } }
void BossSpider::update(float dt) { if(!dying) { time += dt; patrolTime += dt; //setCurrentAnimation("walk"); const int speed_gravity = 960; const float vision_range = 320; const float vision_min_range = 32; if(facing_direction == Facing::Left) { if(body->v.x > -walk_speed) cpBodyApplyImpulse(body, cpv(-500, 0), cpv(0, 0)); if(patrolTime > patrolInterval) { facing_direction = Facing::Right; patrolTime = 0; } } else { if(body->v.x < walk_speed) cpBodyApplyImpulse(body, cpv(500, 0), cpv(0, 0)); if(patrolTime > patrolInterval) { facing_direction = Facing::Left; patrolTime = 0; } } updateSpriteFacing(); if(lastShot + shootInterval < time) { lastShot = time; EnemyCentipedeProjectile * projectile = new EnemyCentipedeProjectile(facing_direction, body->p.x, int(body->p.y - 20.0f)); } //checkcollisions(); } }
void Player::run(float dir) { if (dir > 0) { m_facingRight = true; } else { m_facingRight = false; } cpBodyApplyImpulse(m_pBody, cpv(dir, 0.0), cpv(0, 0)); }
static JSBool body_applyDirectionalImpulse(JSContext* cx, uintN argc, jsval* vp) { jsdouble amt; if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "d", &amt)) { /* Throw a JavaScript exception. */ JS_ReportError(cx, "body_applyDirectionalImpulse: couldn't parse out amt"); return JS_FALSE; } JSObject* bodyObj = JS_THIS_OBJECT(cx, vp); cpBody* body = (cpBody*)JS_GetPrivate(cx, bodyObj); cpFloat angle = cpBodyGetAngle(body); double xAmt = -sin(angle) * amt; double yAmt = cos(angle) * amt; cpVect j = {xAmt, yAmt}; cpBodyApplyImpulse(body, j, cpvzero); jsval rVal = JSVAL_VOID; JS_SET_RVAL(cx, vp, rVal); return JS_TRUE; }
// adds an impulse shape to the cpSpace cpBody * core_add_new_shape_with_impulse ( cpSpace *space, const int type, const double p1x, const double p1y, const double p2x, const double p2y, Color *color, const double orientation, const double friction, const double elasticity, const double density, const int index, cpVect impulse, cpVect offset ) { cpShape * shape; if ( type == BOX_TYPE || type == CIRCLE_TYPE ) { shape = core_add_new_shape ( space, type, p1x, p1y, p2x, p2y, color, orientation, friction, elasticity, density, index ); } else { shape = core_add_single_segment_shape ( space, p1x, p1y, p2x, p2y, color, friction, elasticity, density, index ); } if ( shape == NULL ) return NULL; DrawShapeInfo *info = cpShapeGetUserData ( shape ); info->space_shape_type = SPACE_TYPE_PLANET_R; cpBody *body = cpShapeGetBody ( shape ); impulse = cpv ( impulse.x * IMPULSE_MULTIPLIER, impulse.y * IMPULSE_MULTIPLIER ); cpBodyApplyImpulse ( body, impulse, offset ); return body; }
void PhysicsBody::applyImpulse(Point impulse, Point offset) { cpBodyApplyImpulse(_info->body, PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset)); }
void cBody::ApplyImpulse( const cVect j, const cVect r ) { cpBodyApplyImpulse( mBody, tocpv( j ), tocpv( r ) ); }
void PhysicsBody::applyImpulse(const Vect& impulse, const Vec2& offset) { cpBodyApplyImpulse(_info->getBody(), PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset)); }
void fff::kitty::stopMovingRight(){ if (!rightimpulse){ return;} cpBodyApplyImpulse(body, (cpVect){-640.f, 0.f}, cpvzero); rightimpulse = false; }
void fff::kitty::moveRight(){ if (rightimpulse){ return;} cpBodyApplyImpulse(body, (cpVect){640.f, 0.f}, cpvzero); rightimpulse = true; }
void fff::kitty::stopMovingLeft(){ if (!leftimpulse){ return;} cpBodyApplyImpulse(body, (cpVect){640.f, 0.f}, cpvzero); leftimpulse = false; }
void fff::kitty::moveLeft(){ if (leftimpulse){ return;} cpBodyApplyImpulse(body, (cpVect){-640.f, 0.f}, cpvzero); leftimpulse = true; }
void worldEnt_applyImpulse(WorldEntity_t *aEntity, vec2_t aImpulse, vec2_t aOffset) { cpBodyApplyImpulse(aEntity->cpBody, VEC2_TO_CPV(aImpulse), VEC2_TO_CPV(aOffset)); }
void bmx_body_applyimpulse(cpBody * body, cpVect * impulse, cpVect * offset) { cpBodyApplyImpulse(body, *impulse, *offset); }
static VALUE rb_cpBodyApplyImpulse(VALUE self, VALUE j, VALUE r) { cpBodyApplyImpulse(BODY(self), *VGET(j), *VGET(r)); return self; }
void physics_apply_impulse_at(Entity ent, Vec2 impulse, Vec2 at) { PhysicsInfo *info = entitypool_get(pool, ent); error_assert(info); cpBodyApplyImpulse(info->body, cpv_of_vec2(impulse), cpv_of_vec2(at)); }
void wrBodyApplyImpulse(cpBody *b, cpVect *j, cpVect *r) { cpBodyApplyImpulse(b, *j, *r); }
void Player::jump(float force) { cpBodyApplyImpulse(m_pBody, cpv(0.0, force), cpvzero); }