   Circle and Rotated Rectangle Collision
  I'm trying to have a circle that can collide with a rectangle that can be rotated to any angle, but I don't think the response is working correctly, and I'm not sure why.Here is the code for the collision. //Rotate the circle's position about the rectangle's center Vector2 sPosition = new Vector2(); sPosition.X = mPosition.X + (float)(Math.Cos((double)-mRotation) * (s.Position.X - mPosition.X) - Math.Sin((double)-mRotation) * (s.Position.Y - mPosition.Y)); sPosition.Y = mPosition.Y + (float)(Math.Sin((double)-mRotation) * (s.Position.X - mPosition.X) + Math.Cos((double)-mRotation) * (s.Position.Y - mPosition.Y));  //Find the closest point Vector2 closestPoint = new Vector2(clamp(sPosition.X, mPosition.X - mSize.X / 2, mPosition.X + mSize.X / 2), clamp(sPosition.Y, mPosition.Y - mSize.Y / 2, mPosition.Y + mSize.Y / 2));  //Get the distance between the two points Vector2 mDiff = closestPoint - sPosition; //If if distance is less than the circle's radius, do a collision response if (mDiff.Length() < s.mSize / 2) {//Calculate the amount the position should be offset.float length = mDiff.Length();mDiff.Normalize();mDiff *= (s.mSize / 2) - length;//Rotate the point backmDiff.X = (float)(Math.Cos((double)mRotation)*mDiff.X - Math.Sin((double)mRotation)*mDiff.Y);mDiff.Y = (float)(Math.Sin((double)mRotation)*mDiff.X + Math.Cos((double)mRotation)*mDiff.Y);//Modifty the circle's position.s.Position -= mDiff; }It works fine for a non-rotated rectangle, but not when the rectangle is rotated.The reason I don't think the response is correct is because if a square is rotated by pi/4, when the circle hits some of the sides it slows down significantly, even though they are all at 45 degrees.Any suggestions?Thanks.   
  Commonly, collisions take into consideration velocity of both objects ( velocity = direction with a speed ) and each object would respond to a force dependent on the sum of the objects' velocities and the mass of each object.Also, commonly, a circle with a velocity would rebound from a plane (side of a rectangle) under some rule like "angle of reflection equals angle of incidence" with respect to the normal at the point of collision.Perhaps you could explain a bit more about how you want the circle and rectangle to behave.;  Right now, the velocity is hard-coded for testing purposes, because I'm not exactly sure where I'm going with this.But here's the code that determines the velocity:  if (keyboardState.IsKeyDown(Keys.Left)) mPosition.X -= 500 * (float)gameTime.ElapsedGameTime.TotalSeconds;  if (keyboardState.IsKeyDown(Keys.Right)) mPosition.X += 500 * (float)gameTime.ElapsedGameTime.TotalSeconds;  if (keyboardState.IsKeyDown(Keys.Up)) mPosition.Y -= 500 * (float)gameTime.ElapsedGameTime.TotalSeconds;  if (keyboardState.IsKeyDown(Keys.Down)) mPosition.Y += 500 * (float)gameTime.ElapsedGameTime.TotalSeconds;;Quote:Perhaps you could explain a bit more about how you want the circle and rectangle to behave.It's a bit difficult to help you without knowing what it is you're trying to achieve.BTW, if I understand your code correctly, the position of the circle depends on the total time played. After a while, the position is going to change pretty rapidly, I would think.;  gameTime.ElapsedGameTime is the total time since the last update was called.  Multiplying a value by the gametime should produce a steady velocity, so if there's a lapse between frames for some reason, the object is still updated the correct amount.Right now, my goal is simple.  I have a square that's being displayed, and the keyboard controls control a circle, without gravity.  What I'm trying to achieve is that when the circle hits say, a 45 degree slope, while his movement is only in the positive X direction, he would slide up the slope.Right now, it works, but hitting different slopes from different directions all have him going at what looks like different speeds, so I think something is incorrect about it.Sorry if I was unclear before.   ;  You say that it works fine for an unrotated rectangle but not for a rotated one.I have a hunch you are rotating the opposite way for the test than you are for rendering (easy mistake to make!)An easy way to test this would be seeing if your collision code works with a 180 degree rotated rectangle.If it does, you are probably rotating the wrong way.  Switch to rotating the other direction and if you are lucky it will just work (:   ;Quote:Original post by Atrix256You say that it works fine for an unrotated rectangle but not for a rotated one.I have a hunch you are rotating the opposite way for the test than you are for rendering (easy mistake to make!)An easy way to test this would be seeing if your collision code works with a 180 degree rotated rectangle.If it does, you are probably rotating the wrong way.  Switch to rotating the other direction and if you are lucky it will just work (:I've actually already run into that problem, which is why it uses "-mRotation" instead of mRotation.  I figured it out by rotating 30 degrees.Thanks though.   ;  In case I'm not explaining it well enough, I've uploaded the executable of what I've got so far.XNA is required to run it.Here's the link:http://www.virtual-toast.com/files/snake.zipKeyboard controls the object (made up of circles).Right click puts the object of interest, a square rotated by pi/4.Left click puts a circle, but that's unrelated.Thanks.   