2

I have a code which looks like this:

 /*Need  help here*/ 
if (car1.hitbox.Intersects(parkingLoot[0].hitbox) || car1.hitbox.Intersects(parkingLoot[1].hitbox))
        intersects = true;
    else
        intersects = false;

where hitboxes are a Rectangle. As you see there is 2 parkingLoot object in array and i checked them. But if i had 1000 parkingLoot objects? I dont want to use "for loop" to check every parkingLoot objects hitbox if intersects with car.

How to detect if car1.hitbox intersects with ANY HITBOX OF ANY OBJECT WHICH IS A PARKINGLOOT OBJECT

Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34
Ufuk Bakan
  • 98
  • 7
  • You say you don't want to use a "for loop" but a for loop is a just a tool, why don't you want to use a for loop? If you're worried about performance I advise you first to just implement it by checking all objects, 1000 isn't that much. If you ever find performance problems here I would advise you to take a look at Quadtree (https://en.wikipedia.org/wiki/Quadtree), or a similar data structure that is designed to be able to do quick intersection calculations. –  Dec 30 '19 at 12:14
  • actually i wanted to done this job in car class without having any information about parkingLoot class but it seems like its impossible in monogame? – Ufuk Bakan Dec 31 '19 at 13:26

2 Answers2

3

You can use the System.Linq extensions to iterate over the collection without foreach like below:

var intersects = parkingLoot.Any(_ => car1.hitbox.Intersects(_.hitbox));

or you can use the Parallel.ForEach from the System.Threading.Tasks to iterate over items in the list in parallel.

oleksa
  • 3,688
  • 1
  • 29
  • 54
  • thanks for answer how it works is it a standard iteration too – Ufuk Bakan Dec 30 '19 at 12:38
  • 1
    @UfukBakan yes it is standard iteration. I do not think that there is better option available unfortunately if you need to check the every item in the list. You can try to filter the source list before the iteration on certain condition. However additional filtering could cause additional delay in the intersection calculation. There is [parallel foreach also](https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop) available. – oleksa Dec 30 '19 at 12:50
  • 1
    Be aware, however, that using LINQ methods has a fairly important performance impact that should not be neglected for game development. – fdrobidoux Dec 30 '19 at 14:16
  • actually i wanted to done this job in car class without having any information about parkingLoot class but it seems like its impossible in monogame? – Ufuk Bakan Jan 02 '20 at 01:57
  • You can introduce your own interface like `IIntersects` with `bool Check(IIntersects)` method and create wrapper class over the Monogame's Hitbox (whatever) and implement the `IIntersects` interface with it by forwarding `Check` method calls to the `hitbox.Intersects` method. – oleksa Jan 02 '20 at 09:35
  • Be careful when using `Parallel.ForEach`, the cost of calling your `Intersects` method in parallel might be greater than calling it from a single thread. – Matt Clegg Feb 19 '20 at 09:50
0

I don't know the enviorment of your scenario, but in my case I have a open world, and I use a grid in the map, and the objets are placed around the cells of the grid. You can do something like that, put every parkingLoot obstacle in a grid, and your car to. And then, using the dimentions of your car you can know the set of cells that the car ocupe, and do a checking of those cells to see if exist obstacles and them check the collision whit this subsets of obstacles collisions rectangles.

You always need to do a for (or whereever structure that you use) for a set of obstacles, but you need to try to minimaize the number of elements of this list for performance matters.

If you give me more details of your game envairoment maybe I can do code example.