Skip to main content
added 8 characters in body
Source Link
Steve H
  • 5.1k
  • 20
  • 21

It is not just a rectangle/rectangle test. It is also a dot product test to determine which side of the slope line the other object is on. For this you will need to initialize the 2 end points of your slope. This becomes a line segment and the corners of your slope's collision rect. So your slope and rect both share point A & B.

enter image description here

The algorithm gos like this:

If other object is in the rect, check which side of the line it is on,

If above,

 No collision,

else

 Collision

In XNA, that would go something like this,

if(objectRect.Intersects(slopeRect)
{
  Vector2 objectRelativePosition = objectPosition - PointA;
  Vector2 slopeRelativePosition = PointB - PointA;
  Vector3 slopeNormal3 = Vector3.Cross(Vector3.UnitZ, Vector3(slopeRelativePosition,0), Vector3.UnitZ);
  Vector2 slopeNormal = Vector2(slopeNormal3.X, slopeNormal3.Y);
  
  if(Vector2.Dot(slopeNormal, objectRelativePosition) > 0)
    {
      //object is above slope, no collision
    }
    else
    {
      // object is below slope, collision
    }
}

The slopeRelativePosition, slopeNormal3, & slopeNormal can all be done only once in the initialization of your map, not each frame. but objectRelativePosition must be recalculated each time you want to run a collision check with the slope.

You can also run this check against each point that makes up the object (say, each corner of a rectangle) if there is no single "position" for the object.

It is not just a rectangle/rectangle test. It is also a dot product test to determine which side of the slope line the other object is on. For this you will need to initialize the 2 end points of your slope. This becomes a line segment and the corners of your slope's collision rect. So your slope and rect both share point A & B.

enter image description here

The algorithm gos like this:

If other object is in the rect, check which side of the line it is on,

If above,

 No collision,

else

 Collision

In XNA, that would go something like this,

if(objectRect.Intersects(slopeRect)
{
  Vector2 objectRelativePosition = objectPosition - PointA;
  Vector2 slopeRelativePosition = PointB - PointA;
  Vector3 slopeNormal3 = Vector3.Cross(Vector3(slopeRelativePosition,0), Vector3.UnitZ);
  Vector2 slopeNormal = Vector2(slopeNormal3.X, slopeNormal3.Y);
  
  if(Vector2.Dot(slopeNormal, objectRelativePosition) > 0)
    {
      //object is above slope, no collision
    }
    else
    {
      // object is below slope, collision
    }
}

The slopeRelativePosition, slopeNormal3, & slopeNormal can all be done only once in the initialization of your map, not each frame. but objectRelativePosition must be recalculated each time you want to run a collision check with the slope.

You can also run this check against each point that makes up the object (say, each corner of a rectangle) if there is no single "position" for the object.

It is not just a rectangle/rectangle test. It is also a dot product test to determine which side of the slope line the other object is on. For this you will need to initialize the 2 end points of your slope. This becomes a line segment and the corners of your slope's collision rect. So your slope and rect both share point A & B.

enter image description here

The algorithm gos like this:

If other object is in the rect, check which side of the line it is on,

If above,

 No collision,

else

 Collision

In XNA, that would go something like this,

if(objectRect.Intersects(slopeRect)
{
  Vector2 objectRelativePosition = objectPosition - PointA;
  Vector2 slopeRelativePosition = PointB - PointA;
  Vector3 slopeNormal3 = Vector3.Cross(Vector3.UnitZ, Vector3(slopeRelativePosition,0));
  Vector2 slopeNormal = Vector2(slopeNormal3.X, slopeNormal3.Y);
  
  if(Vector2.Dot(slopeNormal, objectRelativePosition) > 0)
    {
      //object is above slope, no collision
    }
    else
    {
      // object is below slope, collision
    }
}

The slopeRelativePosition, slopeNormal3, & slopeNormal can all be done only once in the initialization of your map, not each frame. but objectRelativePosition must be recalculated each time you want to run a collision check with the slope.

You can also run this check against each point that makes up the object (say, each corner of a rectangle) if there is no single "position" for the object.

Source Link
Steve H
  • 5.1k
  • 20
  • 21

It is not just a rectangle/rectangle test. It is also a dot product test to determine which side of the slope line the other object is on. For this you will need to initialize the 2 end points of your slope. This becomes a line segment and the corners of your slope's collision rect. So your slope and rect both share point A & B.

enter image description here

The algorithm gos like this:

If other object is in the rect, check which side of the line it is on,

If above,

 No collision,

else

 Collision

In XNA, that would go something like this,

if(objectRect.Intersects(slopeRect)
{
  Vector2 objectRelativePosition = objectPosition - PointA;
  Vector2 slopeRelativePosition = PointB - PointA;
  Vector3 slopeNormal3 = Vector3.Cross(Vector3(slopeRelativePosition,0), Vector3.UnitZ);
  Vector2 slopeNormal = Vector2(slopeNormal3.X, slopeNormal3.Y);
  
  if(Vector2.Dot(slopeNormal, objectRelativePosition) > 0)
    {
      //object is above slope, no collision
    }
    else
    {
      // object is below slope, collision
    }
}

The slopeRelativePosition, slopeNormal3, & slopeNormal can all be done only once in the initialization of your map, not each frame. but objectRelativePosition must be recalculated each time you want to run a collision check with the slope.

You can also run this check against each point that makes up the object (say, each corner of a rectangle) if there is no single "position" for the object.