예제 #1
0
파일: main.c 프로젝트: kcouliba/vectors
size_t	wall_collision(t_vector2d *player, t_vector2d next_pos)
{
	t_vector2d	collision;
	t_vector2d	coord;
	t_vector2d	oob;

	coord.x = (int)(next_pos.x / WALLSIZE);
	coord.y = (int)(next_pos.y / WALLSIZE);
	printf("coord : ");
	print_vector(coord);
	printf("\n");
	collision.x = (((size_t)coord.x <= 0) || ((size_t)coord.x >= MAPSIZE)) ? 1 : 0;
	collision.y = (((size_t)coord.y <= 0) || ((size_t)coord.y >= MAPSIZE)) ? 1 : 0;
	if ((next_pos.x - player->x) > 0)
		oob.x = ((size_t)collision.x) ? coord.x * WALLSIZE - 1 : next_pos.x;
	else
		oob.x = ((size_t)collision.x) ? (coord.x + 1) * WALLSIZE + 1 : next_pos.x;
	if ((next_pos.y - player->y) > 0)
		oob.y = ((size_t)collision.y) ? coord.y * WALLSIZE - 1 : next_pos.y;
	else
		oob.y = ((size_t)collision.y) ? (coord.y + 1) * WALLSIZE + 1 : next_pos.y;
	printf("collision : ");
	print_vector(collision);
	printf("\n");
	printf("wall oob : ");
	print_vector(oob);
	printf("\n");
	*player = vector2d_sub(next_pos, vector2d_sub(next_pos, oob));
	return ((size_t)collision.x || (size_t)collision.y);
}
예제 #2
0
파일: op.c 프로젝트: aldenwalker/wallop
void fatgraph_op_gradient(op_vert* ov, 
                          op_vert* grad, 
                          int len,
                          int op_positions,
                          double target_length,
                          double alpha) {
  int i,j,k;
  vector2d vi;
  vector2d vj;
  vector2d vjvi;
  vector2d bezj,bezk;
  double bezjbezk;
  int destvert;
  double normvjvi;
  double thetaj, thetak;
  int edge_num;
  
  //compute the gradient in each of the vertex indices
  //note that these formulas work for both vertices < i and > i
  for (i=0; i<len; i++) {
    grad[i].loc.x = 0;
    grad[i].loc.y = 0;
  }
  if (op_positions == 0) {
    goto skipPositions;
  }
  for (i=0; i<len; i++) {
    vi = ov[i].loc;
    //location of vj affects both 
    //difference parts:
    for (j=0; j<len; j++) {
      if (i==j) continue;
      vj = ov[j].loc;
      vjvi = vector2d_sub(vj,vi);
      normvjvi = sqrt(vector2d_dot(vjvi,vjvi));
      //printf("About to run connected_verts\n");
      if (connected_verts(ov, i,j)==1) {
        grad[i].loc.x = (1/(alpha*alpha)) * 
                        ( 2*(vj.x - vi.x) + 
                          2*target_length*(1/(2*normvjvi))*2*(vj.x-vi.x));
        grad[i].loc.y = (1/(alpha*alpha)) *
                        ( 2*(vj.y - vi.y) + 
                          2*target_length*(1/(2*normvjvi))*2*(vj.y-vi.y));
      } else {
        grad[i].loc.x = -target_length*(vj.x-vi.x)*(1/normvjvi)*
                                                     (1/normvjvi)*
                                                     (1/normvjvi);
        grad[i].loc.y = -target_length*(vj.y-vi.y)*(1/normvjvi)*
                                                     (1/normvjvi)*
                                                     (1/normvjvi);
      }
    }
    //and angle parts:
    //note a vertex location affects its and its neighbors functions
    for (j=0; j<ov[i].num_edges; j++) {
      destvert = ov[i].dest_verts[j];
      if (destvert == i) continue;
      vj = ov[destvert].loc;
      edge_num = ov[i].edges[j];
      for (k=0; k<ov[destvert].num_edges; k++) {
        if (ov[destvert].edges[k] == edge_num) {
          break;
        }
      }
      vjvi = vector2d_sub(vj,vi);
      normvjvi = sqrt(vector2d_dot(vjvi,vjvi));
      thetaj = ov[i].theta[j];
      thetak = ov[destvert].theta[k];
      bezj.x = cos(thetaj);
      bezj.y = sin(thetaj);
      bezk.x = cos(thetak);
      bezk.y = sin(thetak);
      //affecting it's own functions:
      grad[i].loc.x += (-cos(thetaj)/normvjvi) + 
                       (vector2d_dot(vjvi,bezj)*(1/normvjvi)*
                                                (1/normvjvi)*
                                                (1/normvjvi));
      grad[i].loc.y += (-sin(thetaj)/normvjvi) + 
                       (vector2d_dot(vjvi,bezj)*(1/normvjvi)*
                                                (1/normvjvi)*
                                                (1/normvjvi));
      //affecting the other end:
      grad[i].loc.x += (cos(thetak)/normvjvi) + 
                       (vector2d_dot(vjvi,bezj)*(1/normvjvi)*
                                                (1/normvjvi)*
                                                (1/normvjvi));
      grad[i].loc.y += (sin(thetak)/normvjvi) + 
                       (vector2d_dot(vjvi,bezj)*(1/normvjvi)*
                                                (1/normvjvi)*
                                                (1/normvjvi));
    }
  }
  
  
  //LABEL
  //printf("I did NOT skip positions\n");
  skipPositions:
  
  //now for the theta parts:
  for (i=0; i<len; i++) {
    vi = ov[i].loc;
    for (j=0; j<ov[i].num_edges; j++) {
      destvert = ov[i].dest_verts[j];
      thetaj = ov[i].theta[j];
      bezj.x = cos(thetaj);
      bezj.y = sin(thetaj);
      if (destvert != i) {
        //affects pointing towards target:
        vj = ov[destvert].loc;
        vjvi = vector2d_sub(vj,vi);
        normvjvi = sqrt(vector2d_dot(vjvi,vjvi));
        grad[i].theta[j] = (1/normvjvi)*(vjvi.x*(-sin(thetaj)) + vjvi.y*cos(thetaj));
      } else {
        grad[i].theta[j] = 0;
      }
    
      //and affects interactions between edges
      for (k=0; k<ov[i].num_edges; k++) {
        if (k==j) continue;
        thetak = ov[i].theta[k];
        bezk.x = cos(thetak);
        bezk.y = sin(thetak);
        bezjbezk = vector2d_dot(bezj,bezk);
        if (ov[i].edges[j] == ov[i].edges[k]) { //loop edge
          //printf("Loop edge!\n");
          grad[i].theta[j] += (-6)*bezjbezk*(1/(1-bezjbezk))*
                              (2 + bezjbezk*(1/(1-bezjbezk)))*
                              (bezk.x*(-sin(thetaj)) + bezk.y*cos(thetaj));
        } else { //normal pair
          grad[i].theta[j] += (-1/((1-bezjbezk)*(1-bezjbezk))) *
                              (bezk.x * (-sin(thetaj)) + bezk.y * cos(thetaj));
        }
      }
    
    }
  }
  
  
  
}