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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Collision Detection between Sprites and Images?

  1. #1
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Collision Detection between Sprites and Images?

    Hello everyone

    I am creating a small game and have come to the point where I need to use collision detection. The problem is, I do not exactly know how to create basic collision with rectangles. I now it requires you to import java.awt.Rectangle but thats about it as far as what has worked for me.

    package outrun;
     
    import java.awt.Image;
    import javax.swing.ImageIcon;
     
    public class Cobblestone {
     
    	private String cobblestone = "/resources/cobblestone_outrun.png";
     
    private Image image;
     
     
    	public Cobblestone() {
    		ImageIcon i = new ImageIcon(this.getClass().getResource(cobblestone));
    		image = i.getImage();
    		}
     
    		 public Image getImage() {
    		    	return image;
    		    }
     
    }
    This is my cobblestone class. The image is 20x20 pixels, I need collision for this.

    package outrun;
     
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.Rectangle;
     
    import javax.swing.ImageIcon;
     
    public class Person {
     
    	private String person = "/resources/person_outrun.png";
     
    	private int dx;
    	private int dy;
    	private int x;
    	private int y;
    	private Image image;
     
    	public Person() {
    		ImageIcon i = new ImageIcon(this.getClass().getResource(person));
    		image = i.getImage();
     
    		x = 31;
    	    y = 175;	
    	}
     
    	public void move() {
    		x += dx;
    		y += dy;
    	}
     
        public int getX() {
        	return x;
        }
     
        public int getY() {
        	return y;
     
        }
     
        public Image getImage() {
        	return image;
        }
     
        public void KeyPressed(KeyEvent e) {
     
            int key = e.getKeyCode();
     
        	if (key == KeyEvent.VK_RIGHT) {
        		dx = 1;
        	}
     
            if (key == KeyEvent.VK_LEFT) {
            	dx = -1;
            }
     
            if (key == KeyEvent.VK_UP) {
            	dy = -1;
            }
     
            if (key == KeyEvent.VK_DOWN) {
            	dy = 1;
            }
        }
     
        public void KeyReleased(KeyEvent e) {
        	int key = e.getKeyCode();
     
        	if (key == KeyEvent.VK_RIGHT) {
        		dx = 0;
        	}
     
            if (key == KeyEvent.VK_LEFT) { 
            	dx = 0;
            }
     
            if (key == KeyEvent.VK_UP) {
            	dy = 0;
            }
     
            if (key == KeyEvent.VK_DOWN) {
            	dy = 0;
            }
        }
    }
    This is my person class, I also need collision for this.

    Do I determine the collision method in my board.class? What I wish to happen is if the person intersects the cobblestone, the person stops (dx = 0 and dy = 0). How would I go about doing this? Thanks! Any help would be greatly appreciated!


  2. #2
    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: Collision Detection between Sprites and Images?

    The Rectangle class has methods for detecting intersection that might be useful.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    Oh yes, but how would I go about creating a rectangle for the person, it moves? The parameters consist of x and y values. How would I be able to create one if the person is moving, hence constantly changing x and y values? I am confused as to how to use the rectangle class.

  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: Collision Detection between Sprites and Images?

    Is the person a point without width and height? The person's rectangle would be its x,y & width and height.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    The person has width and height, it is an image I created. Would I need to use getWidth() and getHeight() and assign them to two new variables?

  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: Collision Detection between Sprites and Images?

    Yes, you would need the width and height to define the rectangle that surrounds the person.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    Ok I will work on that, Eclipse somehow does not want to open my files stating that the files do not exist. I will resume work once I get that sorted from someone.

  8. #8
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    Ok, so I have made rectangle classes for both cobblestone.java and person.java (the images I wish to intersect.) Now I am actually determining the collision in my board.java:

    public void checkCollisions() {
     
        	Rectangle r1 = person.getBounds();
        	Rectangle r2 = cobblestone.getBounds();
     
       	 if (r1.intersects(r2)) {
       	 }
        }

    The problem is I do not know what to put within my if statement. dx and dy control the 'speed' or translation of the person in my person class through coordinates. In other words, they control the movement of my person. How do I implement these variables in my board.java?

  9. #9
    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: Collision Detection between Sprites and Images?

    what to put within my if statement
    It all depends on what you want to happen when there is a collision. Define that and then you can work on the code to implement it.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    I know what I want to happen, I just don't know how to show it. If they both collide, I want the variables dx and dy to be equal to zero. The trouble is those two variables are in the person class, not the board class, where I want them to be uses also. How would I be able to do that?

  11. #11
    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: Collision Detection between Sprites and Images?

    To change variables in a class, add a method to the class that you can call to change them.

    Not sure what you mean by "If they both collide". It takes two objects to have a collision.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    What I meant by was if they both collide with each other, sorry, I wasn't to clear! I do not want to change the variables. I want to use the variable dx and dy from board.java in person.java though I do not know how to do that.

  13. #13
    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: Collision Detection between Sprites and Images?

    The position and speed of an object should be contained in the object.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    package outrun;
     
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.Rectangle;
     
    import javax.swing.ImageIcon;
     
    public class Person {
     
    	private String person = "/resources/person_outrun.png";
     
    	private int dx;
    	private int dy;
    	private int x;
    	private int y;
    	private int width;
    	private int height;
    	private Image image;
     
    	public Person() {
     
    		ImageIcon i = new ImageIcon(this.getClass().getResource(person));
    		image = i.getImage();
    		width = image.getWidth(null);
    		height = image.getHeight(null);
     
    		x = 31;
    	    y = 175;	
    	}
     
    	public void move() {
    		x += dx;
    		y += dy;
    	}
     
        public int getX() {
        	return x;
        }
     
        public int getY() {
        	return y;
     
        }
     
        public Image getImage() {
        	return image;
        }
     
        public Rectangle getBounds() {
            return new Rectangle(x, y, width, height);
        }
     
        public void KeyPressed(KeyEvent e) {
     
            int key = e.getKeyCode();
     
        	if (key == KeyEvent.VK_RIGHT) {
        		dx = 1;
        	}
     
            if (key == KeyEvent.VK_LEFT) {
            	dx = -1;
            }
     
            if (key == KeyEvent.VK_UP) {
            	dy = -1;
            }
     
            if (key == KeyEvent.VK_DOWN) {
            	dy = 1;
            }
        }
     
        public void KeyReleased(KeyEvent e) {
        	int key = e.getKeyCode();
     
        	if (key == KeyEvent.VK_RIGHT) {
        		dx = 0;
        	}
     
            if (key == KeyEvent.VK_LEFT) { 
            	dx = 0;
            }
     
            if (key == KeyEvent.VK_UP) {
            	dy = 0;
            }
     
            if (key == KeyEvent.VK_DOWN) {
            	dy = 0;
            }
        }
    }

    This is Person.java. It contains the variables dx and dy that I need. However, I want to use the same dx and dy for board.class below:

    package outrun;
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.Rectangle;
     
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
    public class Board extends JPanel implements ActionListener {
     
    	private Timer timer;
     
    	private Person person;
    	private Cobblestone cobblestone;
     
    	public Board() {
     
    		addKeyListener(new TAdapter());
    		setFocusable(true);
    		setBackground(Color.BLACK);
    		setDoubleBuffered(true);
     
    		setSize(10, 10);
     
    		person = new Person();
    		cobblestone = new Cobblestone();
    		timer = new Timer(5, this);
            timer.start();
    	}
     
    	public void paint(Graphics g) {
        	super.paint(g);
     
        	Graphics2D m = (Graphics2D)g;
        	m.drawImage(person.getImage(), person.getX(), person.getY(), this);
     
            drawMaze((Graphics2D) g);
     
        }
        private void drawMaze(Graphics2D g){
        	Graphics2D g2 = (Graphics2D)g;
     
                int width = 27; 
                int height = 17;
                int xOffset = 21; 
                int yOffset = 21; 
                int currentX = 10; 
                int currentY = 10; 
                     for(int j = 0; j < height; j++) {
                    	 if(j!=8)
                           g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                           currentY += yOffset;
                           }
                     for(int k = 0; k < width; k++) {
                    	 if(k!=13)
                    	   g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                    	   currentX += xOffset;
                     }
                     currentY = 10;
                	 currentX = 556;
     
                	 for(int l = 0; l < height; l++) {
                		 if(l!=8)
                    	 g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                  	   currentY += yOffset;
                     }
                     currentY = 10;
                     currentX = 10;
     
                     for(int m = 0; m < width; m++) {
                    	 if(m!=13)
                    	 g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
                    	 currentX += xOffset;
     
                         }
     
                     g.setColor(Color.green);
                     g.drawString(" ENV 1 LVL 1", 480, 50);
     
    }
     
        public void checkCollisions() {
     
        	Rectangle r1 = person.getBounds();
        	Rectangle r2 = cobblestone.getBounds();
     
       	 if (r1.intersects(r2)) {
       	 }
        }
     
     
      public void actionPerformed(ActionEvent e) {
        	person.move();
        	repaint();
        }
     
        private class TAdapter extends KeyAdapter {
     
        	public void keyReleased(KeyEvent e) {
        		person.KeyReleased(e);
        	}
     
        public void keyPressed(KeyEvent e) {
        	person.KeyPressed(e);
        }
     
        }
     
    }

    How do I do this?

  15. #15
    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: Collision Detection between Sprites and Images?

    The Person class has the dx and dy variables. If the Board class has a reference to a Person object, it can call methods in the Person class to access those variables.

    You have not explained why you need access to those variables?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    I need to access them as I want to set dx and dy to zero if both the person and cobblestone intersect each other. How do I make that reference? I have tried person.dx though that does not work.

  17. #17
    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: Collision Detection between Sprites and Images?

    Add a method to Person that you can call to do what you want.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    I tried, but then I come up with many errors stating that the variable could not be resolved. Where in person.java shall I create the method?

  19. #19
    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: Collision Detection between Sprites and Images?

    come up with many errors
    Please post the full text of any errors you want help with.
    Where in person.java shall I create the method?
    You could put the method(s) next to any of the other methods.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    public Person(int dx, int dy) {
     
    		ImageIcon i = new ImageIcon(this.getClass().getResource(person));
    		image = i.getImage();
    		width = image.getWidth(null);
    		height = image.getHeight(null);
     
    		x = 31;
    	    y = 175;
    	}

    I created a new method here. The errors are just errors stating 'dx/dy cannot be resolved into a variable'.

  21. #21
    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: Collision Detection between Sprites and Images?

    What you posted looks like a constructor not a method. A method has a return type in its definition.

    errors are just errors stating
    Please post the full text of the error message. Your description makes no sense.
    The posted code does not use the variables dx and dy. It defines new variables as parameters to the method.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    Oh apologies, I posted the wrong code, the errors were already resolved, the interface never refreshed to reflect that yet. Here is my code using the methods:

    public int getdx() {
        	return dx;
        }
     
        public int getdy() {
        	return dy;
        }

    Now I need to use it for determining collision:

    if (r1.intersects(r2)) {
       	 }
         person.getdx()..................
        }

    I want dx to equal 0, how do I express that? I will do the same for dy in due course.

  23. #23
    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: Collision Detection between Sprites and Images?

    I want dx to equal 0, how do I express that?
    Somewhere that dx is in scope:

    dx = 0;
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    I know how to express 'dx = 0;', the problem is dx is not recognized as a variable in board.java. Do I do this? This is in board.class if that helps.
    if (r1.intersects(r2)) {
       	 }
         person.getdx()= 0; //This is the problem.
        }

  25. #25
    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: Collision Detection between Sprites and Images?

    If dx is in another class, then you need a reference to that class and a method in that class to call that will do what you want.

    aRefToClassWith_dx.methodToClear_dx(); // call a method to clear dx (set to 0)
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 04:11 PM
  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