Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Trouble with Collision Detection

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Trouble with Collision Detection

    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);
     
    	}
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Trouble with Collision Detection

    shouldnt if(!canMove) be if(canMove)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble with Collision Detection

    Please use [code=java] for your java code. Old eyes like mine do well with bright colors. Plus it just makes the forum pretty.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with Collision Detection

    canMove = isCollidingWithWall(
    When does isCollidingWithWall() return true?
    The sense of those names doesn't make sense to me?
    It seems to say it can move if it is colliding with the wall.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Trouble with Collision Detection

    Nevermind, skim reading is bad for ones health.
    Last edited by newbie; September 5th, 2012 at 04:51 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with Collision Detection

    if isColliding is true, then can move will be false
    I don't read it that way. The code: canMove = isCollidingWithWall(

    And $0.02 more makes it $0.04. Almost enough to bend over for to pick up.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble with Collision Detection

    Nevermind, I figured it out not too long ago. Apparantly the problem was that I forgot to reset a variable back to 0, so the object kept moving even when a wall was detected.

    Edit: But I am, however, having trouble with points a bit. You see, I have the speed at which the y variable changes higher than one per frame. This sometimes leads to the character object getting stuck a little bit in the tiles.

    Character object:
    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 = 3;
    	public static int moveSpeed = 4;
    	public static int jumpSpeed = 3;
    	public static int jumpHeight=35, jumpCount=0;
     
    	public static BufferedImage kyle;
    	public static BufferedImage kali;
    	public static BufferedImage steve;
    	public static boolean isTouchingFloor;
    	public static boolean canMove;
    	public static boolean isJumping=false;
     
    	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(!isJumping && !isCollidingWithFloor(new Point(x,(y+44)),new Point((x+16),(y+44)))){
    		y+=fallspeed;
    		Component.sy+=(fallspeed);
    		}else if(Component.isJumping){
    				isJumping=true;
    			}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{
    				x+=0;
    				Component.sx+=0;
    				Component.dir=0;
    				Component.isMoving=false;
    			}
    			if(isJumping){
    				if(jumpCount>=jumpHeight){
    					isJumping=false;
    					Component.isJumping=false;
    					jumpCount=0;
    				}else{
    					y-=jumpSpeed;
    					Component.sy-=jumpSpeed;
     
    					jumpCount+=1;
    				}
    			}
    		}
    	}
     
    	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(x-Component.sx, y-Component.sy, 16, 44);
    		g.drawImage(kyle,x-8-Component.sx,y-Component.sy,null);
     
    	}
    }

    Tile object:
    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.JFrame;
    import javax.imageio.*;
    import java.io.*;
     
    public class Tile extends Rectangle{
    	private static final long serialVersionUID=1L;
     
    	public static int[] g1top={0,0};
    	public static BufferedImage tileset1ground;
    	public Tile(int x, int y){
    		this.x=x;
    		this.y=y;
    		this.width=16;
    		this.height=16;
    		try {
    		    tileset1ground = ImageIO.read(new File("res/tileset1.png"));
    		} catch (IOException e) {
    		}
    	}
     
    public void tick(){
     
    }
     
    	public void render(Graphics g){
    		g.setColor(Color.red);
    		g.drawImage(tileset1ground,x-Component.sx,y-Component.sy,null);
    	}
    }
    Last edited by Gravity Games; September 5th, 2012 at 11:32 PM.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble with Collision Detection

    So for example, say your y value should be no more than 54 to keep an object standing on top of it rather than slightly inside it. When your y updates by some number greater than 1,.....

    if (y > 54) { y = 54; }
    stopUpdatingY();//this is to refer to what you already do

  9. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble with Collision Detection

    That worked for the most part, but for some reason about one out of eight times the character object will immediately snap down to the ground without scrolling the stage with it, resulting in the level beginning to leave the screen.

    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;
    	private int tiley;
    	public static int fallspeed = 3;
    	public static int moveSpeed = 4;
    	public static int jumpSpeed = 3;
    	public static int jumpHeight=35, jumpCount=0;
     
    	public static BufferedImage kyle;
    	public static BufferedImage kali;
    	public static BufferedImage steve;
    	public static boolean isTouchingFloor;
    	public static boolean canMove;
    	public static boolean isJumping=false;
    	public static boolean allowFall=true;
     
    	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(!isJumping && allowFall && !isCollidingWithFloor(new Point(x,(y+44)),new Point((x+16),(y+44)))){
    		y+=fallspeed;
    		Component.sy+=(fallspeed);
    		}else if(Component.isJumping){
    				isJumping=true;
    			}else{
    				y=tiley-45;
    				allowFall=false;
    			}
     
    		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{
    				x+=0;
    				Component.sx+=0;
    				Component.dir=0;
    				Component.isMoving=false;
    			}
    			if(isJumping){
    				if(jumpCount>=jumpHeight){
    					isJumping=false;
    					Component.isJumping=false;
    					jumpCount=0;
    					allowFall=true;
    				}else{
    					y-=jumpSpeed;
    					Component.sy-=jumpSpeed;
     
    					jumpCount+=1;
    				}
    			}
    		}
    	}
     
    	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)){
    				tiley=Level.tile.get(f).y;
    				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(x-Component.sx, y-Component.sy, 16, 44);
    		g.drawImage(kyle,x-8-Component.sx,y-Component.sy,null);
     
    	}
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with Collision Detection

    the character object will immediately snap down to the ground without scrolling the stage with it, resulting in the level beginning to leave the screen.
    Can you describe what happens in terms of program variables? What variables are set to what values and cause the visual effect you are describing?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble with Collision Detection

    Well, according to the printlines, the boolean is false when touching the ground, true when falling, and false when jumping. Also, for some reason moving the object to an area where there is no tile objects won't cause it to fall unless the button to jump is pressed.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with Collision Detection

    I Have no idea if those are the correct values for the boolean variables for the state of the program.

    You need to analyze the values of the program's variables when it is not doing what you want to do and see what the values are when the execution goes wrong and then check the code to see why it gave those values to the variables instead of giving them correct values. I other words, you need to debug the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Collision Detection Between Two Squares
    By thegreatzo in forum Loops & Control Statements
    Replies: 7
    Last Post: August 22nd, 2012, 09:13 AM
  2. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 02:12 PM
  3. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  4. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM