I wanna implement collision but I’m not sure how I would go about this… Here’s the situation
I want to check if pet.cs is colliding with tile.cs
I have a list containing pets(pet.cs) called Pets so… I would have to handle collision for each and every pet.
I’m not sure how I would go about this. Every time I write the code for pet, would it have to be this?
for (int i = 0; i < Pets.Count; i++){
for (int j = 0; j < Tiles.Count; j++){
if (Pets[i].bounds.Intersects(Tiles[j].bounds)){
//collision handling
}
}
I don’t know… that obviously doesn’t work… and it didn’t…
You are right you need to loop through each pet and check it with every tile in the game with each pet.
The code should detect the collisions but you still need to write what happens when they collide.
But you only need to call this once per update.
On the long run you should redesign your class hierarchy, and you should implement something like (pseudo code coming):
interface ICollidable
{
bool Collide(List<ICollidable>);
}
public class Pet : ICollidable
{
public bool Collide(List<ICollidable> objects)
{
//....Intersect(...)
}
}
public class Tile : ICollidable
{
//I leave it for you to figure it out :)
}
And in your Update you would call:
foreach(var pet in Pets)
pet.Collide(Tiles);
The benefit of this approach is, that even your Tiles can collide with other objects. Of course, the parameter’s type in the Collide method can (and possibly will) be something else, something like a List < IDrawable > or something more “generic”, but it dpeneds on your design.
No offence intended, but it seems to me the other commenters may have missed the question, I think the OP is wondering why the collision test in the example code is failing? I apologise if I’m wrong.
This is where the problem is:
I have a feeling your ‘bounds’ only returns the local boundaries of your objects rather than the collision boundaries. Basically you’d need to offset the local boundaries by the position of the object to get the collision boundaries, then perform ‘Intersects’ on those.
I read it once again, and while I answered the “Every time I write the code for pet, would it have to be this?” question, I did missed the other one under the code.
So, yes, the “bounds” (whatever it is) must be the actual drawing rectangle and not the original from where the object comes. In my engine I have two rectangles, one for the original and one for the actual. So, the actual rectangles intersection must give back true, if they collide.
One hint for testing: add only one pet and only one tile to your game, with predefined positions that intersect with each other. Add a breakpoint to the collision detection and see what is the return value. From this you can easily make sure if your collision detection is wrong, or the actual rectangles did not update their values properly.
I think your code looks reasonable - though I’ve never done collision with the ‘intersects’, it looks neat.
Have you looked back on your rectangles to check their bounds? Verified in the code that actually do collide?
I drew a box for where the bounds are, they’re clearly intersecting in game, but no reaction. It should pop a text up. I even chose two specific Pets (Pets[0] and Pets[2]) to check for collision but it’s not working. I’m getting updated with the position of the hit boxes so I know they’re moving and being updated. I just don’t know how to do this
you need to post your bound creation and update code for us to see.
In Pet.cs
Rectangle petbounds = new Rectangle((int)position.X, (int)position.Y, Width, Height);
I’m able to check if Pets[0] intersects with Pet[2] now, I forgot a simple thing. But now I’m trying to make it so it can check if any pet is intersecting with another. I made another thread about it