I am learning Java in college through processing and have been trying to make it so that when the 2 sprites collide on one side or the other they can no longer move in that direction
The collision checks seem to work but i dont know how i would go about to making it so using this i can prevent the movement of the sprites
(also note that these checks are being done in the draw function)
//check if P1 is to the left of P2
if(P1posX == ( P2posX - leftSide ) && P1posY == P2posY){
collisionTestCheckSide1 = true;
print("collision detected side 1");
}
if(P1posX != ( P2posX - leftSide ) && P1posY == P2posY){
collisionTestCheckSide1 = false;
}
//check if P1 is to the right of P2
if(P1posX == ( P2posX + rightSide ) && P1posY == P2posY){
collisionTestCheckSide2 = true;
print("collision detected side 2");
}
if(P1posX != ( P2posX + rightSide ) && P1posY == P2posY){
collisionTestCheckSide2 = false;
}
Heres the movement part of the script
void keyPressed() {
if (keyCode == LEFT && P1posX > 10) {
P1posX = P1posX - 5;
}
if (keyCode == 68 && P2posX < 490) {
P2posX = P2posX + 5;
}
if (keyCode == UP && P1posY > 10) {
P1posY = P1posY - 5;
}
if (keyCode == DOWN && P1posY < 490 ) {
P1posY = P1posY + 5;
}
if (keyCode == RIGHT && P1posX < 490) {
P1posX = P1posX + 5;
}
if (keyCode == 65 && P2posX > 10) {
P2posX = P2posX - 5;
}
if (keyCode == 87 && P2posY > 10) {
P2posY = P2posY - 5;
}
if (keyCode == 83 && P2posY < 490) {
P2posY = P2posY + 5;
}
And heres the whole all my code
float P1posX = 300;
float P1posY = 250;
float P2posX = 200;
float P2posY = 250;
float leftSide = 20;
float rightSide = 20;
PImage bg;
boolean collisionTestCheckSide1 = false;
boolean collisionTestCheckSide2 = false;
void setup() {
size(500, 500);
noStroke();
rectMode(CENTER);
bg = loadImage("canvas.png");
}
void draw() {
background(bg);
if (P1posX < 250){
fill(1000,90,0);
}
else{
fill(200, 1800, 900);
}
rect(P1posX, P1posY, 20, 20, 100);
fill(10, 550, 200);
if (P2posX < 250){
fill(10,10,10);
}
else{
fill(900, 900, 900);
}
rect(P2posX, P2posY, 20, 20, 100);
//check if P1 is to the left of P2
if(P1posX == ( P2posX - leftSide ) && P1posY == P2posY){
collisionTestCheckSide1 = true;
print("collision detected side 1");
}
if(P1posX != ( P2posX - leftSide ) && P1posY == P2posY){
collisionTestCheckSide1 = false;
}
//check if P1 is to the right of P2
if(P1posX == ( P2posX + rightSide ) && P1posY == P2posY){
collisionTestCheckSide2 = true;
print("collision detected side 2");
}
if(P1posX != ( P2posX + rightSide ) && P1posY == P2posY){
collisionTestCheckSide2 = false;
}
if(collisionTestCheckSide1 != true){
}
}
void keyPressed() {
if (keyCode == LEFT && P1posX > 10) {
P1posX = P1posX - 5;
}
if (keyCode == 68 && P2posX < 490) {
P2posX = P2posX + 5;
}
if (keyCode == UP && P1posY > 10) {
P1posY = P1posY - 5;
}
if (keyCode == DOWN && P1posY < 490 ) {
P1posY = P1posY + 5;
}
if (keyCode == RIGHT && P1posX < 490) {
P1posX = P1posX + 5;
}
if (keyCode == 65 && P2posX > 10) {
P2posX = P2posX - 5;
}
if (keyCode == 87 && P2posY > 10) {
P2posY = P2posY - 5;
}
if (keyCode == 83 && P2posY < 490) {
P2posY = P2posY + 5;
}
//WASD keyCode values 87 = w, 65 = a, 83 = s, 68 = d
}