hey i was woking on a game program but the collison detection is proving triky and have been tring to get it to work for a few days but still not sure how to fix it it seems to be a false dection of the right and left hits but is not very consitent and only some time cuases issuss
ArrayList rectangles = new ArrayList();
float PlayerX;
float PlayerY;
float PlayerW;
float PlayerSX;
float PlayerSY;
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;
boolean jumping = false;
float GroundY;
float GroundX;
boolean jumpingg =false;
void setup(){
size(600,400);
background(255);
frameRate(60);
GroundY = height-20;
GroundX = 20;
PlayerW = 50;
PlayerX = 420;
PlayerY = 200;
PlayerSX = 0;
PlayerSY = 0;
addObj();
}
void draw(){
background(0);
PlayerMove();
Collision();
drawPlayer(PlayerX,PlayerY,PlayerW);
println(PlayerSY);
println(PlayerY);
println(jumping);
}
void Collision(){
for (int i = 0; i < rectangles.size(); i++) {
Rectangle rectangle = rectangles.get(i);
if (PlayerX + PlayerW + PlayerSX > rectangle.x &&
PlayerX + PlayerSX < rectangle.x + rectangle.rectWidth &&
PlayerY + PlayerW > rectangle.y &&
PlayerY < rectangle.y + rectangle.rectHeight) {
if(PlayerX <= rectangle.x){
PlayerX = rectangle.x - PlayerW;
}
if(PlayerX + PlayerW >= rectangle.x + rectangle.rectWidth){
PlayerX = rectangle.x + rectangle.rectWidth;
}
PlayerSX = 0;
}
if (
PlayerX + PlayerW > rectangle.x &&
PlayerX < rectangle.x + rectangle.rectWidth &&
PlayerY + PlayerW + PlayerSY > rectangle.y &&
PlayerY + PlayerSY < rectangle.y + rectangle.rectHeight) {
if(PlayerY <= rectangle.y){
jumping = false;
PlayerY = rectangle.y - PlayerW;
}
if(PlayerY >= rectangle.y + rectangle.rectHeight){
PlayerY = rectangle.y + rectangle.rectHeight;
}
PlayerSY = 0;
}
fill(255, 0, 0);
rect(rectangle.x, rectangle.y, rectangle.rectWidth, rectangle.rectHeight);
}
}
void PlayerMove(){
if (!rightPressed || !leftPressed) {
PlayerSX = 0;
}
if (upPressed) {
if (!jumping) {
PlayerSY = -15;
jumping = true;
}
}
if (downPressed) {
}
if (leftPressed) {
PlayerSX -= 1;
}
if (rightPressed) {
PlayerSX = 1;
}
if (jumping) {
PlayerSY ++;
}
for (int i = 0; i < rectangles.size(); i++) {
Rectangle rectangle = rectangles.get(i);
if(!jumping && !(
PlayerX + PlayerW > rectangle.x &&
PlayerX < rectangle.x + rectangle.rectWidth &&
PlayerY + PlayerW + 1 > rectangle.y &&
PlayerY + 1< rectangle.y + rectangle.rectHeight)) {
jumping = true;
}
}
PlayerY += PlayerSY;
PlayerX += PlayerSX;
}
void drawPlayer(float playerX, float playerY, float playerW){
fill(0,255,0);
rect(playerX, playerY, playerW, playerW);
}
void addObj(){
/* rectangles.add(new Rectangle(0, 0, width, 20));
rectangles.add(new Rectangle(0, GroundY, width, 20));
rectangles.add(new Rectangle(0, 0, 20, height));
rectangles.add(new Rectangle(width-20, 0, 20, height));
rectangles.add(new Rectangle(0, height*.33, width, 20));
rectangles.add(new Rectangle(width*0.33, 0, 20, height));
rectangles.add(new Rectangle(0, height*.66, width, 20));
rectangles.add(new Rectangle(width0.66, 0, 20, height));/
rectangles.add(new Rectangle(0, 0, width, 20));
rectangles.add(new Rectangle(0, GroundY, width, 20));
rectangles.add(new Rectangle(0, 0, 20, height));
rectangles.add(new Rectangle(width-20, 0, 20, height));
rectangles.add(new Rectangle(70, 300, 200, 20));
rectangles.add(new Rectangle(70, 260, 130, 20));
rectangles.add(new Rectangle(400, 300, 80, 20));
rectangles.add(new Rectangle(530, 100, 50, 20));
rectangles.add(new Rectangle(510, 120, 50, 20));
rectangles.add(new Rectangle(490, 140, 50, 20));
rectangles.add(new Rectangle(470, 160, 50, 20));
rectangles.add(new Rectangle(450, 180, 50, 20));
}
class Rectangle {
float x;
float y;
float rectWidth;
float rectHeight;
public Rectangle(float x, float y, float rectWidth, float rectHeight) {
this.x = x;
this.y = y;
this.rectWidth = rectWidth;
this.rectHeight = rectHeight;
}
}
void keyPressed() {
if (keyCode == UP) {
upPressed = true;
}
else if (keyCode == DOWN) {
downPressed = true;
}
else if (keyCode == LEFT) {
leftPressed = true;
}
else if (keyCode == RIGHT) {
rightPressed = true;
}
}
void keyReleased() {
if (keyCode == UP) {
upPressed = false;
}
else if (keyCode == DOWN) {
downPressed = false;
}
else if (keyCode == LEFT) {
leftPressed = false;
}
else if (keyCode == RIGHT) {
rightPressed = false;
}
}
any suggestions?