• Create Account

### #ActualAurioch

Posted 04 December 2012 - 06:25 PM

Wow, thank you very much for detailed explanations. I had a vague understanding of it, now it's everything more clear
And honestly, if I ever did encounter encapsulation that would be in my "old" VB.NET book. On university, while we're allowed to use common languages (C# being one of them), we actually never studied about object-oriented programming at all, only procedural ( C ). I was baffled when my friend told me that one of his teammates for Project class wrote a finite state machine in C# completely procedural without usage of object methods and classes. So, explanations like your are pure gold for me.

I haven't done pixel-perfect collision detection before but I would start by comparing bounding boxes to check that the tank is colliding with a particular wall segment, and if the bounding boxes are colliding only check the area where the two bounding boxes overlap for a collision. Also I would check one wall at a time so that if you find a collision early on you can return true and don't have to check the other walls.

I already did that ;) There is no need to perform pixel-perfect collision for objects that won't be modified over the course of the game like water tiles (unpassable for tanks). A code will make that more clear:
[source lang="csharp"] bool Collision(Tank Object) { List<Tile> collided = new List<Tile>(); Rectangle objectRect = Object.GetRectangle(); if (!playableArea.Contains(objectRect)) return true; foreach (var tank in Tanks) if (!Object.Equals(tank) &amp;&amp; objectRect.Intersects(tank.GetRectangle())) return true; foreach (var tile in waterTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in metalTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in brickTile) if (objectRect.Intersects(tile.BoundingBox)) collided.Add(tile); if (collided.Count > 0) return PixelPerfectCollision(Object, collided); return false; }[/source]
As you can see, I structured collision checks so that the check for collision with brick wall (destructible) is last, enabling the function to exit early if it find collision with objects that don't need the pixel-perfect collision check.

I'm a bit confused on the way that you're calculating the offset variable. If the Tank is more than the totalWidth or totalHeight from the origin, won't your x and y indexes go out of range? E.g. in the following loop if tankBox.left > offset.X + totalWidth, x will not be a valid index for wallColor and tankColor. I've probably missed something, it's late

I've taken care of it too. There are 2 reasons why that won't happen (unless I'm gravely mistaken which I'll find out after I change resolution):
• Pixel-perfect collision check is ran if Bounding Box collision check is positive, meaning that tank and walls will always be inside 2*2 tile (80*80 pixel at 800*600 resolution) area, hence
int totalWidth = Math.Max(Wall[0].BoundingBox.Width, tankBox.Width) * 2;
int totalHeight = Math.Max(Wall[0].BoundingBox.Height, tankBox.Height) * 2;
Incidentally, if tankBox.left > offset.X + totalWidth pixel-perfect collision check won't be invoked since it will fail on bounding box collision check.
• Vector2 offset is simply a location of top left pixel whose usage is to ease the iteration through 2D array. I used it specifically to cover the situation where the tank is on the left and/or above the top-left wall tile (I can draw all possible situations).

If your PixelPerfectCollision method is a problem performance-wise then you could combine the first two for loops together, and you could combine the last two for loops together.

Combining last 2 double loops I didn't notice Thanks for suggestion. Originally it was from 0 to totalWidth/totalHeight but when I debugged it I saw I didn't cover few cases and added offset into calculation, resulting in equality of those two loops.

Well, off to the sleep, and tomorrow I'm continuing with my work. To quote Natsu from Fairy Tail: "I'm all fired up!"

### #3Aurioch

Posted 04 December 2012 - 06:25 PM

Wow, thank you very much for detailed explanations. I had a vague understanding of it, now it's everything more clear
And honestly, if I ever did encounter encapsulation that would be in my "old" VB.NET book. On university, while we're allowed to use common languages (C# being one of them), we actually never studied about object-oriented programming at all, only procedural ( C ). I was baffled when my friend told me that one of his teammates for Project class wrote a finite state machine in C# completely procedural without usage of object methods and classes. So, explanations like your are pure gold for me.

I haven't done pixel-perfect collision detection before but I would start by comparing bounding boxes to check that the tank is colliding with a particular wall segment, and if the bounding boxes are colliding only check the area where the two bounding boxes overlap for a collision. Also I would check one wall at a time so that if you find a collision early on you can return true and don't have to check the other walls.

