// 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); } } }
void draw_shape(cpBody* body, cpShape* shape, void* data) { // get body info cpVect v = cpBodyGetPos(body); cpFloat angle = cpBodyGetAngle(body); cpVect rot = cpvforangle(angle); // get vectors int n = cpPolyShapeGetNumVerts(shape); SDL_Point* pts = calloc(sizeof(SDL_Point), n+1); // rotate vectors int i; for(i=0; i<n; i++) { cpVect p = cpPolyShapeGetVert(shape, i); cpVect vr = cpvrotate(cpv(p.x,p.y), rot); pts[i] = (SDL_Point) { (vr.x+v.x)*10+50, (vr.y+v.y)*10+50 }; if(i == 0) pts[n] = pts[i]; } // draw SDL_RenderDrawLines(ren, pts, n+1); free(pts); }
int draw_particles(void) { static Vec pointv[512]; static size_t pointc = ARRLEN(pointv, Vec); double z = zoom(); particle_t* p = particles; int i = 0; size_t count = HASH_COUNT(particles); while(i < count) { for(; p != NULL && i < pointc; p = p->hh.next, i++) { cpVect pos = cpBodyGetPos(p->body); if(fabs(pos.x) <= z / 2 && fabs(pos.y) <= z / 2) { // rough check if position is inside screen borders // TODO: possibly make it separate function pointv[i] = cpv2vec(pos); } } if(draw_points(pointv, i % pointc)) { return -1; } } return 0; }
__declspec( dllexport ) void pullall( const void * _in, int in_size, void * _out, int out_sz ) { int i; cpBody *body; Variable *var; //int bodyCount = 0; int index = 0; for (i = 0;i != mVariableHandler.mSize;i++) { var = mVariableHandler.mPtrArray[i]; if (var == NULL)continue; //Varmistetaan, että haetaan vain runkojen tiedot if (var->mType != VarTypeBody)continue; body = (cpBody*)var->mPtr; //Eikä staattisen tietoja if (cpBodyIsStatic(body))continue; //Alkuun cb:n muistipalan osoite POKEINT(OUTPUT_MEMBLOCK,index,var->mCBPtr); POKEFLOAT(OUTPUT_MEMBLOCK,index+4,radToDeg(cpBodyGetAngle(body))); POKEVECT(OUTPUT_MEMBLOCK,index+8,cpBodyGetPos(body)); POKEFLOAT(OUTPUT_MEMBLOCK,index+16,radToDeg(cpBodyGetAngVel(body))); POKEVECT(OUTPUT_MEMBLOCK,index+20,cpBodyGetVel(body)); index += 28; //bodyCount++; } //POKEINT(OUTPUT_MEMBLOCK,0,bodyCount); }
void Player::Update(float dt) { if (this->m_Dead) { return; } // Fix the velocity this->m_Body->v = cpvmult(this->m_Body->v, this->m_Speed / cpvdist(cpVect(), this->m_Body->v)); // Force the player in the upward direction if (this->m_Body->v.y > 0.0f) { this->m_Body->v = cpvneg(this->m_Body->v); } // Update the position cpVect bv(cpBodyGetPos(this->m_Body)); this->m_Position.x = bv.x; this->m_Position.y = bv.y; // Set the steering value this->m_Body->v.x = -this->m_SteeringValue * 5.0f * dt; //#ifdef true int KEY_MAP[5][3] = { {HGEK_LEFT, HGEK_RIGHT, HGEK_O}, {HGEK_A, HGEK_S, HGEK_D} }; // Process keys if (this->m_Hge->Input_GetKeyState(HGEK_ESCAPE)) { this->m_Engine->Shutdown(); } if (this->m_Hge->Input_GetKeyState(KEY_MAP[this->m_PlayerIndex][0])) { this->m_Body->v.x -= SPEED * dt; } if (this->m_Hge->Input_GetKeyState(KEY_MAP[this->m_PlayerIndex][1])) { this->m_Body->v.x += SPEED * dt; } if (this->m_Hge->Input_GetKeyState(KEY_MAP[this->m_PlayerIndex][2])) { this->CycleWeapons(); } //#endif // Do some movement calculations and collision detection this->m_Body->v.x *= FRICTION; this->m_Body->v.y *= FRICTION; // Update particle system float a = abs(this->m_Body->v.x) * 50.0f; float b = abs(this->m_Body->v.y) * 50.0f; this->m_ParticleSystem->info.nEmission = (int) (a * a + b * b); this->m_ParticleSystem->MoveTo(this->m_Position.x, this->m_Position.y); this->m_ParticleSystem->Update(dt); }
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 LinkerAgent::UpdateFromChipmunk() { float angle = (float)cpBodyGetAngle(m_pBody); cpVect cpPosition = cpBodyGetPos(m_pBody); m_pSprite->SetPosition((float)cpPosition.x, (float)-cpPosition.y); m_pSprite->SetRotation(angle); }
static void _set_type(PhysicsInfo *info, PhysicsBody type) { if (info->type == type) return; /* already set */ info->type = type; switch (type) { case PB_KINEMATIC: info->last_pos = cpBodyGetPos(info->body); info->last_ang = cpBodyGetAngle(info->body); /* fall through */ case PB_STATIC: if (!cpBodyIsStatic(info->body)) { cpSpaceRemoveBody(space, info->body); cpSpaceConvertBodyToStatic(space, info->body); } break; case PB_DYNAMIC: cpSpaceConvertBodyToDynamic(space, info->body, info->mass, 1.0); cpSpaceAddBody(space, info->body); _recalculate_moment(info); break; } }
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; }
void ws::game::Scene::draw(sf::RenderWindow& window) { ws::components::Renderable::setGlobalRenderTarget(&window); auto groundPos = cpBodyGetPos(cpShapeGetBody(ground)); auto rect = sf::RectangleShape(sf::Vector2f(400, 10)); //std::cout << "Ground Position [" << groundPos.x << "," << groundPos.y << "]" << std::endl; cpVect screenSize = cpv(800,600); cpVect screenPos = groundPos; //screenPos = cpvsub(cpvmult(screenSize, 0.5), groundPos); rect.setPosition(sf::Vector2f(screenSize.x - screenPos.x-200, screenSize.y - screenPos.y)); rect.setFillColor(sf::Color::Green); window.draw(rect); // Renderable for( auto object : objects ) { auto renderables = object->getAllComponentsOfType<ws::components::Renderable>(); for( auto renderable : renderables ) { if( renderable != nullptr ) { renderable->render(); } } } }
/** * Pop the bubble splitting it into two bubbles. * No children are created if the bubble was too small. */ int bubble_pop(struct bubble *b, struct game *g) { if (b->l <= 1) { bubble_destroy(b); return 0; } double radius = cpCircleShapeGetRadius(&b->shape); cpVect pos = cpBodyGetPos(&b->body); cpVect vel = cpBodyGetVel(&b->body); bubble_remove(b, g); bubble_destroy(b); b = bubble_init(b, pos.x, pos.y, vel.x + 5, vel.y + 5, radius / 2, b->l - 1); if (!b) { ERR_TRACE(); return -1; } //b->events = b->events; if (bubble_add(b, g)) { ERR_TRACE(); return -1; } struct bubble *c = bubble_create(pos.x, pos.y, vel.x - 5, vel.y + 5, radius / 2, b->l - 1); if (!c) { ERR_TRACE(); return -1; } //c->events = b->events; if (bubble_add(c, g)) { ERR_TRACE(); return -1; } return 0; }
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; }
__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)); } }
//Hakee yhden muuttujan uudet arvot __declspec( dllexport ) void pull( 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; POKEFLOAT(OUTPUT_MEMBLOCK,0,radToDeg(cpBodyGetAngle(body))); POKEVECT(OUTPUT_MEMBLOCK,4,cpBodyGetPos(body)); POKEFLOAT(OUTPUT_MEMBLOCK,12,radToDeg(cpBodyGetAngVel(body))); POKEVECT(OUTPUT_MEMBLOCK,16,cpBodyGetVel(body)); break; case VarTypeShape: shape = (cpShape*)var->mPtr; POKEINT(OUTPUT_MEMBLOCK,0,VarTypeShape); default: MessageBoxA(NULL,"cpPull: Invalid variable type","cpChipmunk error",MB_OK); exit(0); break; } }
void PhysicsDebugDraw::drawShape(PhysicsShape& shape) { const Color4F fillColor(1.0f, 0.0f, 0.0f, 0.3f); const Color4F outlineColor(1.0f, 0.0f, 0.0f, 1.0f); for (auto it = shape._cpShapes.begin(); it != shape._cpShapes.end(); ++it) { cpShape *subShape = *it; switch ((*it)->klass_private->type) { case CP_CIRCLE_SHAPE: { float radius = PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(subShape)); Vec2 centre = PhysicsHelper::cpv2point(cpBodyGetPos(cpShapeGetBody(subShape))); Vec2 offset = PhysicsHelper::cpv2point(cpCircleShapeGetOffset(subShape)); Vec2 rotation(PhysicsHelper::cpv2point(cpBodyGetRot(cpShapeGetBody(subShape)))); centre += offset.rotate(rotation); static const int CIRCLE_SEG_NUM = 12; Vec2 seg[CIRCLE_SEG_NUM] = {}; for (int i = 0; i < CIRCLE_SEG_NUM; ++i) { float angle = (float)i * M_PI / (float)CIRCLE_SEG_NUM * 2.0f; Vec2 d(radius * cosf(angle), radius * sinf(angle)); seg[i] = centre + d; } _drawNode->drawPolygon(seg, CIRCLE_SEG_NUM, fillColor, 1, outlineColor); break; } case CP_SEGMENT_SHAPE: { cpSegmentShape *seg = (cpSegmentShape *)subShape; _drawNode->drawSegment(PhysicsHelper::cpv2point(seg->ta), PhysicsHelper::cpv2point(seg->tb), PhysicsHelper::cpfloat2float(seg->r==0 ? 1 : seg->r), outlineColor); break; } case CP_POLY_SHAPE: { cpPolyShape* poly = (cpPolyShape*)subShape; int num = poly->numVerts; Vec2* seg = new (std::nothrow) Vec2[num]; PhysicsHelper::cpvs2points(poly->tVerts, seg, num); _drawNode->drawPolygon(seg, num, fillColor, 1.0f, outlineColor); delete[] seg; break; } default: break; } } }
// 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)); } }
static void print_positions(cpBody* drawing) { cpVect pos = cpBodyGetPos(drawing); printf("Center of mass (%2.2f, %2.2f)\n", pos.x, pos.y); Body_data *pa = (Body_data *) cpBodyGetUserData(drawing); for (int i = 0; i < pa->x_values->len; i++) { printf("Segment %d starts at (%2.2f, %2.2f)\n", i, g_array_index(pa->x_values, cpFloat, i), g_array_index(pa->y_values, cpFloat, i)); } }
BOOL debugMessageBox(cpBody *body,const char* head) { char str[100]; cpVect pos; pos = cpBodyGetPos(body); sprintf(str,"Pos:(%f,%f) Angle: %f \0",pos.x,pos.y,cpBodyGetAngle(body)); if (MessageBoxA(NULL,str,head,MB_OKCANCEL) == IDOK) return TRUE; return FALSE; }
//destroy bodies out of bound static void core_destroy_out_bodies ( cpBody *body, void *data ) { cpVect pos = cpBodyGetPos ( body ); if ( pos.y < -50 || pos.y > (CORE_MAX_HEIGHT + 50) || pos.x < - 50 || pos.x > 200) { cpSpaceAddPostStepCallback( cpBodyGetSpace(body), (cpPostStepFunc)postStepRemoveBody, body, NULL ); } else return; }
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 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 = cpBodyGetPos(body); cpFloat sqdist = cpvlengthsq(p); cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist))); cpBodyUpdateVelocity(body, g, damping, dt); }
void PhysicsDebugDraw::drawShape(PhysicsShape& shape) { for (auto it = shape._info->getShapes().begin(); it != shape._info->getShapes().end(); ++it) { cpShape *subShape = *it; switch ((*it)->klass_private->type) { case CP_CIRCLE_SHAPE: { float radius = PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(subShape)); Point centre = PhysicsHelper::cpv2point(cpBodyGetPos(cpShapeGetBody(subShape))) + PhysicsHelper::cpv2point(cpCircleShapeGetOffset(subShape)); static const int CIRCLE_SEG_NUM = 12; Point seg[CIRCLE_SEG_NUM] = {}; for (int i = 0; i < CIRCLE_SEG_NUM; ++i) { float angle = (float)i * M_PI / (float)CIRCLE_SEG_NUM * 2.0f; Point d(radius * cosf(angle), radius * sinf(angle)); seg[i] = centre + d; } _drawNode->drawPolygon(seg, CIRCLE_SEG_NUM, Color4F(1.0f, 0.0f, 0.0f, 0.3f), 1, Color4F(1, 0, 0, 1)); break; } case CP_SEGMENT_SHAPE: { cpSegmentShape *seg = (cpSegmentShape *)subShape; _drawNode->drawSegment(PhysicsHelper::cpv2point(seg->ta), PhysicsHelper::cpv2point(seg->tb), PhysicsHelper::cpfloat2float(seg->r==0 ? 1 : seg->r), Color4F(1, 0, 0, 1)); break; } case CP_POLY_SHAPE: { cpPolyShape* poly = (cpPolyShape*)subShape; int num = poly->numVerts; Point* seg = new Point[num]; PhysicsHelper::cpvs2points(poly->tVerts, seg, num); _drawNode->drawPolygon(seg, num, Color4F(1.0f, 0.0f, 0.0f, 0.3f), 1.0f, Color4F(1.0f, 0.0f, 0.0f, 1.0f)); delete[] seg; break; } default: break; } } }
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 affichage(){ cpFloat timeStep = 1.0/60.0; SDL_Color white={255,255,255}; for(cpFloat time = 0; time < 25; time += timeStep){ SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format,0,0,0)); for(int i=0;i<50;i++){ if(lesBoules[i].del==FALSE){ cpVect pos = cpBodyGetPos(lesBoules[i].body); cpVect vel = cpBodyGetVel(lesBoules[i].body); rectangleColor(ecran,10, 0, 630, 420,SDL_MapRGB(ecran->format,255,255,255)); circleRGBA(ecran, pos.y, pos.x, lesBoules[i].radius, lesBoules[i].r,lesBoules[i].g,lesBoules[i].b,lesBoules[i].a); filledCircleRGBA(ecran,pos.y,pos.x,lesBoules[i].radius, lesBoules[i].r,lesBoules[i].g,lesBoules[i].b,lesBoules[i].a); //lettre SDL_Surface *lettre= TTF_RenderText_Solid(police, lesBoules[i].lettre, white); SDL_Rect position; position.y=pos.x; position.x=pos.y; lesBoules[i].x=pos.x; lesBoules[i].y=pos.y; SDL_BlitSurface(lettre, NULL, ecran, &position); } } SDL_Surface *pointsTexte=TTF_RenderText_Solid(police,"Score:" , white); SDL_Rect position_pointsTexte; position_pointsTexte.y=435; position_pointsTexte.x=10; SDL_BlitSurface(pointsTexte, NULL, ecran, &position_pointsTexte); char scoreChar[15]={"0"}; snprintf(scoreChar, 15, "%d", score); SDL_Surface *points=TTF_RenderText_Solid(police,scoreChar, white); SDL_Rect position_points; position_points.y=435; position_points.x=100; SDL_BlitSurface(points, NULL, ecran, &position_points); cpSpaceStep(espace, timeStep); SDL_Flip(ecran); } //affichage points SDL_Flip(ecran); }
const Vec2& PhysicsSprite::getPosFromPhysics() const { static Vec2 s_physicPosion; #if CC_ENABLE_CHIPMUNK_INTEGRATION cpVect cpPos = cpBodyGetPos(_CPBody); s_physicPosion = Vec2(cpPos.x, cpPos.y); #elif CC_ENABLE_BOX2D_INTEGRATION b2Vec2 pos = _pB2Body->GetPosition(); float x = pos.x * _PTMRatio; float y = pos.y * _PTMRatio; s_physicPosion = Vec2(x,y); #endif return s_physicPosion; }
Boule creerBoule() { cpFloat radius = rand() % 16 + 25;// radius entre 25 et 40 cpFloat mass = rand() % 3 + 1;// masse entre 1 et 3; cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); cpBody *ballBody = cpSpaceAddBody(espace, cpBodyNew(mass, moment)); cpBodySetPos(ballBody, cpv(0, 310)); cpShape *ballShape = cpSpaceAddShape(espace, cpCircleShapeNew(ballBody, radius, cpvzero)); cpShapeSetFriction(ballShape, -0.2); Boule laBoule={radius, cpBodyGetPos(ballBody).x,0,rand() % 136 + 150,rand() % 136 + 100,rand() % 136 + 50,255,ballShape,ballBody, genChar(),FALSE}; return laBoule; }
static void draw_poly_shape(struct draw_options opts, cpShape *shape, cpBody *body) { int num_verts = cpPolyShapeGetNumVerts(shape); cpVect first_vert = cpBodyLocal2World(body, cpPolyShapeGetVert(shape, 0)); cpVect prev_vert = first_vert; cpVect pos = cpBodyGetPos(body); for (int i = 1; i < num_verts; i++) { cpVect vert = cpBodyLocal2World(body, cpPolyShapeGetVert(shape, i)); draw_line(opts, prev_vert, vert); prev_vert = vert; } /* close up the polygon */ draw_line(opts, prev_vert, first_vert); }
void fff::kitty::Update(){ cpVect pos = cpBodyGetPos(body); lastpos = pos; sprite.SetPosition(pos.x, pos.y); flames[0].SetPosition(pos.x, pos.y); flames[1].SetPosition(pos.x, pos.y); cpVect vel = cpBodyGetVel(body); if (vel.y > KMH_TO_PXS(500.f) && !burstinflames){ burst.Play(); burstinflames = true; }else if (vel.y <= KMH_TO_PXS(500.f) ){ burstinflames = false; } iflames += 1; if (iflames > 1){ iflames = 0; } }
int main (int argc, char **argv) { cpSpace *space = cpSpaceNew(); cpSpaceSetGravity(space, cpv(0, GRAVITY)); cpEnableSegmentToSegmentCollisions(); cpShape *ground = cpSpaceAddShape(space, cpSegmentShapeNew(space->staticBody, cpv(0, 5), cpv(10, -5), 0)); cpShapeSetFriction(ground, 0.3); cpBody *drawing = drawing_new(2, 7, 0); drawing = drawing_update(space, drawing, 3, 8); drawing = drawing_update(space, drawing, 3, 9); drawing = drawing_update(space, drawing, 2, 9); //cpBody *drawing = drawing_new(0, 0, 0); //drawing = drawing_update(space, drawing, 2, 0); //drawing = drawing_update(space, drawing, 2, 2); //drawing = drawing_update(space, drawing, 0, 2); print_positions(drawing); //drawing_activate(drawing); //print_positions(drawing); cpVect vel, pos; int i = 0; for(cpFloat time = 0; time < 2; time += TIMESTEP){ pos = cpBodyGetPos(drawing); vel = cpBodyGetVel(drawing); printf( "Time = %2.2f Position = (%2.2f, %2.2f) Velocity = (%2.2f, %2.2f)\n", time, pos.x, pos.y, vel.x, vel.y); cpSpaceStep(space, TIMESTEP); i++; if (i == 20) { drawing_activate(space, drawing); print_positions(drawing); } } print_positions(drawing); free_body_full(drawing); cpShapeFree(ground); cpSpaceFree(space); return EXIT_SUCCESS; }
static cpBody *utils_update_drawing(cpBody *drawing) { cpFloat mass = cpBodyGetMass(drawing); cpFloat moment = cpBodyGetMoment(drawing); Point_array *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)); cpvadd(pos_a, origin); cpvadd(pos_b, origin); x += pos_b.x; y += pos_b.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 } 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); cpBodySetPos( drawing, new_origin ); //cpBodySetPos(drawing, cpv(pos.x + (second.x / length), pos.y + (second.y / length))); pa = shift_origin(pa, origin, new_origin); cpBodySetUserData(drawing, pa); return drawing; }