Collision resulting in sticking objects.

Member
Posts: 40
Joined: 2004.12
Post: #1
I am having problems with a line that gets "stuck" to its bounds when the line rotates and then moves immediately after rotating. The line will hit the bounds and then continue to alternate along the bounds because of my collision resolution code, but I cannot for the life of me figure out what is wrong with it.
The offending code:
Code:
- (void)rotate
{
  static int count = 0;
  float x1, y1, x2, y2;
  float xmax, xmin, ymax, ymin;
  static float theta = 15.0;
  static bool collide = false;
  if (++count == 230)
    {
      //when rotating, make sure doesn't go outside bounds
      x1 = origin.x + length * 0.5 * cos(RADIANS(angle + theta));
      y1 = origin.y + length * 0.5 * sin(RADIANS(angle + theta));
      x2 = origin.x - length * 0.5 * cos(RADIANS(angle + theta));
      y2 = origin.y - length * 0.5 * sin(RADIANS(angle + theta));

      xmax = (x1 > x2 ? x1 : x2);
      xmin = (x1 < x2 ? x1 : x2);
      
      ymax = (y1 > y2 ? y1 : y2);
      ymin = (y1 < y2 ? y1 : y2);


      if (xmax >= 1.0 || xmin <= -1.0)
    collide = true;
      if (ymax >= 1.0 || ymin <= -1.0)
    collide = true;
      
      if (!collide)
    {
      angle += theta;
    }
      count = 0;
    }
}



- (void)move
{
  // check bounds
  // bounds are -1 to 1 in x and y directions
  // need to check endpoints eventually
  static float vx = 0.75 / FPS;
  static float vy = 0.8 / FPS;
  float x1, x2, y1, y2;
  float xmax, xmin, ymax, ymin;
  x1 = origin.x + length * 0.5 * cos(RADIANS(angle)) + vx;
  y1 = origin.y + length * 0.5 * sin(RADIANS(angle)) + vy;
  x2 = origin.x - length * 0.5 * cos(RADIANS(angle)) + vx;
  y2 = origin.y - length * 0.5 * sin(RADIANS(angle)) + vy;

  xmax = (x1 > x2 ? x1 : x2);
  xmin = (x1 < x2 ? x1 : x2);

  ymax = (y1 > y2 ? y1 : y2);
  ymin = (y1 < y2 ? y1 : y2);

  if (xmax >= 1.0 || xmin <= -1.0)
    vx *= -1;
  else
    origin.x += vx;

  if (ymax >= 1.0 || ymin <= -1.0)
    vy *= -1;
  else
    origin.y += vy;
}

Jericho
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #2
Try changing the direction of rotation after collision (i.e. flip the sign of theta). Also there are macros for MAX and MIN. e.g.
Code:
xmax = MAX(x1,x2);
and I don't think there's any reason for theta and collide to be declared static.

--phillip
Quote this message in a reply
Post Reply