Movement and Collision Detection
in
Programming Questions
•
7 months ago
I have been creating a game in Processing and have been having a little trouble working out movement controls for the player character.
To try and get my head around it I've created a second sketch to test some ideas, and have managed to cobble together some simple collision detection between boxes (My game makes heavy use of bounding boxes to perform collision detection).
In its current state I'm able to move the player box up to the environment box, and it detects the collision when there is one, but I'm a little confused about how to go about allowing the player to slide against the box. In its current state, if you move diagonally against the bounding box, it will stop for the duration of the collision being detected. While I can see why it does this, I'm struggling to think of a way to solve the issue...
I've included my code below, thanks in advance for any assistance! :)
Collision
- Box player1, environment;
- final static int NORTH = 1;
- final static int EAST = 2;
- final static int SOUTH = 4;
- final static int WEST = 8;
- int result;
- void setup()
- {
- player1 = new Box(20, 20, 50, 50);
- environment = new Box(200, 200, 50, 50);
- size(300,300);
- }
- void draw()
- {
- background(0);
- player1.move();
- player1.drawBox();
- environment.drawBox();
- }
- int isColliding(Box box1, Box box2)
- {
- if (!((box1.posX + box1.sizeX) >= box2.posX)) return 0;
- if (!(box1.posX <= (box2.posX + box2.sizeX))) return 0;
- if (!((box1.posY - box1.sizeY) <= box2.posY)) return 0;
- if (!(box1.posY >= (box2.posY - box2.sizeY))) return 0;
- return 1;
- }
- void keyPressed(){
- switch(key) {
- case('w'):case('W'):result |=NORTH;break;
- case('d'):case('D'):result |=EAST;break;
- case('s'):case('S'):result |=SOUTH;break;
- case('a'):case('A'):result |=WEST;break;
- }
- }
- void keyReleased(){
- switch(key) {
- case('w'):case('W'):result ^=NORTH;break;
- case('d'):case('D'):result ^=EAST;break;
- case('s'):case('S'):result ^=SOUTH;break;
- case('a'):case('A'):result ^=WEST;break;
- }
- }
- class Box
- {
- int posX, posY, sizeX, sizeY;
- Box(int x1, int y1, int x2, int y2)
- {
- posX = x1;
- posY = y1;
- sizeX = x2;
- sizeY = y2;
- }
- void drawBox()
- {
- if (isColliding(player1, environment) == 1)
- {
- fill(255, 0, 0);
- }
- else
- {
- fill(255);
- }
- rect(posX, posY, sizeX, sizeY);
- }
- void move()
- {
- switch(result)
- {
- case NORTH:
- posY--;
- if (isColliding(player1, environment) == 1) posY++;
- break;
- case EAST:
- posX++;
- if (isColliding(player1, environment) == 1) posX--;
- break;
- case SOUTH:
- posY++;
- if (isColliding(player1, environment) == 1) posY--;
- break;
- case WEST:
- posX--;
- if (isColliding(player1, environment) == 1) posX++;
- break;
- case NORTH|EAST:
- posY--;
- posX++;
- if (isColliding(player1, environment) == 1)
- {
- posY++;
- posX--;
- }
- break;
- case NORTH|WEST:
- posY--;
- posX--;
- if (isColliding(player1, environment) == 1)
- {
- posY++;
- posX++;
- }
- break;
- case SOUTH|EAST:
- posY++;
- posX++;
- if (isColliding(player1, environment) == 1)
- {
- posY--;
- posX--;
- }
- break;
- case SOUTH|WEST:
- posY++;
- posX--;
- if (isColliding(player1, environment) == 1)
- {
- posY--;
- posX++;
- }
- break;
- }
- }
- }
1