// Game initialization function // ========================================================== void init(void) { // init random seed unsigned int seed = time(NULL); srand(seed); // Init asteroid object array asteroids = (object *)malloc(sizeof(object)*MAXASTEROIDS); int i; for(i=0;i<MAXASTEROIDS;i++) { // Create an asteroid at a random location on screen vector p = makeVec(10.0-(float)(rand()%25), 10.0-(float)(rand()%25)); initAsteroid(asteroids+i, &p, 3.0); // If asteroid spawns too close to ship, make it somewhere else while(lengthVec(&(asteroids[i].pos)) < 5.0) { p = makeVec(10.0-(float)(rand()%25), 10.0-(float)(rand()%25)); initAsteroid(asteroids+i, &p, 3.0); } // Activate 4 big asteroids at the beginning if (i%4!=0) asteroids[i].state &= !ASTEROID_ACTIVE; else asteroids[i].state |= ASTEROID_BIG; } // Init asteroid gibs (inactive) asteroidGibs = (object**)malloc(sizeof(object*)*MAXASTEROIDS); int ab; for (ab=0; ab<MAXASTEROIDS; ab++) { asteroidGibs[ab] = (object*)malloc(sizeof(object)*asteroids[ab].size); initGib(asteroidGibs[ab], asteroids+ab, 0); } // init ship ships = malloc(sizeof(object)*MAXSHIPS); for (i = 0; i < MAXSHIPS; i++) { initShip(ships+i); } myship = ships; myship->state = 0; // init ship stuff //initMissile(); // init ship gibs (inactive) shipGibs = (object*)malloc(sizeof(object)*ships[0].size*MAXSHIPS); for (i=0; i < MAXSHIPS; i++) { initGib(shipGibs+i*ships[0].size, ships, 0); } // init stars stars = (object*)malloc(sizeof(object)*MAXSTARS); int si; for(si=0;si<MAXSTARS; si++) { vector v = makeVec((float)(rand()%50)-25.0, (float)(rand()%50)-25.0); initStar(stars+si, &v); } }
void getItem (int itemNo, float x0, float x1, float y0, float y1, float z0, float z1, float acc) { float best = floatZero; int lim = 40; if (time < lim) lim = time; for (float x = x0; x < x1 + floatItsml; x += acc) { for (float y = y0; y < y1 + floatItsml; y += acc) { for (float z = z0; z < z1 + floatItsml; z += acc) { float temp = floatZero; for (int i = 0; i <= lim; i ++) { float diff = fabs(tridis[i][itemNo] - disBetweenPts(makeVec(x, y, z), tripos[i])); if (diff < 0.0101f) temp += floatOne - diff; } if (temp > best) { iloc[0] = x; iloc[1] = y; iloc[2] = z; best = temp; } } } } #if printDebug DEBUG(("getItem(%d)\n", itemNo)); printVec("\tItemPos (m)", iloc); DEBUG(("\tAccuracy Score\t%.3f\n", best / (time + 1.0f))); DEBUG(("\tVel Magnitude (m/s)\t%.3f\n", mathVecMagnitude(myState + 3, 3))); DEBUG(("\tAngvel Magnitude (deg/s)\t%.3f\n", mathVecMagnitude(myState + 9, 3) * 57.296f)); DEBUG(("\tDis from Item (m)\t%.3f\n", disBetweenPts(myState, iloc))); #endif api.setPositionTarget(iloc); if (hitTarget(myState, iloc)) { #if printDebug DEBUG(("\tHit target\n")); #endif setTargetAngVel(makeVec(floatZero, floatZero, pickUpSpeed)); } else { setTargetAngVel(zeroVec); } }
// Create asteroid (called at start and whenever an asteroid is destroyed) void initAsteroid(object *o, vector *p, float r) { object A; A.verts = (vector*)malloc(sizeof(vector)*13); int i; for(i = 0; i < 12; i++) { vector vi = makeVec(cos(i*pi/6.0), sin(i*pi/6.0)); A.verts[i] = scaleVec(&vi, r*(0.75+0.25*cos(rand()))); } A.verts[i] = A.verts[0]; A.size = 13; A.pos = copyVec(p); A.dir = upVec(); A.ang = (float)(rand()%10); A.turn = 2.0; vector vel = makeVec(sin((float)rand()), cos((float)rand())); A.vel = scaleVec(&vel, 2.0+cos(rand())); A.spd = 5.0; A.type = ASTEROID; A.mode = GL_LINE_LOOP; A.state = ASTEROID_ACTIVE; A.radius = r; *o = A; }
// Create star that twinkles occasionally void initStar(object *o, vector *l) { object star; vector* v = (vector*)malloc(sizeof(vector)*8); star.verts = v; int i; for(i=0;i<8;i+=2) { *v = makeVec(0.1*cos(pi*(float)i/4.0), 0.1*sin(pi*(float)i/4.0)); v++; *v = makeVec(0.1*cos(pi*(float)(i+1)/4.0), 0.1*sin(pi*(float)(i+1)/4.0)); v++; } star.size = 8; star.pos = copyVec(l); star.dir = upVec(); star.ang = 0.0; star.turn = 0.0; star.vel = zeroVec(); star.spd = 50.0; star.type = STAR; star.mode = GL_LINE_LOOP; star.state = 0; *o = star; }
// Create gibs (called each time an object is destroyed) // oa: array of objects to put gibs into // o: object to create gibs form // flags: state to begin gibs in void initGib(object *oa, object *o, int flags) { int i; for (i=0; i<o->size; i++) { object gib; vector *gv = (vector*)malloc(sizeof(vector)*2); gib.verts = gv; gv->x = o->verts[i].x; gv->y = o->verts[i].y; gv++; if (i==o->size-1) { gv->x = o->verts[0].x; gv->y = o->verts[0].y; } else { gv->x = o->verts[i+1].x; gv->y = o->verts[i+1].y; } vector mid = makeVec((gib.verts[0].x+gib.verts[1].x)/2.0, (gib.verts[0].y+gib.verts[1].y)/2.0); gib.pos = localToWorld(&mid, o); // vector dv = subVec(&(gib.pos), &(o->pos)); gib.verts[0] = subVec(&mid, &(gib.verts[0])); gib.verts[1] = subVec(&mid, &(gib.verts[1])); gib.size = 2; gib.dir = upVec(); gib.ang = o->ang; gib.turn = 1.0+0.5*cos((float)rand()); vector vel = makeVec(sin((float)rand()), cos((float)rand())); gib.vel = scaleVec(&vel, 0.2+cos(rand())); gib.spd = 2.0; gib.type = GIB; gib.mode = GL_LINES; gib.state = flags; gib.lifetime = 0.5; oa[i] = gib; } }
// Create the ship (called only once, at start) void initShip(object *o) { object ship; vector* v = (vector *)malloc(sizeof(vector)*3); ship.verts = v; v->x = 0.0; v->y = -1.0; v++; v->x = -0.5; v->y = 1.0; v++; v->x = 0.5; v->y = 1.0; ship.size = 3; ship.pos = zeroVec(); ship.dir = makeVec(0.0, -1.0);; ship.ang = 0.0; ship.turn = 10.0; ship.vel = zeroVec(); ship.spd = 25.0; ship.type = SHIP; ship.mode = GL_LINE_LOOP; ship.state = SHIP_INACTIVE; LAZORZ = (object *)malloc(sizeof(object)*MAXSHOTS); *o = ship; }
void remDust () { int i = obsDetected = game.getIdentifiedObstacles(obstacles); api.setAttitudeTarget(makeVec(floatZero, -1.0f, floatZero)); #if printDebug DEBUG(("remDust()\n")); DEBUG(("\tDetected\t%d\n", obsDetected)); #endif while (i -- && chg) { #if printDebug DEBUG(("\tObstacle ID\t%d\tRadius\t%.3f\n", obstacles[i].ID, obstacles[i].size)); printVec("\t\tObstacle Location", obstacles[i].loc); #endif if (obstacles[i].visible) { #if printDebug DEBUG(("\t\tShrinking\n")); #endif game.shrinkObstacle(obstacles[i].ID); return; } } }
TEST(TrapezoidGenerator, start_stop_with_cruising) { const uint32_t steps = 10; const float v = 25.3; // Velocity corresponding to ref[3] const float f = 1e4f; const float acc = 2.0/powf(1000/f/0.676,2); TrapezoidParameters p(steps, 0, 0, v, f, acc); uint32_t ref[] = { reference[0], reference[1], reference[2], reference[3], reference[3], reference[3], reference[3], reference[3], reference[2], reference[1] }; testTrapezoid(p, makeVec(ref)); }
TEST(TrapezoidGenerator, start_to_running_with_cruising) { const uint32_t steps = 10; const float v = 25.3; const float f = 1e4f; const float acc = 2.0/powf(1000/f/0.676,2); TrapezoidParameters p(steps, 0, v, v, f, acc); const uint32_t ref[] = { reference[0], reference[1], reference[2], reference[3], reference[3], reference[3], reference[3], reference[3], reference[3], reference[3], }; testTrapezoid(p, makeVec(ref)); }
TEST(TrapezoidGenerator, start_stop_no_cruising) { const uint32_t steps = 10; const float v = 100; const float f = 1e4f; const float acc = 2.0/powf(1000/f/0.676,2); TrapezoidParameters p(steps, 0, 0, v, f, acc); EXPECT_LT(steps/2, v*v/(2*acc)) << "Input params cause cruising"; uint32_t ref[] = { reference[0], reference[1], reference[2], reference[3], reference[4], reference[5], reference[4], reference[3], reference[2], reference[1] }; testTrapezoid(p, makeVec(ref)); }
void testApp::update() { Mat mat = getMat(depthImage); Sobel(mat, sobelxy, CV_32F, 1, 1); sobelxy = abs(sobelxy); boxFilter(sobelxy, sobelbox, 0, cv::Size(7, 7)); triangulator.init(); points.clear(); int i = 0; attempts = 0; while(i < 5000) { Point2d curPosition((int) ofRandom(sobelbox.cols - 1), (int) ofRandom(sobelbox.rows - 1)); float curSample = sobelbox.at<unsigned char>(curPosition) / 255.f; float curGauntlet = powf(ofRandom(0, 1), 2 * (float) mouseX / ofGetWidth()); if(curSample > curGauntlet) { points.push_back(makeVec(curPosition)); triangulator.addPoint(curPosition.x, curPosition.y); sobelbox.at<unsigned char>(curPosition) = 0; // don't do the same point twice i++; } attempts++; if(i > attempts * 100) { break; } } int w = mat.cols - 1; int h = mat.rows - 1; triangulator.addPoint(0, 0); triangulator.addPoint(w, 0); triangulator.addPoint(w, h); triangulator.addPoint(0, h); triangulator.triangulate(); }
// Create message (called at start, activated on player death) void initMessage() { message = (object*)malloc(sizeof(object)*5); vector** letters = (vector**)malloc(sizeof(vector*)*5); letters[0] = (vector*)malloc(sizeof(vector)*7); vector* vi = letters[0]; vi->x = 3.0; vi->y = -5.0; vi++; vi->x = 3.0; vi->y = 4.0; vi++; vi->x = 1.0; vi->y = 5.0; vi++; vi->x = -1.0; vi->y = 5.0; vi++; vi->x = -3.0; vi->y = 4.0; vi++; vi->x = -3.0; vi->y = 4.0; vi++; vi->x = -3.0; vi->y = -5.0; letters[1] = (vector*)malloc(sizeof(vector)*7); vi = letters[1]; float n = 0.0; int i; for(i=0;i<7;i++){vi->x=n;vi->y=n;vi++;} letters[2] = (vector*)malloc(sizeof(vector)*7); vi = letters[2]; vi->x = 3.0; vi->y = -5.0; vi++; vi->x = -1.0; vi->y = -5.0; vi++; vi->x = -3.0; vi->y = -3.0; vi++; vi->x = -3.0; vi->y = 3.0; vi++; vi->x = -1.0; vi->y = 5.0; vi++; vi->x = 3.0; vi->y = 5.0; vi++; vi->x = 3.0; vi->y = -5.0; letters[3] = (vector*)malloc(sizeof(vector)*7); vi = letters[3]; vi->x = -3.0; vi->y = -5.0; vi++; vi->x = 3.0; vi->y = -5.0; vi++; vi->x = 3.0; vi->y = 0.0; vi++; vi->x = -3.0; vi->y = 0.0; vi++; vi->x = 3.0; vi->y = 0.0; vi++; vi->x = 3.0; vi->y = 5.0; vi++; vi->x = -3.0; vi->y = 5.0; letters[4] = (vector*)malloc(sizeof(vector)*7); vi = letters[4]; vi->x = 3.0; vi->y = -5.0; vi++; vi->x = -1.0; vi->y = -5.0; vi++; vi->x = -3.0; vi->y = -3.0; vi++; vi->x = -3.0; vi->y = 3.0; vi++; vi->x = -1.0; vi->y = 5.0; vi++; vi->x = 3.0; vi->y = 5.0; vi++; vi->x = 3.0; vi->y = -5.0; int li; for (li=0; li<5; li++) { object L; L.verts = letters[li]; L.size = 7; L.pos = makeVec(16.0-8.0*(float)li, 0.0); L.dir = upVec(); L.ang = 0.0; L.turn = 0.0; L.vel = zeroVec(); L.spd = 0.0; L.type = MESSAGE; L.mode = GL_LINE_STRIP; L.state = 0; *(message+li) = L; } }
void loop() { //Constant Variables zeroVec[0] = zeroVec[1] = zeroVec[2] = floatZero; //Variable Initialization time ++; api.getMyZRState(myState); api.getOtherZRState(otherState); phase = game.getCurrentPhase(); dust = game.getRemainingMaterial(); chg = game.getCharge(); if (!time) red = myState[0] < floatZero; if (time < 30 && otherState[1] > 0.1f) otherRush = true; //Trilateration api.getMyZRState(tripos[time]); game.pingForItems(tridis[time]); //Debug Console #if printDebug DEBUG(("\nUS#7 DevilTech BACON Hart District\nMessage Screen: Fall 2012\n")); DEBUG(("Debug Info\n")); DEBUG(("\tTime (s)\t%d\tPhase\t%d\tPlayer\t%s\n", time, phase, (red ? "Red" : "Blue"))); DEBUG(("\tFuel\t%.3f\tDust\t%.3f\tChg\t%d\n", game.getFuelRemaining(), dust, chg)); DEBUG(("\tMy Score\t%.3f\tOther Score\t%.3f\n", game.getScore(), game.getOtherScore())); DEBUG(("\tObstacles Created\t%d\n", obsCreated)); DEBUG(("\tOther Rush\t%d\n", otherRush)); printVec("\tMy Pos (m)", myState); printVec("\tMy Vel (m/s)", (myState + 3)); printVec("\tMy Att (unitvec)", (myState + 6)); printVec("\tMy AngVel (rad/s)", (myState + 9)); printVec("\tOther Pos (m)", otherState); printVec("\tOther Vel (m/s)", (otherState + 3)); printVec("\tOther Att (unitvec)", (otherState + 6)); printVec("\tOther AngVel (rad/s)", (otherState + 9)); #endif //Game Logic if (time < 4) { api.setVelocityTarget(makeVec(floatZero, floatZero, 0.0095f)); } if (!time) { game.startObstacle(); return; } if (time == 1) { game.stopObstacle(); } if (!game.haveObject(0)) { if (!game.haveObject(1) && !game.otherHasObject(1)) getOne; else getZero; } else { vec temp = {(red ? 0.15f : -0.15f), floatZero, myState[2]};/* if (!obsDetected && ((red && myState[0] > -0.05f) || (!red && myState[0] < 0.05f))) { int res = game.extendView(); if (printDebug) { DEBUG(("Extending View: %d\n", res)); } }*/ float tryy[7] = {(otherRush ? myState[2] : 0.38f), 0.11f, 0.22f, 0.34f, 0.45f, 0.55f, 0.68f}; obsDetected = game.getIdentifiedObstacles(obstacles); bool shr = false; for (int i = 0; i < 7; i ++) { if (printDebug) DEBUG(("\tTrying %.3f\n", tryy[i])); bool good = true; temp[2] = tryy[i]; for (int j = 0; j < obsDetected; j ++) { temp[1] = obstacles[j].loc[1]; if (printDebug) { DEBUG(("\t\tID %d, rad %.3f\n", obstacles[j].ID, obstacles[j].size)); printVec("\t\tLocation", obstacles[j].loc); DEBUG(("\t\tDis %.3f\n", disBetweenPts(obstacles[j].loc, temp))); } if (disBetweenPts(obstacles[j].loc, temp) < obstacles[j].size + 0.058f) { good = false; break; } if (!shr && obstacles[j].visible) { DEBUG(("\t\tShrinking\n")); shr = true; game.shrinkObstacle(obstacles[j].ID); } } if (good) { if (printDebug) DEBUG(("Decided %.3f\n", tryy[i])); temp[2] = tryy[i]; break; } } temp[1] = floatZero; if ((red && myState[0] < floatZero) || ((!red && myState[0] > floatZero))) { goStraight(temp, 1, -0.024f); } else if (myState[1] > -0.66f) { goStraight(temp, 1, (myState[1] > -0.48f && fabs(myState[2] - temp[2]) < 0.058f ? -0.0475f : -0.0235f)); } else { if (!alreadySet) { alreadySet = true; top = myState[2] > floatZero; } temp[1] = -0.71f; goStraight(temp, 2, top ? -0.055f : 0.055f); } } }