Nooby programer trying to make a tile system. Having a problem.

I am trying to make a tile system. Right now I generate a list of rectangles, and a list of texture2ds (to color the rectangles random colors).

All the rectangles are the same size with a random x and y coordinate.

I plug the rectangles and texture into a draw statement, and it draws them onto the screen. This system works.

I am trying to make it so that the rectangles don’t over lap.

I have a check in place in my update function that that prevent new rectangles from being generated if they intersect with any rectangles that have already been generated. But when I run my game there are no rectangles being drawn onto my screen.

http://pastebin.com/ayKfFjBc

Line 67 is where update starts. And line 102 is my intersection testing function.

Just a quick observation: You can use spritebatch to draw a single pixel white texture (1x1) into a rectangle with a specified color. No need to create many larger textures. Just use an overload of draw with a destination rectangle (that should be 100x100).

hi darth_irule,

Without looking into too much detail, it appears you need to rethink your update function. Line 69, Since you Init “List Squares” as an empty list, your for loop will never fire. Squares.Count() will be 0. Squares.Count() - 1 will be -1 (0 minus 1). Your “var i = 0” will never be “<= Squares.Count() -1”, or “-1”, therefore your For loop will never start and nothing will be added into your Squares list.

The way you are testing for intersection is also going to be be problematic but you should try to fix your update loop first. I recommend taking a beginners programming course or do more reading online before tackling making a game. Good luck!

In my Init function I have added a square and texture to the lists so they wont be empty once the update function is called.

And you’re right about my intersection testing, it doesn’t seem to be doing anything. Any tips/hints on how I could go about with the intersection testing? I’m not looking for a direct, here is what you should do, as I am messing around in monogame/game dev while I take my programming classes at college. And I think it is better to figure out these problems myself and learn than just copying code!

Although I am pretty stuck on the intersection which I why I am asking for some guidance.

Hi darth_irule,

What you are doing with the intersections is going to be pretty processor intensive and I can’t get into any great details here as efficient heuristics would be a lengthy topic on it’s own.

What you could be doing with your checkInter() is loop through the whole list of rectangles and check if each one intersects, then break early if you find an intersection. You don’t need an “intersected” variable as that is actually what is causing you issues. Let’s say you’re new rectangle intersects the 3rd rectangle in your list, your “intersected” would be set to true, but then on the next iteration of your loop, the 4th rectangle doesn’t intersect, so now “intersected” is false. When you finally return “intersected”, it’ll return false even though the 3rd rectangle intersected. What you need to do is put a “return false” at the end of checkInter(), and put a return “true” inside the for loop if you find an intersection. It’s also better to name your function “doesIntersect()” or “isIntersecting()” so it’s more clear what your function returns as true/false. You should probably make checkInter() take in a rectangle instead of an integer as array index as well. Good luck