# How to make the character cannot pass through other randomly moving objects when they are collided?

As the title above, how can i make my random wallImg solid enough so that my birdImg cannot pass through it from any sides of the wallImg? Here's the code of Processing's Crappy Bird that i've altered so my game will be running upside down instead of sidescrolling. Any help will be appreciated

PImage backImg;
PImage birdImg;
PImage wallImg;
PImage startImg;

int gamestate = 1;
int score = 0;
int highScore = 0;
float speed;
boolean moveLeft, moveRight, moveUp, moveDown;

int x = -200;
int y;
int vy = 0;
int wx[] = new int[2];
int wy[] = new int[2];

//initiate and declare
Bird playerBird = new Bird (x, y, vy);

void setup()
{
size(600,800);
fill(0);
textSize(40);
speed = 2;

}

void draw()
{
if(gamestate == 0) {
imageMode(CORNER);
image(backImg, 0, x);
image(backImg, 0,x-backImg.height);
x += 6;
vy += 1;
y += vy;
if(x >= 800) x = 0;
for(int i = 0 ; i < 2; i++) {
imageMode(CENTER);
image(wallImg, wy[i] - (wallImg.width/2+100),wx[i]);
image(wallImg, wy[i] + (wallImg.width/2+100), wx[i]);
if(wx[i] > 900) {
wy[i] = (int)random(200,width-200);
wx[i] = 0;
}
if(wx[i] == width/2) highScore = max(++score, highScore);
if((abs(width/2-wx[i])<25 && abs(y-wy[i])>100))playerBird.y=wy[i];
wx[i] += 6;
}
playerBird.y = y;
playerBird.drawBird();
text(""+score, width/2-15, 700);
}
else {
imageMode(CENTER);
image(startImg, width/2,height/2);
text("High Score: "+highScore, 50, width);
}
}

void mousePressed() {
if(gamestate==1) {
wx[0] = 400;
wy[0] = y = width/2;
wx[1] = 0;
wy[1] = 600;
x = gamestate = score = 0;
}
}
void keyPressed() {
// vy = -17;
if(keyCode == UP)
{
vy = -17;
}
if (key == CODED) {
if (keyCode == LEFT) {
moveLeft = true;
} else if(keyCode == RIGHT) {
moveRight = true;
}
}
}

void keyReleased() {
vy=-17;
if (key == CODED) {
if (keyCode == LEFT) {
moveLeft = false;
} else if(keyCode == RIGHT) {
moveRight = false;
}
}
}

//bird class
class Bird
{
int x;
int y;
int vy;
float speed;
float xPos;

//constructor
Bird(int tempX,int tempY,int tempVy)
{
x= tempX;
y= tempY;
vy= tempVy;
speed = 5;
xPos = width/2;
}

void drawBird(){
image(birdImg, xPos , y);
if(moveLeft&&xPos>=20) xPos -= speed;
if(moveRight&&xPos<=580)xPos += speed;
}
}

I manage to get the character to be able to stay on top of the platform 'wallImg' from above but I can't make my character from jumping through the platform from below. Any ideas?

PImage backImg;
PImage birdImg;
PImage wallImg;
PImage startImg;

int gamestate = 1;
int score = 0;
int highScore = 0;
float speed;
boolean moveLeft, moveRight, moveUp, moveDown;
int minX;
int maxX;

int x = -200;
int y;
int vy = 0;
int wx[] = new int[2];
int wy[] = new int[2];

//initiate and declare
Bird playerBird = new Bird (x, y, vy);

void setup()
{
size(600,800);
fill(0);
textSize(40);
speed = 2;

}

