static cpSpace * init(void) { QUERY_START = cpvzero; space = cpSpaceNew(); cpSpaceSetIterations(space, 5); { // add a fat segment cpFloat mass = 1.0f; cpFloat length = 100.0f; cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f); cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b))); cpBodySetPos(body, cpv(0.0f, 100.0f)); cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 20.0f)); } { // add a static segment cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(0, 300), cpv(300, 0), 0.0f)); } { // add a pentagon cpFloat mass = 1.0f; const int NUM_VERTS = 5; cpVect verts[NUM_VERTS]; for(int i=0; i<NUM_VERTS; i++){ cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS); verts[i] = cpv(30*cos(angle), 30*sin(angle)); } cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero))); cpBodySetPos(body, cpv(50.0f, 50.0f)); cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero)); } { // add a circle cpFloat mass = 1.0f; cpFloat r = 20.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, r, cpvzero))); cpBodySetPos(body, cpv(100.0f, 100.0f)); cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero)); } return space; }
__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; } }
void CDynamics2DMultiBodyObjectModel::MoveTo(const CVector3& c_position, const CQuaternion& c_orientation) { /* Set target position and orientation */ cpVect tBodyPos = cpv(c_position.GetX(), c_position.GetY()); CRadians cXAngle, cYAngle, cZAngle; c_orientation.ToEulerAngles(cZAngle, cYAngle, cXAngle); cpFloat tBodyOrient = cZAngle.GetValue(); /* For each body: */ for(size_t i = 0; i < m_vecBodies.size(); ++i) { /* Set body orientation at anchor */ cpBodySetAngle(m_vecBodies[i].Body, tBodyOrient + m_vecBodies[i].OffsetOrient); /* Set body position at anchor */ cpBodySetPos(m_vecBodies[i].Body, cpvadd(tBodyPos, cpvrotate(m_vecBodies[i].OffsetPos, m_vecBodies[i].Body->rot))); /* Update shape index */ cpSpaceReindexShapesForBody(GetDynamics2DEngine().GetPhysicsSpace(), m_vecBodies[i].Body); } /* Update ARGoS entity state */ UpdateEntityStatus(); }
MachineSystem::MachineSystem(int width, int height, int hPegs, int vPegs, cpVect position) : parts(hPegs*vPegs), attachments((hPegs*vPegs), std::vector<Attachment *>(hPegs*vPegs, NULL)), space(cpSpaceNew()), nMachines(0), nAttachments(0) { cpSpaceSetIterations(space, 20); gridSpacing = cpv((float)width/(hPegs + 1), (float)height/(vPegs + 1)); size = cpv(hPegs, vPegs); body = cpBodyNewStatic(); cpBodySetPos(body, position); cpShape *wallShape = cpSegmentShapeNew(body, cpv(-width/2, height/2), cpv(width/2, height/2), .5); cpShapeSetLayers(wallShape, WALL_LAYER); cpSpaceAddStaticShape(space, wallShape); wallShape = cpSegmentShapeNew(body, cpv(-width/2, -height/2), cpv(+width/2, -height/2), 0.5); cpShapeSetLayers(wallShape, WALL_LAYER); cpSpaceAddStaticShape(space, wallShape); wallShape = cpSegmentShapeNew(body, cpv(-width/2, +height/2), cpv(-width/2, -height/2), 0.5); cpShapeSetLayers(wallShape, WALL_LAYER); cpSpaceAddStaticShape(space, wallShape); wallShape = cpSegmentShapeNew(body, cpv(+width/2, +height/2), cpv(+width/2, -height/2), 0.5); cpShapeSetLayers(wallShape, WALL_LAYER); cpSpaceAddStaticShape(space, wallShape); inputMachinePosition = cpv(-1,-1); outputMachinePosition = cpv(-1,-1); }
void PhysicsBody::setPosition(Point position) { if (!_positionResetTag) { cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position + _positionOffset)); } }
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); }
static void do_logic(game_state *state) { switch_state_data *data = state->data->switch_data; if (data->mouse_down_last_step && state->game->mouse_down == false) { /* clicked and released; create a circle */ cpFloat radius = 15; cpFloat mass = 10; cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); cpBody *ball = cpSpaceAddBody(data->space, cpBodyNew(mass, moment)); cpBodySetPos(ball, state->game->mouse_pos); cpShape *ball_shape = cpSpaceAddShape(data->space, cpCircleShapeNew(ball, radius, cpvzero)); cpShapeSetElasticity(ball_shape, 0.7); cpShapeSetFriction(ball_shape, 0.7); cpShapeSetLayers(ball_shape, L_PLAYER); } cpSpaceStep(data->space, TARGET_SEC_PER_FRAME); data->mouse_down_last_step = state->game->mouse_down; if (ent_switch_get_state(data->sw)) debug_puts("switch is on"); else debug_puts("switch is off"); }
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)); } } } }
void physics_add(Entity ent) { PhysicsInfo *info; if (entitypool_get(pool, ent)) return; /* already has physics */ transform_add(ent); info = entitypool_add(pool, ent); info->mass = 1.0; info->type = PB_DYNAMIC; /* create, init cpBody */ info->body = cpSpaceAddBody(space, cpBodyNew(info->mass, 1.0)); cpBodySetUserData(info->body, ent); /* for cpBody -> Entity mapping */ cpBodySetPos(info->body, cpv_of_vec2(transform_get_position(ent))); cpBodySetAngle(info->body, transform_get_rotation(ent)); info->last_dirty_count = transform_get_dirty_count(ent); /* initially no shapes */ info->shapes = array_new(ShapeInfo); /* initialize last_pos/last_ang info for kinematic bodies */ info->last_pos = cpBodyGetPos(info->body); info->last_ang = cpBodyGetAngle(info->body); info->collisions = NULL; }
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; } }
/** * Initializes a new player. * The player is placed in the queue of available players. */ struct player *player_init(struct player *p, struct game *g, double x, double y, double w, double h, uint32_t score, uint8_t data) { cpVect all[4] = {cpv(0,0), cpv(0,h), cpv(w,h), cpv(w,0)}; cpBody *body = cpBodyInit(&p->body, 10, cpMomentForBox(10, w, h)); if (!body) { ERR_ERRNO(); return 0; } cpBodySetPos(body, cpv(x,y)); //cpShape *shape = cpPolyShapeNew(body,4,all,cpv((p->l+p->r)/2.0,(p->b+p->t)/2.0)); cpShape *shape = cpPolyShapeInit(&p->shape, body, 4, all, cpv(0, 0)); if (!shape) { ERR_ERRNO(); cpBodyDestroy(body); return 0; } shape->data = p; shape->collision_type = PLAYER; if (linkedlist_add_last(g->p_q, p)) { ERR_TRACE(); cpBodyDestroy(body); cpShapeDestroy(shape); return 0; } p->x = x; p->y = y; p->node = g->p_q->last; return p; }
cpBody* createNewBodyFrom(cpSpace* space, cpFloat xPos, cpFloat yPos, cpFloat mass, cpFloat moment, JSObject* outterJsObj) { cpBody* circularBody = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPos(circularBody, cpv(xPos, yPos)); cpBodySetUserData(circularBody, outterJsObj); return circularBody; }
void Unit::setUnitPos( float x, float y ) { cpVect v = {x, y}; cpBodySetPos( physBody, v ); Image.setPosition( x, y, Z ); if( cpBodyIsStatic(physBody) && phys.type != potNone ) cpSpaceReindexShape( Phys::space, physShape ); }
static cpSpace * init(void) { space = cpSpaceNew(); cpSpaceSetGravity(space, cpv(0, -600)); cpBody *body; cpShape *shape; // We create an infinite mass rogue body to attach the line segments too // This way we can control the rotation however we want. rogueBoxBody = cpBodyNew(INFINITY, INFINITY); cpBodySetAngVel(rogueBoxBody, 0.4f); // Set up the static box. cpVect a = cpv(-200, -200); cpVect b = cpv(-200, 200); cpVect c = cpv( 200, 200); cpVect d = cpv( 200, -200); shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, a, b, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, b, c, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, c, d, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, d, a, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpFloat mass = 1; cpFloat width = 60; cpFloat height = 30; // Add the bricks. for(int i=0; i<3; i++){ for(int j=0; j<7; j++){ body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); cpBodySetPos(body, cpv(i*60 - 150, j*30 - 150)); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); } } return space; }
// Iterate over all of the bodies and reset the ones that have fallen offscreen. static void eachBody(cpBody *body, void *unused) { cpVect pos = cpBodyGetPos(body); if(pos.y < -260 || cpfabs(pos.x) > 340){ cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; cpBodySetPos(body, cpv(x, 260)); } }
// adds a freestyle shape given a pointer to a an array of vertices and the number of vertices //returns a cpbody due to the many shapes associated with one body cpBody *core_add_freestyle_shape ( cpSpace * space, cpVect* verts , const int num_verts, Color *color, const double friction, const double elasticity, const double density, const int index ) { if ( num_verts <= 1 ) return NULL; // first determine center of mass of object cpVect center; core_freestyle_center ( verts, num_verts, ¢er ); // calculate mass and moment cpFloat mass = core_freestyle_mass ( verts, num_verts, density ); // dummy moment calculation cpFloat moment = core_freestyle_moment ( verts, num_verts, center, density ); cpBody *body = cpBodyNew ( mass, moment ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc)postStepAddBody, body, NULL ); cpBodySetPos ( body, center ); // set index of body // add body info to body BodyInfo * bi = body_info_new(num_verts); bi->index = index; bi->type = FREESTYLE_TYPE; bi->num_verts = num_verts; for ( int i = 0; i < num_verts; i++ ) { (bi->verts[i]).x = verts[i].x; (bi->verts[i]).y = verts[i].y; } bi->color->r = color->r; bi->color->g = color->g; bi->color->b = color->b; bi->friction = friction; bi->density = density; bi->elasticity = elasticity; body->data = bi; // add line segment collision shapes to body for ( int i = 0; i < num_verts - 1; i++ ) { cpVect offset_a = cpvmult ( cpvsub ( verts[i], center), 1.0 ); cpVect offset_b = cpvmult ( cpvsub ( verts[i+1], center), 1.0 ); cpShape * segment = cpSegmentShapeNew ( body, offset_a, offset_b, 0.1 ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, segment, NULL); segment->u = friction; DrawShapeInfo *info = draw_shape_info_new (); info->color->r = color->r; info->color->g = color->g; info->color->b = color->b; segment->data = ( cpDataPointer ) info; } return body; }
cpBody* drawing_new(cpFloat x, cpFloat y, long color_rgb) { cpBody *drawing = cpBodyNew(INITIAL, INITIAL); cpBodySetPos(drawing, cpv(x, y)); Point_array *pa = point_array_new(x, y, color_rgb); cpBodySetUserData(drawing, pa); return drawing; }
void PhysicsBody::setPosition(float positionX, float positionY) { cpVect tt; tt.x = positionX + _positionOffset.x; tt.y = positionY + _positionOffset.y; cpBodySetPos(_cpBody, tt); }
void Player::Initialize(void) { // Load the texture if (!(this->m_Texture = this->m_Hge->Texture_Load("data/particles.png"))) { MessageBox(NULL, "Can't load particles.png texture.", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL); this->m_Engine->Shutdown(); return; } // Create the player sprite this->m_Sprite = new hgeSprite(this->m_Texture, 60, 40, 20, 20); this->m_Sprite->SetColor(this->m_Color); this->m_Sprite->SetHotSpot(10, 10); // Create the particle sprite this->m_ParticleSprite = new hgeSprite(this->m_Texture, 20, 20, 20, 20); this->m_ParticleSprite->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE); this->m_ParticleSprite->SetHotSpot(10, 10); // Create the particle system this->m_ParticleSystem = new hgeParticleSystem("data/trail.psi", this->m_ParticleSprite); this->m_ParticleSystem->Fire(); // Initialize the weapon slots this->m_Weaponslots = (Weapon**) malloc(sizeof(Weapon*) * NUM_WEAPON_SLOTS); for (int i = 0 ; i < NUM_WEAPON_SLOTS ; ++i) { this->m_Weaponslots[i] = 0; } this->m_Weaponslots[0] = new Neoshooter(this, this->m_Engine->GetWorld()); // Initialize physics { // Define moment cpFloat moment = cpMomentForCircle(1, 0, 10, cpvzero); // Fetch the physics space cpSpace *space = this->m_Engine->GetWorld()->GetSpace(); // Add the physics body this->m_Body = cpSpaceAddBody(space, cpBodyNew(1, moment)); cpBodySetPos(this->m_Body, this->m_Position); // Add the physics shape this->m_Shape = cpSpaceAddShape(space, cpCircleShapeNew(this->m_Body, 10, cpvzero)); this->m_Shape->data = this; this->m_Shape->collision_type = COLLISION_TYPE_GO; cpShapeSetElasticity(this->m_Shape, 1.0f); cpShapeSetFriction(this->m_Shape, 0.7); // Define the collision handlers for various types of collisions cpSpaceAddCollisionHandler(space, COLLISION_TYPE_GO, COLLISION_TYPE_ACTIVATOR, BeginCollisionD, NULL, NULL, SeparateCollisionD, this); cpSpaceAddCollisionHandler(space, COLLISION_TYPE_GO, COLLISION_TYPE_BULLET, BeginCollisionD, NULL, NULL, NULL, this); } }
cpBody* PhysicsWorld::AddRectBody(cpFloat width, cpFloat height, cpFloat xPosition, cpFloat yPosition, cpFloat mass) { // Create and add the body cpBody *boxBody = cpSpaceAddBody(mySpace, cpBodyNew(mass, cpMomentForBox(mass, width, height))); // Set it's position in the world cpBodySetPos(boxBody, cpv(xPosition, yPosition)); return boxBody; }
void LinkerAgent::UpdateFromEngine() { DJVector2 djPosition = m_pSprite->GetPosition(); float djAngle = m_pSprite->GetRotation(); cpVect cpPosition; cpPosition.x = (float) djPosition.x(); cpPosition.y = (float) -djPosition.y(); cpBodySetAngle(m_pBody, djAngle); cpBodySetPos(m_pBody, cpPosition); }
static void make_leg(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))); cpBodySetPos(upper_leg, cpv(offset, 0.0f)); cpSpaceAddShape(space, cpSegmentShapeNew(upper_leg, a, b, seg_radius)); 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))); cpBodySetPos(lower_leg, cpv(offset, -side)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(lower_leg, a, b, seg_radius)); cpShapeSetGroup(shape, 1); shape = cpSpaceAddShape(space, cpCircleShapeNew(lower_leg, seg_radius*2.0f, b)); cpShapeSetGroup(shape, 1); 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); }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Right click to make pentagons static/dynamic."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 5); cpSpaceSetGravity(space, cpv(0, -100)); cpBody *body, *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // Vertexes for a triangle shape. cpVect tris[] = { cpv(-15,-15), cpv( 0, 10), cpv( 15,-15), }; // Create the static triangles. for(int i=0; i<9; i++){ for(int j=0; j<6; j++){ cpFloat stagger = (j%2)*40; cpVect offset = cpv(i*80 - 320 + stagger, j*70 - 240); shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris, offset)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); } } // Create vertexes for a pentagon shape. cpVect verts[NUM_VERTS]; for(int i=0; i<NUM_VERTS; i++){ cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS); verts[i] = cpv(10*cos(angle), 10*sin(angle)); } pentagon_mass = 1.0; pentagon_moment = cpMomentForPoly(1.0f, NUM_VERTS, verts, cpvzero); // Add lots of pentagons. for(int i=0; i<300; i++){ body = cpSpaceAddBody(space, cpBodyNew(pentagon_mass, pentagon_moment)); cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; cpBodySetPos(body, cpv(x, 350)); shape = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.4f); } return space; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Sticky collisions using the cpArbiter data pointer."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -1000)); cpSpaceSetCollisionSlop(space, 2.0); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // Create segments around the edge of the screen. shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv(-340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv( 340,-260), cpv( 340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv( 340,-260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340, 260), cpv( 340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); for(int i=0; i<200; i++){ cpFloat mass = 0.15f; cpFloat radius = 10.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); cpBodySetPos(body, cpv(cpflerp(-150.0f, 150.0f, frand()), cpflerp(-150.0f, 150.0f, frand()))); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius + STICK_SENSOR_THICKNESS, cpvzero)); cpShapeSetFriction(shape, 0.9f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); } cpSpaceAddCollisionHandler(space, COLLIDE_STICK_SENSOR, COLLIDE_STICK_SENSOR, NULL, StickyPreSolve, NULL, StickySeparate, NULL); return space; }
cpBody* drawing_new(cpFloat x, cpFloat y, long color_rgb) { cpBody *drawing = cpBodyNew(INITIAL, INITIAL); cpVect clickpoint = cpv(x, y); cpBodySetPos(drawing, clickpoint); cpVect position = cpBodyGetPos(drawing); fprintf(stdout, "INITIAL POSITION AT: (%5.2f, %5.2f)\n\n", position.x, position.y); Body_data *pa = point_array_new(0, 0, color_rgb); cpBodySetUserData(drawing, pa); return drawing; }
static cpBody * add_ball(cpVect pos) { cpBody *body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 30, 0, cpvzero))); cpBodySetPos(body, pos); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, 30, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); return body; }
static cpBody *utils_update_drawing(cpBody *drawing) { cpFloat mass = cpBodyGetMass(drawing); cpFloat moment = cpBodyGetMoment(drawing); Body_data *pa = cpBodyGetUserData(drawing); //cpFloat x = g_array_index(pa->x_values, cpFloat, 0); //cpFloat y = g_array_index(pa->y_values, cpFloat, 0); cpVect pos_a, pos_b; cpVect origin = cpBodyGetPos(drawing); cpFloat mi, micx = 0, micy = 0; int length = pa->x_values->len; for (int index = 1; index < length; index++) { pos_a = cpv(g_array_index(pa->x_values, cpFloat, index - 1), g_array_index(pa->y_values, cpFloat, index - 1)); pos_b = cpv(g_array_index(pa->x_values, cpFloat, index), g_array_index(pa->y_values, cpFloat, index)); //fprintf(stdout, "Pos_a = (%5.2f, %5.2f)\n", pos_a.x, pos_a.y); mi = (CRAYON_MASS * cpAreaForSegment( pos_a, pos_b, CRAYON_RADIUS )); micx += mi * ((pos_a.x + pos_b.x) / 2); micy += mi * ((pos_a.y + pos_b.y) / 2); mass += mi; moment += cpMomentForSegment(mass, pos_a, pos_b); // not actually sum, but maybe it is } cpBodySetMass(drawing, mass); cpBodySetMoment(drawing, moment); // center of mass is the average of all vertices NOT //cpVect new_origin = cpv(x / length, y / length); cpVect new_origin = cpv(micx / mass, micy / mass); new_origin = cpBodyLocal2World(drawing, new_origin); cpBodySetPos( drawing, new_origin ); //fprintf(stdout, "Position set at (%5.2f, %5.2f)\n", new_origin.x, new_origin.y); cpSpace * space = cpBodyGetSpace(drawing); cpSpaceReindexShapesForBody(space, drawing); //cpBodySetPos(drawing, cpv(pos.x + (second.x / length), pos.y + (second.y / length))); //pa->offset = cpvsub(new_origin, origin); pa = shift_origin(drawing, origin, new_origin); cpBodySetUserData(drawing, pa); if (space) post_step_body_replace_shapes(space, drawing, NULL); else fprintf(stderr, "WTF\n"); //if (!(cpSpaceAddPostStepCallback(space, (cpPostStepFunc) post_step_body_replace_shapes, drawing, NULL))) //fprintf(stderr, "FAILED POST-STEP CALLBACK\n\n"); return drawing; }
void PhysicsSprite::setPosition(const Vec2 &pos) { #if CC_ENABLE_CHIPMUNK_INTEGRATION cpVect cpPos = cpv(pos.x, pos.y); cpBodySetPos(_CPBody, cpPos); #elif CC_ENABLE_BOX2D_INTEGRATION float angle = _pB2Body->GetAngle(); _pB2Body->SetTransform(b2Vec2(pos.x / _PTMRatio, pos.y / _PTMRatio), angle); #endif }
// adds nonstatic box shape to space // currently does not roate box static cpShape *core_add_box_shape ( cpSpace *space, Box *box, const int index ) { // calculate mass and moment of a box cpFloat mass = box->density * box->width * box->height; cpFloat moment = cpMomentForBox ( mass, box->width, box->height ); // add body with mass and moment of a square to space cpBody *body = cpBodyNew ( mass, moment ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddBody, body, NULL ); cpBodySetPos ( body, cpv ( box->x, box->y ) ); // set index of body BodyInfo * bi = body_info_new(0); bi->index = index; bi->type = BOX_TYPE; bi->p1x = box->x - (box->width) / 2.0; bi->p1y = box->y + (box->height) / 2.0; bi->p2x = box->x + (box->width) / 2.0;; bi->p2y = box->y - (box->height) / 2.0; bi->color->r = box->color->r; bi->color->g = box->color->g; bi->color->b = box->color->b; bi->friction = box->friction; bi->density = box->density; bi->elasticity = box->elasticity; body->data = bi; double hw = ( box->width ) / 2.0; double hh = ( box->height ) / 2.0; cpVect cpv1 = cpv ( -hw,-hh ); cpVect cpv2 = cpv ( -hw, hh ); cpVect cpv3 = cpv ( hw, hh ); cpVect cpv4 = cpv ( hw, -hh ); cpVect verts [4]= { cpv1, cpv2, cpv3, cpv4 }; // add box collision shape to body cpShape *boxShape = cpPolyShapeNew ( body, 4, verts, cpv ( 0, 0 ) ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, boxShape, NULL ); cpShapeSetFriction ( boxShape, box->friction ); cpShapeSetElasticity ( boxShape, box->elasticity ); cpBodySetAngle ( body, box->angle); DrawShapeInfo *info = add_box_draw_shape_info ( box ); boxShape->data= ( cpDataPointer ) info; return boxShape; }
static void add_domino(cpSpace *space, cpVect pos, cpBool flipped) { cpFloat mass = 1.0f; cpFloat moment = cpMomentForBox(mass, WIDTH, HEIGHT); cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPos(body, pos); cpShape *shape = (flipped ? cpBoxShapeNew(body, HEIGHT, WIDTH) : cpBoxShapeNew(body, WIDTH, HEIGHT)); cpSpaceAddShape(space, shape); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.6f); }