# How to manage multiple objects for a tile based game for collision?

So I'm making the tile-based 2D sandbox platformer...thing to get an idea of how to manage different elements of coding games.

Currently I'm at the point where my collision system works fine for a few objects, but I want to optimize it. The way it works is it checks what direction the object is moving in and by using the array that contains the locations of the different objects in the game, decide if there is a collision taking place.

This works great for objects that aren't moving, but I'm not sure what to do about moving objects. I haven't got a very good idea of how to solve this issue, but what I'm thinking of doing is the following.

1. Move objects
2. Update the level array to reflect the new positions of the objects
3. Revert the previous changes made to the array (As in, if the player moved, set his previous position in the array to a null value so it doesn't affect any collisions later)
4. Check for collision

This way I can assign numerical values to each object type in case I want collision between certain objects to have certain outcomes.

Does this make sense? Is there a better way of going about this?

I didn't upload any of my code because I didn't think it was relevant. Sorry if certain things I said are vague, not very good at explaining things. Thanks in advance!

``````static function checkCollision(player:MovieClip, platforms:Array)
{
//Get the speed of the player
var speedX:Number = player.vx;
var speedY:Number = player.vy;
//Variables that will hold the I,J coordinates of
//which tile the player is currently in
var playerJ:int = player.x/20;
var playerI:int = player.y/20;
//Test whether the player is moving downwards
if(speedY >= 0)
{
//Test whether the tile beneath the player is empty or not
if(platforms[playerI + 1][playerJ] != null)
{

if(platforms[playerI + 1][playerJ] != 0)
{
//If player is on ground act accordingly
player.y = playerI*20;
player.grounded = true;
player.vy = 0;
}
else
{
//Otherwise
player.grounded = false;
}
}
}
//Test whether the player is moving upwards
else if (speedY < 0)
{
if(platforms[playerI + 1][playerJ] != null)
{
playerI = (player.y + player.height)/20
if(platforms[playerI - 1][playerJ] != 0)
{
player.y = playerI*20;
player.vy = 0;
}
}
}
//Figure out which direction the player is moving in
//If its moving left
if(speedX < 0)
{
//Set the playerJ to the tile the player is in
playerJ = (player.x + player.width)/20
//Test the tile to the left of the player
if(platforms[playerI][playerJ - 1] != 0)
{
//Move player accordingly
player.x = playerJ*20;
player.vx = 0;
}
}
//If its moving right
else if(speedX > 0)
{
//Test the tile to the right of the player
if (platforms[playerI][playerJ + 1] != 0)
{
//Move player accordingly
player.x = playerJ*20;
player.vx = 0;
}
}
}
``````
-
Can you give a bit more detail on your current collision detection process? Some code would be nice, even if it's just pseudo-code. –  DBRalir Mar 30 '13 at 22:13
Added the code for the collision detection as requested –  inzombiak Mar 30 '13 at 22:19