Hi, I'm having a little trouble with collision detection for walls in my game project. I'm trying to make it so that when a wall is detected, you can't keep moving in that direction, but for some reason, the character object will keep moving anyway.
package com.gravitygamesinteractive.kylethecaiman; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Character { private int x,y; public static int fallspeed = 2; public static int moveSpeed = 1; public static BufferedImage kyle; public static BufferedImage cali; public static BufferedImage steve; public static boolean isTouchingFloor; public static boolean canMove; public Character(int x, int y){ this.x=x; this.y=y; try { kyle = ImageIO.read(new File("res/Kyle.png")); } catch (IOException e) { } } public void tick(){ if(!isCollidingWithFloor(new Point(x,(y+44+fallspeed)),new Point((x+16),(y+44+fallspeed)))){ y+=fallspeed; Component.sy+=(fallspeed); }else{ } if(Component.isMoving){ if(Component.dir==moveSpeed){ canMove = isCollidingWithWall(new Point(x+16,y), new Point(x+16,y+40)); }else if(Component.dir==-moveSpeed){ canMove = isCollidingWithWall(new Point(x-1,y+2), new Point(x-1,y+40)); } if(!canMove){ x+=Component.dir; Component.sx+=Component.dir; }else{ Component.isMoving=false; } } } public boolean isCollidingWithFloor(Point pt1, Point pt2){ for(int f=0;f<Level.tile.size();f++){ if(Level.tile.get(f).contains(pt1) || Level.tile.get(f).contains(pt2)){ isTouchingFloor=true; return true; } } isTouchingFloor=false; return false; } public boolean isCollidingWithWall(Point pt1, Point pt2){ for(int g=0;g<Level.tile.size();g++){ if(Level.tile.get(g).contains(pt1) || Level.tile.get(g).contains(pt2)){ return true; } } return false; } public void render(Graphics g){ g.setColor(Color.red); g.drawRect(Component.sx+x, Component.sy+y, 16, 44); g.drawRect(Component.sx+x-1, Component.sy+y+40, 16, 44); g.drawImage(kyle,Component.sx+x-8,Component.sy+y,null); } }