I already did that ;) There is no need to perform pixel-perfect collision for objects that won't be modified over the course of the game like water tiles (unpassable for tanks). A code will make that more clear:
[source lang="csharp"] bool Collision(Tank Object) { List<Tile> collided = new List<Tile>(); Rectangle objectRect = Object.GetRectangle(); if (!playableArea.Contains(objectRect)) return true; foreach (var tank in Tanks) if (!Object.Equals(tank) &amp;&amp; objectRect.Intersects(tank.GetRectangle())) return true; foreach (var tile in waterTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in metalTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in brickTile) if (objectRect.Intersects(tile.BoundingBox)) collided.Add(tile); if (collided.Count > 0) return PixelPerfectCollision(Object, collided); return false; }[/source]
As you can see, I structured collision checks so that the check for collision with brick wall (destructible) is last, enabling the function to exit early if it find collision with objects that don't need the pixel-perfect collision check.

I'm a bit confused on the way that you're calculating the offset variable. If the Tank is more than the totalWidth or totalHeight from the origin, won't your x and y indexes go out of range? E.g. in the following loop if tankBox.left > offset.X + totalWidth, x will not be a valid index for wallColor and tankColor. I've probably missed something, it's late

I've taken care of it too. There are 2 reasons why that won't happen (unless I'm gravely mistaken which I'll find out after I change resolution):
• Pixel-perfect collision check is ran if Bounding Box collision check is positive, meaning that tank and walls will always be inside 2*2 tile (80*80 pixel at 800*600 resolution) area, hence
int totalWidth = Math.Max(Wall[0].BoundingBox.Width, tankBox.Width) * 2;
int totalHeight = Math.Max(Wall[0].BoundingBox.Height, tankBox.Height) * 2;
Incidentally, if tankBox.left > offset.X + totalWidth pixel-perfect collision check won't be invoked since it will fail on bounding box collision check.
• Vector2 offset is simply a location of top left pixel whose usage is to ease the iteration through 2D array. I used it specifically to cover the situation where the tank is on the left and/or above the top-left wall tile (I can draw all possible situations).

If your PixelPerfectCollision method is a problem performance-wise then you could combine the first two for loops together, and you could combine the last two for loops together.

Combining last 2 double loops I didn't notice Thanks for suggestion. Originally it was from 0 to totalWidth/totalHeight but when I debugged it I saw I didn't cover few cases and added offset into calculation, resulting in equality of those two loops.

Well, off to the sleep, and tomorrow I'm continuing with my work. To quote Natsu from Fairy Tail: "I'm all fired up!"

### #2Aurioch

Posted 04 December 2012 - 06:25 PM

Wow, thank you very much for detailed explanations. I had a vague understanding of it, now it's everything more clear
And honestly, if I ever did encounter encapsulation that would be in my "old" VB.NET book. On university, while we're allowed to use common languages (C# being one of them), we actually never studied about object-oriented programming at all, only procedural ( C ). I was baffled when my friend told me that one of his teammates for Project class wrote a finite state machine in C# completely procedural without usage of object methods and classes. So, explanations like your are pure gold for me.

I haven't done pixel-perfect collision detection before but I would start by comparing bounding boxes to check that the tank is colliding with a particular wall segment, and if the bounding boxes are colliding only check the area where the two bounding boxes overlap for a collision. Also I would check one wall at a time so that if you find a collision early on you can return true and don't have to check the other walls.

I already did that ;) There is no need to perform pixel-perfect collision for objects that won't be modified over the course of the game like water tiles (unpassable for tanks). A code will make that more clear:
[source lang="csharp"] bool Collision(Tank Object) { List<Tile> collided = new List<Tile>(); Rectangle objectRect = Object.GetRectangle(); if (!playableArea.Contains(objectRect)) return true; foreach (var tank in Tanks) if (!Object.Equals(tank) &amp;&amp; objectRect.Intersects(tank.GetRectangle())) return true; foreach (var tile in waterTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in metalTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in brickTile) if (objectRect.Intersects(tile.BoundingBox)) collided.Add(tile); if (collided.Count > 0) return PixelPerfectCollision(Object, collided); return false; }[/source]
As you can see, I structured collision checks so that the check for collision with brick wall (destructible) is last, enabling the function to exit early if it find collision with objects that don't need the pixel-perfect collision check.

I'm a bit confused on the way that you're calculating the offset variable. If the Tank is more than the totalWidth or totalHeight from the origin, won't your x and y indexes go out of range? E.g. in the following loop if tankBox.left > offset.X + totalWidth, x will not be a valid index for wallColor and tankColor. I've probably missed something, it's late

