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; }
// 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); } } }
cpFloat cpMomentForSegment(cpFloat m, cpVect a, cpVect b, cpFloat radius) { // TODO account for radius cpVect offset = cpvmult(cpvadd(a, b), 0.5f); return m*(cpvdistsq(b, a)/12.0f + cpvlengthsq(offset)); }
cpFloat cpMomentForSegment(cpFloat m, cpVect a, cpVect b) { cpVect offset = cpvmult(cpvadd(a, b), 0.5f); return m*(cpvdistsq(b, a)/12.0f + cpvlengthsq(offset)); }