As you probably have guessed already, collision, even simple, is a pretty hard and confusing topic.
I once dabbled with it myself, and came up with a solution to simple 2D rectangle collision. I have no idea how to translate it to balls, so, with my solution, your player is now a square - or any rectangle, if you wish.
I’ve modified your sketch, removed all the constraint things, and made collisions.
static final int DIAM = 48, SPD = 4, FPS = 60;
static final color BG = 0350;
Player p;
ArrayList<Wall> walls;
void setup() {
size(800, 600, JAVA2D);
smooth(4);
frameRate(FPS);
ellipseMode(CENTER);
fill(Player.INK);
stroke(Player.OUTLINE);
strokeWeight(Player.BOLD);
p = new Player(width>>1, height>>1, DIAM, DIAM, SPD);
walls = new ArrayList<Wall>();
//Off-screen screen borders. May cause black outlines because Processing tries to draw them as they are just off the screen.
walls.add(new Wall(-50,-50,width,50));
walls.add(new Wall(-50,-50,50,height+100));
walls.add(new Wall(width,-50,50,height+100));
walls.add(new Wall(-50,height,width+100,50));
//Any old walls.
walls.add(new Wall(100,100,200,100));
walls.add(new Wall(200,150,100,100));
}
void draw() {
background(BG);
p.move();
p.display();
for(Wall i:walls) i.render();
}
int pos(int x,int y){
return (y)*width+x;
}
void keyPressed() {
p.setMove(keyCode, true);
}
void keyReleased() {
p.setMove(keyCode, false);
}
class Wall{
int x, y, sizex, sizey;
Wall(int x, int y,int sizex, int sizey){
this.x = x; this.y = y; this.sizex = sizex; this.sizey = sizey;
}
void render(){
rect(x,y,sizex,sizey);
}
}
int minSignless(int a1, int a2){
//Figures which number is closer to zero.
return abs(a1)-abs(a2)>0?a2:a1;
}
final class Player {
static final color INK = #008000, OUTLINE = 0;
static final float BOLD = 2.0;
boolean isLeft, isRight, isUp, isDown;
int x, y;
final int sizex, sizey, v;
Player(int xx, int yy, int xs, int ys, int vv) {
x = xx;
y = yy;
sizex = xs;
sizey = ys;
v = vv;
}
void display() {
rect(x, y, sizex, sizey);
}
void move() {
int movementX = v*(int(isRight) - int(isLeft));
int movementY = v*(int(isDown) - int(isUp));
for(Wall i:walls){
// if(movementX == 0 && movementY == 0) break; //If we aren't moving, break out of the for loop.
PVector result = figureCollision(x,y,sizex,sizey,movementX,movementY,i);
movementX = minSignless(movementX,(int)result.x);
movementY = minSignless(movementY,(int)result.y);
}
x+=movementX;
y+=movementY;
}
boolean setMove(int k, boolean b) {
switch (k) {
case 'W':
case UP:
return isUp = b;
case 'S':
case DOWN:
return isDown = b;
case 'A':
case LEFT:
return isLeft = b;
case 'D':
case RIGHT:
return isRight = b;
default:
return b;
}
}
}
boolean collision(int x, int y, int sizex, int sizey, Wall wall){
return !(x>=wall.x+wall.sizex||y>=wall.y+wall.sizey||x+sizex<=wall.x||y+sizey<=wall.y);
}
boolean collisionPreciser(int x, int y, int sizex, int sizey, int movx, int movy, Wall wall, int precision){
//Checks collision "precision" times so that the object won't happen to jump over if it's flying too rapidly fast.
if(precision<=1) return collision(x+movx,y+movy,sizex,sizey,wall);
for(int i=1;i<=precision;i++){
if(collision(x+movx/i,y+movy/i,sizex,sizey,wall)) return true;
}
return false;
}
static final int collisionQuality = 2;
//Bump this up if you'll have really thin walls or really fast players - else stuff will fly through.
PVector figureCollision(int x, int y, int sizex, int sizey, int movx, int movy, Wall wall){
//Figures what movx and movy should be so that it wouldn't collide with this wall.
//Check vertical collision...
if(collisionPreciser(x,y,sizex,sizey,0, movy,wall,collisionQuality)){
if(y+sizey/2>wall.y+wall.sizey/2){
//1 is below the wall...
return new PVector(movx, (wall.y+wall.sizey)-y);
}else{
return new PVector(movx, wall.y-(y+sizey));
}
}
if(collisionPreciser(x,y,sizex,sizey,movx,0,wall,collisionQuality)){
if(x+sizex/2>wall.x+wall.sizex/2){
return new PVector((wall.x+wall.sizex)-x,movy);
}else{
return new PVector(wall.x-(x+sizex),movy);
}
}
return new PVector(movx,movy);
}
Off-topic: I haven’t slept for the last 26 hours and drank (int)random(6,19)
cups of coffee, and this exercise is the thing that finally made me want to go sleep. I’ll probably go sleep now. °¬°