void draw()
{
if(gamestate == 0) {
imageMode(CORNER);
image(backImg, 0, x);
image(backImg, 0,x-backImg.height);
x += 6;
vy += 1;
y += vy;
if(x >= 800) x = 0;
for(int i = 0 ; i < 2; i++) {
imageMode(CENTER);
image(wallImg, wy[i] - (wallImg.width/2+100),wx[i]);
image(wallImg, wy[i] + (wallImg.width/2+100), wx[i]);
if(wx[i] > 900) {
wy[i] = (int)random(200,width-200);
wx[i] = 0;
}
minX = wy[i] - (wallImg.width/2+400);
maxX = wy[i] + (wallImg.width/2);
if(wx[i] == width/2) highScore = max(++score, highScore);

if((abs(width/2-wy[i])<25 && abs(y-wy[i])>maxX)){
playerBird.y=wy[i];

}
/*COLLISION */
int collisionTestY1 = wx[i] - 50;
int collisionTestY2 = wx[i] + 50;
int collisionTestX1 = wy[i] + 100;
int collisionTestX2 = wy[i] + (wallImg.width/2);
int collisionTestXX1 = wy[i] - 100;
int collisionTestXX2 = wy[i] - (wallImg.width/2);

if(playerBird.xPos >= collisionTestX1 && playerBird.xPos <= collisionTestX2 && playerBird.y <= collisionTestY2 && playerBird.y >= collisionTestY1){
vy=0;
print("lol");
}

if(playerBird.xPos <= collisionTestXX1 && playerBird.xPos >= collisionTestXX2 && playerBird.y <= collisionTestY2 && playerBird.y >= collisionTestY1){
vy=0;
}
//line(collisionTestX1,collisionTestY1,collisionTestX2,collisionTestY1);
//line(wy[i]-100,wx[i]+10,minX,wx[i]+10);
//line(wy[i],wx[i],wy[i] - wallImg.width/2,wx[i]);
wx[i] += 6;
}
playerBird.y = y;
playerBird.drawBird();
text(""+score, width/2-15, 700);
}
else {
imageMode(CENTER);
image(startImg, width/2,height/2);
text("High Score: "+highScore, 50, width);
}
}
void mousePressed() {
if(gamestate==1) {
wx[0] = 400;
wy[0] = y = width/2;
wx[1] = 0;
wy[1] = 600;
x = gamestate = score = 0;
}
}
void keyPressed() {
// vy = -17;
if(keyCode == UP)
{
vy = -17;
}
if (key == CODED) {
if (keyCode == LEFT) {
moveLeft = true;
} else if(keyCode == RIGHT) {
moveRight = true;
}
}
}
void keyReleased() {
vy=-17;
if (key == CODED) {
if (keyCode == LEFT) {
moveLeft = false;
} else if(keyCode == RIGHT) {
moveRight = false;
}
}
}
//bird class
class Bird
{
int x;
int y;
int vy;
float speed;
float xPos;

//constructor
Bird(int tempX,int tempY,int tempVy)
{
x= tempX;
y= tempY;
vy= tempVy;
speed = 5;
xPos = width/2;
}
void drawBird(){
image(birdImg, xPos , y);
if(moveLeft&&xPos>=20) xPos -= speed;
if(moveRight&&xPos<=580)xPos += speed;
}
}

You have to write your own collision checks and solve collisions when they occur. Processing has no build in collision checks.

Solving collisions can mean several things:

1. Mostly it means, that you cancel the movement that created the collision. Or you modify the movement to another direction eith lower magnitude in another direction. (Imagine running diagonally towards a wall, when you hit it you might want to move further along the wall)

2. Or it means that you start some event(s) when your collision ist registered (For example "Game over" or activating something when you step on something)

You also have several ways of detecting a collision:

1. You could do pixel wise checks (checking if individual non-transparent pixels of the images overlap)
2. You could use hitboxes with simpler shapes and check them against each other (Bounding rectangles or bounding spheres)
3. You could use sensors to check if something is right mexzlt to your objects and adjust xour movement accordingly. (I don't recommend this option, as it can be tedious to implement if your background is multicolor or your game gets more and more complex. Hitboxes are imo cleaner)
• I'm sorry I'm such a dumb, especially in syntax and logics. Im planning to cancel the velocity after collision using bounding box and I tried everything but now I'm out of depth :'( Commented Dec 10, 2015 at 10:39
• @user3132527 What kind of bounding boxes do you use? Do you need help actually implementing the collision checks between those? If that's the case do some research, there are plenty of ressources on simple 2D collision checks. such as this Commented Dec 10, 2015 at 11:39

Assuming that your walls are squares/rectangles, and that your bird is also squareish (if it isn't the collision detection in the method I'm going to show you might be a bit funny)

int x1 = objA.X;
int x2 = objB.X;
int y1 = objA.Y;
int y2 = objB.Y;
int X1 = x1 + objA.Width;
int X2 = x2 + objB.Width;
int Y1 = y1 + objA.Height;
int Y2 = y2 + objB.Height;

if (Y1 < y2)
return false;
if (y1 > Y2)
return false;
if (X1 < x2)
return false;
if (x1 > X2)
return false;

return true;


if you create a method where object A is your bird and object B is your wall, if it returns false, change the velocity of your bird.

You'll need to do a for loop for each wall, for example

for(int i = 0; i < WALLCOUNT; i++)