Collision detection between player and an object

I’m writing a game where the player has to push all of the blocks into the holes in as few moves as possible, but I’m having trouble getting the player colliding with the blocks correctly.

I know I must be doing something VERY inefficiently. The only way I could think to do it was to call a new block like this -

this.block1 = new Block();
this.block2 = new Block();

and to keep doing that until I had as many as I needed. But something about that seems wrong.

Any help?

Thanks,
-Nathan

What seems wrong about it?

When I go to check collisions then, I’m going to have to do something like this… -

if ((playerX == block1X) && (playerX == block1Y))
{
	if (direction == 1)
	{
		block1X += 1;
		playerX += 1;
	}
	if (direction == 2)
	{
		block1X -= 1;
		playerX -= 1;
}

…and so on for all four directions and as many blocks as there are.

p.s. I know that code ^^ wouldn’t work for moving blocks around, just wanted to quickly write something up

Oh you are going to have block1, block2, …blockN? How about you use a 2D array that represents the grid, then when the character moves to a new spot, you check if the new spot has a block and then move that block in the same direction.

Can you give me a quick lesson on arrays? I have never used them before in programming. I know it’s something like

int grid[] = *....*

but I don’t know how that’s done.

This should help :slight_smile:

Thanks! ;D

Is there a way I can read an array and draw my map based on that?

Example -

int[] anArray = {
				0, 1, 2,
				1, 2, 1,
				2, 1, 2
		};

makes

P B H
B H B
H B H

where P = player, B = block, and H = hole.

Use a 2D array:


int[][] myMap = { { 0, 1, 2 },
                  { 1, 2, 0 },
                  { 2, 0, 1 } };

What is the difference between a 2D array and a regular one? All I see is a few more brackets, haha.

A 2D array is basically an array of arrays. When using a normal 1 dimensional array, you get elements with an index: int aNumber = myArray[n];
With a 2 dimensional array, each element of the array is an array too. int[] deeperArray = myArray[n];
So, effectively you could use this as the X and Y axis: int aNumber = myArray[x][y];

Ok… so…

I have this 2D array

int[][] myMap = {{
				0, 1, 2,
				1, 2, 1,
				2, 1, 2
		}};

then I call this

int aNumber = myMap[x][y];

according to your little example-thing. My question: what does that second bit of code do?

Let’s say x = 0 and y = 1, myMap[x][y] would get you the “1” on the first row, second column.
Read up some more on arrays so you can get comfortable with them.

Will-do. Thanks

Ok, so I’m trying to do tile based collison detection like this -

int[][] myMap = {{
				0, 1, 2,
				1, 2, 1,
				2, 1, 2
		}};
		
		for (int x = 0; x < 3; x++)
		{
			for (int y = 0; y <= 1; y++)
			{
				int aNumber = myMap[x][y];
				if (aNumber == 1)
				{
					//collides with block
				}
				if (aNumber == 2)
				{
					//falls in hole
				}
			}
		}

But it throws this error -

“Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1”

What is going wrong with it? I tried debugging and learned nothing.

Try defining myMap like this:


int[][] myMap = {
            {0, 1, 2},
            {1, 2, 1},
            {2, 1, 2}
      };

Thanks! I’ve been trying to figure this out for an hour.

+1 to you :slight_smile:

When you’re dealing with a 2d array for your map, it may help think of it in terms of rows and columns, not x and y. In that instance, y = the rows (because they’re vertical) and x = the columns (because they’re horizontal), which REALLY tripped me up when displaying my tile map in the game I’m making, because it seems completely backwards.

Just something to keep in mind.