// Read in lines from line.in and add them into collision world for simulation.
void LineDemo_createLines(LineDemo* lineDemo) {
  unsigned int lineId = 0;
  unsigned int numOfLines;
  window_dimension px1;
  window_dimension py1;
  window_dimension px2;
  window_dimension py2;
  window_dimension vx;
  window_dimension vy;
  int isGray;
  FILE *fin;
  fin = fopen("line.in", "r");
  assert(fin != NULL);

  fscanf(fin, "%d\n", &numOfLines);
  lineDemo->collisionWorld = CollisionWorld_new(numOfLines);

  while (EOF
      != fscanf(fin, "(%lf, %lf), (%lf, %lf), %lf, %lf, %d\n", &px1, &py1, &px2,
                &py2, &vx, &vy, &isGray)) {
    Line *line = malloc(sizeof(Line));

    // convert window coordinates to box coordinates
    windowToBox(&line->p1.x, &line->p1.y, px1, py1);
    windowToBox(&line->p2.x, &line->p2.y, px2, py2);

    line->max_x_is_p1 = (line->p1.x > line->p2.x);
    line->max_y_is_p1 = (line->p1.y > line->p2.y);

    // convert window velocity to box velocity
    velocityWindowToBox(&line->velocity.x, &line->velocity.y, vx, vy);
    update_box(line, lineDemo->collisionWorld->timeStep);

    // store color
    line->color = (Color) isGray;

    // store line ID
    line->id = lineId;
    lineId++;

    // transfer ownership of line to collisionWorld
    CollisionWorld_addLine(lineDemo->collisionWorld, line);
  }
  fclose(fin);
}
예제 #2
0
// Read in lines from line.in and add them into collision world for simulation.
void LineDemo_createLines(LineDemo* lineDemo) {
  unsigned int lineId = 0;
  unsigned int numOfLines;
  window_dimension px1;
  window_dimension py1;
  window_dimension px2;
  window_dimension py2;
  window_dimension vx;
  window_dimension vy;
  int isGray;
  FILE *fin;
  fin = fopen(LineDemo_input_file_path, "r");
  assert(fin != NULL);

  fscanf(fin, "%d\n", &numOfLines);
  lineDemo->collisionWorld = CollisionWorld_new(numOfLines);

  while (EOF
      != fscanf(fin, "(%lf, %lf), (%lf, %lf), %lf, %lf, %d\n", &px1, &py1, &px2,
                &py2, &vx, &vy, &isGray)) {
    Line *line = malloc(sizeof(Line));

    // convert window coordinates to box coordinates
    windowToBox(&line->p1.x, &line->p1.y, px1, py1);
    windowToBox(&line->p2.x, &line->p2.y, px2, py2);

    // convert window velocity to box velocity
    velocityWindowToBox(&line->velocity.x, &line->velocity.y, vx, vy);

    // store color
    line->color = (Color) isGray;

    // store line ID
    line->id = lineId;
    lineId++;

    // precompute some information about the line
    line->relative_vector = Vec_makeFromLine(*line);
    line->top_left = (Vec) {.x = MIN(line->p1.x, line->p2.x), .y = MIN(line->p1.y, line->p2.y)};
    line->bottom_right = (Vec) {.x = MAX(line->p1.x, line->p2.x), .y = MAX(line->p1.y, line->p2.y)};

    // transfer ownership of line to collisionWorld
    CollisionWorld_addLine(lineDemo->collisionWorld, line);
  }
  fclose(fin);
}

void LineDemo_setNumFrames(LineDemo* lineDemo, const unsigned int numFrames) {
  lineDemo->numFrames = numFrames;
}

void LineDemo_initLine(LineDemo* lineDemo) {
  LineDemo_createLines(lineDemo);
}

Line* LineDemo_getLine(LineDemo* lineDemo, const unsigned int index) {
  return CollisionWorld_getLine(lineDemo->collisionWorld, index);
}

unsigned int LineDemo_getNumOfLines(LineDemo* lineDemo) {
  return CollisionWorld_getNumOfLines(lineDemo->collisionWorld);
}

unsigned int LineDemo_getNumLineWallCollisions(LineDemo* lineDemo) {
  return CollisionWorld_getNumLineWallCollisions(lineDemo->collisionWorld);
}

unsigned int LineDemo_getNumLineLineCollisions(LineDemo* lineDemo) {
  return CollisionWorld_getNumLineLineCollisions(lineDemo->collisionWorld);
}

// The main simulation loop
bool LineDemo_update(LineDemo* lineDemo) {
  lineDemo->count++;
  CollisionWorld_updateLines(lineDemo->collisionWorld);
  if (lineDemo->count > lineDemo->numFrames) {
    return false;
  }
  return true;
}