-1

I have a small school project, of which I am required to create a simple game (platformer). I have been messing around with this which I found:

Var
 Overlay : Trect;
Begin
 If not intersectrect(Overlay, player.boundsrect, shape1.boundsrect) then
  //code
end;

This checks if the Player is colliding with shape1 and prevents falling past the shape. My problem is that I am creating the shapes at run time in an array. I can't check for collision against each shape, especially if there are many and could be problematic checking multiple times every timer tick.

Can I somehow do something like this:

If not intersectrect(Overlay, player.boundsrect, arrayShapes[1..20]);

or create an event that only triggers when player collides with a component, then checks if that component is a shape?

If not, is there a better way to accomplish this without checking against each shape?

User09871
  • 11
  • 1

1 Answers1

1

Rectangle intersection is very simple function and there no problem to check some hundreds/thousands of intersections.

for i := Low(arrayShapes) to High(arrayShapes) do
  if IntersectRect(Overlay, player.boundsrect, arrayShapes[i]) then
      DoSomething

If you want more effective approach, consider using some spatial indexing structure like R-tree. It allows to determine what rectangles are touched by given object without brute-force

MBo
  • 77,366
  • 5
  • 53
  • 86