__declspec( dllexport ) void push( const void * _in, int in_size, void * _out, int out_sz ) { int index; Variable *var; cpBody *body; cpShape *shape; index = PEEKINT(INPUT_MEMBLOCK,0); var = vhGetVariable(&mVariableHandler,index); switch (var->mType) { case VarTypeBody: body = (cpBody*)var->mPtr; cpBodySetAngle(body,degToRad(PEEKFLOAT(INPUT_MEMBLOCK,4))); cpBodySetPos(body,PEEKVECT(INPUT_MEMBLOCK,8)); cpBodySetAngVel(body,degToRad(PEEKFLOAT(INPUT_MEMBLOCK,16))); cpBodySetVel(body,PEEKVECT(INPUT_MEMBLOCK,20)); break; case VarTypeShape: shape = (cpShape*)var->mPtr; cpShapeSetFriction(shape,PEEKFLOAT(INPUT_MEMBLOCK,4)); cpShapeSetElasticity(shape,PEEKFLOAT(INPUT_MEMBLOCK,8)); cpShapeSetLayers(shape,PEEKUINT(INPUT_MEMBLOCK,12)); cpShapeSetGroup(shape,PEEKUINT(INPUT_MEMBLOCK,16)); break; } }
//Run After All Think Functions //This is very generic void StandarAI_Think(entity_t *ent) { entity_t *cache_ref; cpVect temp_cp; int flags; flags = ent->mData->mFlags; ent->mNextThink = gCurrentTime + ent->mData->mVariables[AI_VAR_FRAMES]*UPDATE_FRAME_DELAY; ent->mDamage = ent->mData->mVariables[AI_VAR_DAMAGE]; cache_ref = FindCachedEntity(ent->mName); //Checks are right to left if(cache_ref->mPhysicsProperties && cache_ref) { if(flags & AI_FLAG_GRAVITY) { temp_cp.x = ent->mPhysicsProperties->body->v.x; temp_cp.y = 0; cpBodySetVel(ent->mPhysicsProperties->body, temp_cp); } } //Check Data if( --ent->mData->mVariables[AI_VAR_TIME] == 0) { ent->mData->mVariables[AI_VAR_TIME] = 1; ent->mData = ent->mData->mLink; } //Kill ent if dead if(ent->mHealth <= 0) { FreeEntity(ent); } }
void PhysicsBody::setDynamic(bool dynamic) { if (dynamic != _dynamic) { _dynamic = dynamic; if (dynamic) { if (_world && _cpBody->CP_PRIVATE(space)) { cpSpaceConvertBodyToDynamic(_world->_cpSpace, _cpBody, _mass, _moment); cpSpaceAddBody(_world->_cpSpace, _cpBody); } else { cpBodySetMass(_cpBody, _mass); cpBodySetMoment(_cpBody, _moment); } } else { if (_world && _cpBody->CP_PRIVATE(space)) { cpSpaceRemoveBody(_world->_cpSpace, _cpBody); cpSpaceConvertBodyToStatic(_world->_cpSpace, _cpBody); } else { cpBodySetMass(_cpBody, PHYSICS_INFINITY); cpBodySetMoment(_cpBody, PHYSICS_INFINITY); cpBodySetVel(_cpBody, cpvzero); cpBodySetAngVel(_cpBody, 0.0); } } } }
void WalkAI(entity_t *ent) { vec2_t temp_vec2; cpVect cp_vect; 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); } //Set direction vector temp_vec2.x = ent->mData->mVariables[AI_VAR_DIR_X]; temp_vec2.y = ent->mData->mVariables[AI_VAR_DIR_Y]; cp_vect = Vec2Cp(&temp_vec2); //Normalize & Multiply by speed cp_vect = cpvnormalize(cp_vect); cp_vect = cpvmult(cp_vect, ent->mData->mVariables[AI_VAR_SPEED]); //Set Velocity cpBodySetVel(ent->mPhysicsProperties->body, cp_vect); StandarAI_Think(ent); }
static void update(int ticks) { cpFloat coef = (2.0f + ChipmunkDemoKeyboard.y)/3.0f; cpFloat rate = ChipmunkDemoKeyboard.x*30.0f*coef; cpSimpleMotorSetRate(motor, rate); cpConstraintSetMaxForce(motor, rate ? 1000000.0f : 0.0f); int steps = 2; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; for(int i=0; i<steps; i++){ cpSpaceStep(space, dt); for(int i=0; i<numBalls; i++){ cpBody *ball = balls[i]; cpVect pos = cpBodyGetPos(ball); if(pos.x > 320.0f){ cpBodySetVel(ball, cpvzero); cpBodySetPos(ball, cpv(-224.0f, 200.0f)); } } } }
static void _update_kinematics() { PhysicsInfo *info; cpVect pos; cpFloat ang; Scalar invdt; Entity ent; if (timing_dt <= FLT_EPSILON) return; invdt = 1 / timing_dt; entitypool_foreach(info, pool) if (info->type == PB_KINEMATIC) { ent = info->pool_elem.ent; /* move to transform */ pos = cpv_of_vec2(transform_get_position(ent)); ang = transform_get_rotation(ent); cpBodySetPos(info->body, pos); cpBodySetAngle(info->body, ang); info->last_dirty_count = transform_get_dirty_count(ent); /* update linear, angular velocities based on delta */ cpBodySetVel(info->body, cpvmult(cpvsub(pos, info->last_pos), invdt)); cpBodySetAngVel(info->body, (ang - info->last_ang) * invdt); cpSpaceReindexShapesForBody(space, info->body); /* save current state for next computation */ info->last_pos = pos; info->last_ang = ang; } }
cpBody* drawing_update(cpSpace *space, cpBody *drawing, cpFloat x, cpFloat y) { Body_data *pa = cpBodyGetUserData(drawing); int length = pa->x_values->len; cpVect old = cpvzero; old.x = g_array_index(pa->x_values, cpFloat, length - 1); old.y = g_array_index(pa->y_values, cpFloat, length - 1); old = cpBodyLocal2World(drawing, old); cpVect point = cpv(x, y); // if the difference between the new x and the previous x is greater than the threshold if (cpvdistsq(old, point) > THRESHOLD) { cpVect position = cpBodyGetPos(drawing); point = cpvsub(point, position); //cpBodyWorld2Local(drawing, point); fprintf(stdout, "Point= (%5.2f, %5.2f) while x= %5.2f, y=%5.2f\n", point.x, point.y, x, y); x = point.x; y = point.y; g_array_append_val(pa->x_values, x); g_array_append_val(pa->y_values, y); cpBodySetUserData(drawing, pa); drawing = add_segment_to_body(space, drawing); } cpBodySetVel(drawing, cpvzero); return drawing; }
static void add_box(cpSpace *space) { const cpFloat size = 10.0f; const cpFloat mass = 1.0f; cpVect verts[] = { cpv(-size,-size), cpv(-size, size), cpv( size, size), cpv( size,-size), }; cpFloat radius = cpvlength(cpv(size, size)); cpVect pos = rand_pos(radius); cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero))); body->velocity_func = planetGravityVelocityFunc; cpBodySetPos(body, pos); // Set the box's velocity to put it into a circular orbit from its // starting position. cpFloat r = cpvlength(pos); cpFloat v = cpfsqrt(gravityStrength / r) / r; cpBodySetVel(body, cpvmult(cpvperp(pos), v)); // Set the box's angular velocity to match its orbital period and // align its initial angle with its position. cpBodySetAngVel(body, v); cpBodySetAngle(body, cpfatan2(pos.y, pos.x)); cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); }
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 PhysicsBody::setVelocity(const Vec2& velocity) { if (!_dynamic) { CCLOG("physics warning: your can't set velocity for a static body."); return; } cpBodySetVel(_info->getBody(), PhysicsHelper::point2cpv(velocity)); }
void fff::kitty::applyImpulse(float impulse){ cpVect vel = cpBodyGetVel(body); if (impulse < 0.f && vel.y > 0.f){ vel.y = impulse; }else if (impulse < 0.f && vel.y < 0.f){ vel.y += impulse; }else if (impulse > 0.f && vel.y < 0.f){ vel.y = impulse; }else if (impulse > 0.f && vel.y > 0.f){ vel.y += impulse; } cpBodySetVel(body, vel); }
void cpSpaceConvertBodyToStatic(cpSpace *space, cpBody *body) { cpAssertHard(!cpBodyIsStatic(body), "Body is already static."); cpAssertHard(cpBodyIsRogue(body), "Remove the body from the space before calling this function."); cpAssertSpaceUnlocked(space); cpBodySetMass(body, INFINITY); cpBodySetMoment(body, INFINITY); cpBodySetVel(body, cpvzero); cpBodySetAngVel(body, 0.0f); body->node.idleTime = INFINITY; CP_BODY_FOREACH_SHAPE(body, shape){ cpSpatialIndexRemove(space->activeShapes, shape, shape->hashid); cpSpatialIndexInsert(space->staticShapes, shape, shape->hashid); }
int lc_body_newindex(lua_State *vm){ //userdata, key, data const char *key = lua_tostring(vm, 2); cpBody *body = (lc_GetBody(1, vm))->body; if (strcmp("pos", key) == 0 && lua_istable(vm, 3)){ cpBodySetPos(body, lc_TableTocpVect(3, vm)); } else if (strcmp("vel", key) == 0 && lua_istable(vm, 3)){ cpBodySetVel(body, lc_TableTocpVect(3, vm)); } else if (strcmp("angle", key) == 0 || strcmp("radangle", key) == 0){ cpBodySetAngle(body, (cpFloat)lua_tonumber(vm, 3)); } else if (strcmp("degangle", key) == 0){ cpBodySetAngle(body, ( (cpFloat)lua_tonumber(vm, 3)*(cpFloat)M_PI/(cpFloat)180.f ) ); } return 0; }
cpBody* drawing_update(cpSpace *space, cpBody *drawing, cpFloat x, cpFloat y) { Point_array *pa = cpBodyGetUserData(drawing); int length = pa->x_values->len; // if the difference between the new x and the previous x is greater than the threshold if (( abs((SCALE * x) - (SCALE * g_array_index(pa->x_values, cpFloat, length - 1))) >= (SCALE * THRESHOLD) ) || ( abs((SCALE * y) - (SCALE * g_array_index(pa->y_values, cpFloat, length - 1))) >= (SCALE * THRESHOLD) )) { g_array_append_val(pa->x_values, x); g_array_append_val(pa->y_values, y); cpBodySetUserData(drawing, pa); drawing = add_segment_to_body(space, drawing); } cpBodySetVel(drawing, cpvzero); return drawing; }
static void ClipPoly(cpSpace *space, cpShape *shape, cpVect n, cpFloat dist) { cpBody *body = cpShapeGetBody(shape); int count = cpPolyShapeGetNumVerts(shape); int clippedCount = 0; cpVect *clipped = (cpVect *)alloca((count + 1)*sizeof(cpVect)); for(int i=0, j=count-1; i<count; j=i, i++){ cpVect a = cpBodyLocal2World(body, cpPolyShapeGetVert(shape, j)); cpFloat a_dist = cpvdot(a, n) - dist; if(a_dist < 0.0){ clipped[clippedCount] = a; clippedCount++; } cpVect b = cpBodyLocal2World(body, cpPolyShapeGetVert(shape, i)); cpFloat b_dist = cpvdot(b, n) - dist; if(a_dist*b_dist < 0.0f){ cpFloat t = cpfabs(a_dist)/(cpfabs(a_dist) + cpfabs(b_dist)); clipped[clippedCount] = cpvlerp(a, b, t); clippedCount++; } } cpVect centroid = cpCentroidForPoly(clippedCount, clipped); cpFloat mass = cpAreaForPoly(clippedCount, clipped)*DENSITY; cpFloat moment = cpMomentForPoly(mass, clippedCount, clipped, cpvneg(centroid)); cpBody *new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPos(new_body, centroid); cpBodySetVel(new_body, cpBodyGetVelAtWorldPoint(body, centroid)); cpBodySetAngVel(new_body, cpBodyGetAngVel(body)); cpShape *new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, clippedCount, clipped, cpvneg(centroid))); // Copy whatever properties you have set on the original shape that are important cpShapeSetFriction(new_shape, cpShapeGetFriction(shape)); }
void PhysicsBody::setDynamic(bool dynamic) { if (dynamic != _dynamic) { _dynamic = dynamic; if (dynamic) { cpBodySetMass(_info->getBody(), _mass); cpBodySetMoment(_info->getBody(), _moment); if (_world != nullptr) { // reset the gravity enable if (isGravityEnabled()) { _gravityEnabled = false; setGravityEnable(true); } cpSpaceAddBody(_world->_info->getSpace(), _info->getBody()); } } else { if (_world != nullptr) { cpSpaceRemoveBody(_world->_info->getSpace(), _info->getBody()); } // avoid incorrect collion simulation. cpBodySetMass(_info->getBody(), PHYSICS_INFINITY); cpBodySetMoment(_info->getBody(), PHYSICS_INFINITY); cpBodySetVel(_info->getBody(), cpvzero); cpBodySetAngVel(_info->getBody(), 0.0f); resetForces(); } } }
/** * Initialize a bubble that will bounce around. * \param b The bubble to initialize. * \param x The x location of the bubble. * \param y The y location of the bubble. * \param v_x The x velocity of the bubble. * \param v_y The y velocity of the bubble. * \param r The radius of the bubble. * \param l The bubble's life. */ struct bubble *bubble_init(struct bubble *b, double x, double y, double v_x, double v_y, double r, uint8_t l) { cpBody *body = cpBodyInit(&b->body, r, cpMomentForCircle(r, 0, r, cpv(0, 0))); if (!body) { ERR_ERRNO(); return 0; } cpShape *shape = cpCircleShapeInit(&b->shape, body, r, cpv(0,0)); if (!shape) { cpBodyDestroy(body); ERR_ERRNO(); return 0; } cpBodySetVel(body, cpv(v_x, v_y)); cpBodySetPos(body, cpv(x, y)); shape->e = 1.0; shape->data = b; shape->collision_type = BUBBLE; b->l = l; return b; }
void physics_update_all() { PhysicsInfo *info; Entity ent; entitypool_remove_destroyed(pool, physics_remove); entitymap_clear(debug_draw_map); /* simulate */ if (!timing_get_paused()) { _update_kinematics(); _step(); } /* synchronize transform <-> physics */ entitypool_foreach(info, pool) { ent = info->pool_elem.ent; /* if transform is dirtier, move to it, else overwrite it */ if (transform_get_dirty_count(ent) != info->last_dirty_count) { cpBodySetVel(info->body, cpvzero); cpBodySetAngVel(info->body, 0.0f); cpBodySetPos(info->body, cpv_of_vec2(transform_get_position(ent))); cpBodySetAngle(info->body, transform_get_rotation(ent)); cpSpaceReindexShapesForBody(space, info->body); } else if (info->type == PB_DYNAMIC) { transform_set_position(ent, vec2_of_cpv(cpBodyGetPos(info->body))); transform_set_rotation(ent, cpBodyGetAngle(info->body)); } info->last_dirty_count = transform_get_dirty_count(ent); }
void AttackAI(entity_t *ent) { entity_t *temp_ent; vec2_t temp_vec2; cpVect cp_temp; if(!ent->mData || !ent) { printf("MoveAI given a null paramerter \n"); return; } cp_temp = cpvzero; //Standard Vars if(ent->mCollisionType != COLLISION_TYPE_RAGDOLL) { ent->mCollisionType = COLLISION_TYPE_RAGDOLL; SetCpCollisionType(ent); } if(ent->mData->mObject) { temp_ent = FindCachedEntity(ent->mData->mObject); if(temp_ent) { 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); AddPhyicsToEntity(temp_ent); cpBodySetVel(ent->mPhysicsProperties->body, cp_temp); Spawn(ent, &temp_ent, NULL); } } StandarAI_Think(ent); }
static cpSpace * init(void) { cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 30); cpSpaceSetGravity(space, cpv(0, -100)); cpSpaceSetSleepTimeThreshold(space, 0.5f); cpBody *body, *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // Create segments around the edge of the screen. 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(-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); cpFloat mass = 1; cpFloat width = 20; cpFloat height = 30; cpFloat spacing = width*0.3; // Add lots of boxes. for(int i=0; i<CHAIN_COUNT; i++){ cpBody *prev = NULL; for(int j=0; j<LINK_COUNT; j++){ cpVect pos = cpv(40*(i - (CHAIN_COUNT - 1)/2.0), 240 - (j + 0.5)*height - (j + 1)*spacing); body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); cpBodySetPos(body, pos); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height)); cpShapeSetFriction(shape, 0.8f); cpFloat breakingForce = 80000; cpConstraint *constraint = NULL; if(prev == NULL){ constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, staticBody, cpv(0, height/2), cpv(pos.x, 240), 0, spacing)); } else { constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, prev, cpv(0, height/2), cpv(0, -height/2), 0, spacing)); } cpConstraintSetMaxForce(constraint, breakingForce); cpConstraintSetPostSolveFunc(constraint, BreakableJointPostSolve); prev = body; } } cpFloat radius = 15.0f; body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero))); cpBodySetPos(body, cpv(0, -240 + radius+5)); cpBodySetVel(body, cpv(0, 300)); shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.9f); return space; }
void cBody::Vel( const cVect& vel ) { cpBodySetVel( mBody, tocpv( vel ) ); }
void Body::setVel(const cp::Vect& value) { cpBodySetVel(body,value); }
void fff::kitty::setInitialFallingSpeed(float speed){ cpVect vel = {0, KMH_TO_PXS(speed)}; cpBodySetVel(body, vel); }
void worldEnt_setVelocity(WorldEntity_t *aEntity, vec2_t aVelocity) { cpBodySetVel(aEntity->cpBody, VEC2_TO_CPV(aVelocity)); }
void PhysicsBody::setVelocity(Point velocity) { cpBodySetVel(_info->body, PhysicsHelper::point2cpv(velocity)); }
void physics_set_velocity(Entity ent, Vec2 vel) { PhysicsInfo *info = entitypool_get(pool, ent); error_assert(info); cpBodySetVel(info->body, cpv_of_vec2(vel)); }