I've taken care of it too. There are 2 reasons why that won't happen (unless I'm gravely mistaken which I'll find out after I change resolution):
• Pixel-perfect collision check is ran if Bounding Box collision check is positive, meaning that tank and walls will always be inside 2*2 tile (80*80 pixel at 800*600 resolution) area, hence
int totalWidth = Math.Max(Wall[0].BoundingBox.Width, tankBox.Width) * 2;
int totalHeight = Math.Max(Wall[0].BoundingBox.Height, tankBox.Height) * 2;
Incidentally, if tankBox.left > offset.X + totalWidth pixel-perfect collision check won't be invoked since it will fail on bounding box collision check.
• Vector2 offset is simply a location of top left pixel whose usage is to ease the iteration through 2D array. I used it specifically to cover the situation where the tank is on the left and/or above the top-left wall tile (I can draw all possible situations).

If your PixelPerfectCollision method is a problem performance-wise then you could combine the first two for loops together, and you could combine the last two for loops together.

Combining last 2 double loops I didn't notice Thanks for suggestion. Originally it was from 0 to totalWidth/totalHeight but when I debugged it I saw I didn't cover few cases and added offset into calculation, resulting in equality of those two loops.

Well, off to the sleep, and tomorrow I'm continuing with my work. To quote Natsu from Fairy Tail: "I'm all fired up!"

### #1Aurioch

Posted 04 December 2012 - 06:17 PM

Wow, thank you very much for detailed explanations. I had a vague understanding of it, now it's everything more clear
And honestly, if I ever did encounter encapsulation that would be in my "old" VB.NET book. On university, while we're allowed to use common languages (C# being one of them), we actually never studied about object-oriented programming at all, only procedural ( C ). I was baffled when my friend told me that one of his teammates for Project class wrote a finite state machine in C# completely procedural without usage of object methods and classes. So, explanations like your are pure gold for me.

I haven't done pixel-perfect collision detection before but I would start by comparing bounding boxes to check that the tank is colliding with a particular wall segment, and if the bounding boxes are colliding only check the area where the two bounding boxes overlap for a collision. Also I would check one wall at a time so that if you find a collision early on you can return true and don't have to check the other walls.

I already did that ;) There is no need to perform pixel-perfect collision for objects that won't be modified over the course of the game like water tiles (unpassable for tanks). A code will make that more clear:
[source lang="csharp"] bool Collision(Tank Object) { List<Tile> collided = new List<Tile>(); Rectangle objectRect = Object.GetRectangle(); if (!playableArea.Contains(objectRect)) return true; foreach (var tank in Tanks) if (!Object.Equals(tank) && objectRect.Intersects(tank.GetRectangle())) return true; foreach (var tile in waterTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in metalTile) if (objectRect.Intersects(tile.BoundingBox)) return true; foreach (var tile in brickTile) if (objectRect.Intersects(tile.BoundingBox)) { collided.Add(tile); return PixelPerfectCollision(Object, collided); } return false; }[/source]
As you can see, I structured collision checks so that the check for collision with brick wall (destructible) is last, enabling the function to exit early if it find collision with objects that don't need the pixel-perfect collision check.

I'm a bit confused on the way that you're calculating the offset variable. If the Tank is more than the totalWidth or totalHeight from the origin, won't your x and y indexes go out of range? E.g. in the following loop if tankBox.left > offset.X + totalWidth, x will not be a valid index for wallColor and tankColor. I've probably missed something, it's late

I've taken care of it too. There are 2 reasons why that won't happen (unless I'm gravely mistaken which I'll find out after I change resolution):
• Pixel-perfect collision check is ran if Bounding Box collision check is positive, meaning that tank and walls will always be inside 2*2 tile (80*80 pixel at 800*600 resolution) area, hence
int totalWidth = Math.Max(Wall[0].BoundingBox.Width, tankBox.Width) * 2;
int totalHeight = Math.Max(Wall[0].BoundingBox.Height, tankBox.Height) * 2;
Incidentally, if tankBox.left > offset.X + totalWidth pixel-perfect collision check won't be invoked since it will fail on bounding box collision check.
• Vector2 offset is simply a location of top left pixel whose usage is to ease the iteration through 2D array. I used it specifically to cover the situation where the tank is on the left and/or above the top-left wall tile (I can draw all possible situations).

If your PixelPerfectCollision method is a problem performance-wise then you could combine the first two for loops together, and you could combine the last two for loops together.

Combining last 2 double loops I didn't notice Thanks for suggestion. Originally it was from 0 to totalWidth/totalHeight but when I debugged it I saw I didn't cover few cases and added offset into calculation, resulting in equality of those two loops.

Well, off to the sleep, and tomorrow I'm continuing with my work. To quote Natsu from Fairy Tail: "I'm all fired up!"

PARTNERS