Define the player’s position within this grid using X and Y indices.
3. When the player tries to move, check whether they are about to collide with an obstacle in the grid by calculating their new cell index based on their current position and direction of movement. If that cell is true (obstacle), then a collision has occurred.
4. Use this information to either prevent the player from moving or adjust their position accordingly.
Here’s some sample code for implementing grid-based collision detection in Processing:
//build a grid
int rows = 20;
int columns = 20;
boolean[][] grid = new boolean[rows][columns];
//where the player is in the grid
int playerIndexX = 10;
int playerIndexY = 10;
void setup() {
size(300, 300);
//randomly place obstacles in the grid
for (int row = 0; row < grid.length; row++) {
for (int column = 0; column < grid[row].length; column++) {
//each cell has a 20% chance of being an obstacle
if (random(1) < .2) {
grid[row][column] = true;
}
}
}
}
void draw() {
background(128);
float cellWidth = width/columns;
float cellHeight = height/rows;
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
float cellX = cellWidth*column;
float cellY = cellHeight*row;
//fill the obstacles in with red
if (grid[row][column]) {
fill(255, 0, 0);
}
else {
noFill();
}
rect(cellX, cellY, cellWidth, cellHeight);
}
}
//fill the player's cell with green
float playerPixelX = playerIndexX * cellWidth;
float playerPixelY = playerIndexY * cellHeight;
fill(0, 255, 0);
rect(playerPixelX, playerPixelY, cellWidth, cellHeight);
}
void keyPressed() {
if (keyCode == UP) {
//if we aren't in the top row and the cell above us doesn't contain an obstacle
//then we can move up
int newRow = playerIndexY 1;
if (newRow >= 0 && !grid[newRow][playerIndexX]) {
playerIndexY–;
}
}
else if (keyCode == DOWN) {
//if we aren’t in the bottom row and the cell below us doesn’t contain an obstacle
//then we can move down
int newRow = playerIndexY + 1;
if (newRow < rows && !grid[newRow][playerIndexX]) {
playerIndexY++;
}
}
else if (keyCode == LEFT) {
//if we aren't in the left column and the cell to our left doesn't contain an obstacle
//then we can move left
int newColumn = playerIndexX 1;
if (newColumn >= 0 && !grid[playerIndexY][newColumn]) {
playerIndexX–;
}
}
else if (keyCode == RIGHT) {
//if we aren’t in the right column and the cell to our right doesn’t contain an obstacle
//then we can move right
int newColumn = playerIndexX + 1;
if (newColumn < columns && !grid[playerIndexY][newColumn]) {
playerIndexX++;
}
}
In this example, we're using a boolean array to represent our game world as a grid of cells. Each cell is either true or false depending on whether it contains an obstacle (true) or not (false). We then define the player's position within this grid using X and Y indices. When the player tries to move, we calculate their new cell index based on their current position and direction of movement. If that cell is true (obstacle), then a collision has occurred and we prevent the player from moving or adjust their position accordingly.
Maybe some examples would help me understand better.