Efficient 2d collision [SOLVED]
Posted: Sat Feb 13, 2010 4:50 pm
Heya!
To start off, I'm very new to game development so this might be an incredibly dumb question and if it is very annoying to answer, you don't have to bother =P
Okay, so I'm making a 2d game with C++/SDL and I've just started making movement and collision checking for my character. My game screen is basically built up of a 10x10 grid. I have an array called "objects[10][10]" to hold this information and there's an collision check for these 100 objects that works great, but it seems very ineffective.
Every frame i call a function named "move()" uses my velocities to move the character and checks for collision. The collision checking is simply a loop that goes through my "object[10][10]"-array and if there is an object on some location in the array I put values into an temporary rectangle that I'll then use for collision detecting. So I call another function named "collision(SDL_Rect, SDL_Rect)" and use my characters offsets and the temporary rectangle as the paremeters.
This means that I check if there is an object on every location of my array every frame. If there would be like 30 objects on the map I would also check for collision 30 times every frame and this seems like a very slow way to do this. So my question is basically if someone could explain a more efficient way to do this or give me an URL to a guide or something of that kind.
Thanks for your help! =)
To start off, I'm very new to game development so this might be an incredibly dumb question and if it is very annoying to answer, you don't have to bother =P
Okay, so I'm making a 2d game with C++/SDL and I've just started making movement and collision checking for my character. My game screen is basically built up of a 10x10 grid. I have an array called "objects[10][10]" to hold this information and there's an collision check for these 100 objects that works great, but it seems very ineffective.
Every frame i call a function named "move()" uses my velocities to move the character and checks for collision. The collision checking is simply a loop that goes through my "object[10][10]"-array and if there is an object on some location in the array I put values into an temporary rectangle that I'll then use for collision detecting. So I call another function named "collision(SDL_Rect, SDL_Rect)" and use my characters offsets and the temporary rectangle as the paremeters.
This means that I check if there is an object on every location of my array every frame. If there would be like 30 objects on the map I would also check for collision 30 times every frame and this seems like a very slow way to do this. So my question is basically if someone could explain a more efficient way to do this or give me an URL to a guide or something of that kind.
Thanks for your help! =)