Page 1 of 2 12 LastLast
Results 1 to 20 of 40
Like Tree1Likes

Thread: Collision Detection?

  1. #1
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Collision Detection?

    Ohai everybody! Just started up a small new project,
    creating a small game for school.

    I'm using the draw(); method to draw stuff on the screen,
    I managed to get the character to move around,
    but when I add walls (In another color),
    I just can't figure out how to add a collision detection that works.

    Is it possible to make an if statement about what color it is?
    I just want it as simple as possible,
    going to add some rooms, a store & monsters later on.

    Thanks,
    Alexander Hållenius
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default

    Yes you can detect what has been drawn on an image by looking at its pixels.
    It might be easier for your app to define some kind of shape objects for the boundaries and use the contains method to detect collisions.

  3. #3
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    Contains? Explain more please. (-:
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default

    Contains? Explain more please.
    Read the API doc for the Shape class.

  5. #5
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    Okay, I will! (-:
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  6. #6
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    I have really no idea. Contains? Can't find it in the API. :-/
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default

    Here is how you find a method if you don't know what class it is in.
    Go to the API doc page. At the top there is a light blue strip with the word Index to the right.
    Click on Index to go to the index page.
    Then click on the first letter of the method in the row of letters. That would be C for contains.
    On the page with all the methods starting with C, Find the method you want to read about.

  8. #8
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    Okay, I found this out,

    public boolean contains(Point p)
    Checks whether the specified point is within this object's bounds, where the point's x and y coordinates are defined to be relative to the coordinate system of the object.
    Specified by:
    contains in interface AccessibleComponent
    Parameters:
    p - the Point relative to the coordinate system of the object
    Returns:
    true if object contains Point; otherwise false
    See Also:
    AccessibleComponent.getBounds()
    ...

    How can I use this for the collision detecting?
    I'm not using Java 2D, does it matters?
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How can I use this for the collision detecting?
    I'm not using Java 2D, does it matters?
    Maybe you want to tell us what you are in fact using if not Java2D.

  10. #10
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    Hmm, I might actually be using Java 2D? (I'm very new to graphics)

    What I mean is that I have'nt been typing anything more than "super.paintComponents(g)" in my paint method.
    I also have some stuff just drawn by g.fillRect();

    So, then to the collision detection again I suppose if you don't need me to do something else before doing that.
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Collision Detection?

    If you define a Rectangle where there is a rectangular boundary that you want to detect a collision with and use a Point on the object that is moving then use the contains method to see if the point is in the rectangle.
    Or if you have a rectangular shape that is moving you could use the intersects method to see if the moving rectangle's position intersects with the rectangular boundary.
    The are several methods for shapes that could be useful for collision detection.

  12. #12
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Re: Collision Detection?

    How exactly do I define the boundaries? I mean. the rectangle does'nt have a name :-/
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Collision Detection?

    How exactly do I define the boundaries
    Define a rectangle to align with one of the boundaries.
    Where are the boundaries that you want to detect?
    Say they are at upper left of 10, 10 to lower right of 50, 50
    Then define a rectangle:
    Rectangle bndry1 = new Rectangle(10, 10, 40, 40);
    Then if you have a point:
    Point pt = new Point(20, 20);

    if (bndry1.contains(pt)) { //Test if the pt within the rectangle
    ....
    }
    Alerhau likes this.

  14. #14
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Re: Collision Detection?

    Thankyou very much Norm! You made me solve it! :-)
    I still have a lot to learn about it, but now I can make my first little adventure game for school!

    Thanks,
    Ah
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  15. #15
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Re: Collision Detection?

    Hmm, seems to work fine.
    But there is one more thing that I have to ask.

    How do I make my character not getting stuck in the wall each time he collides with it?
    Any ideas?

    Thanks,
    Ah
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Collision Detection?

    Change its x,y and direction of motion if it is supposed to bounce

  17. #17
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Re: Collision Detection?

    Hmm, can you please explain?
    I don't really know what to change my X & Y to.
    If I do that I suppose that my character will start to jump around and do double steps sometimes (Jump 4 pixels forward).
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Collision Detection?

    getting stuck in the wall
    I assume means the object's x,y values overlap with the wall's x,y values.

    I don't really know what to change my X & Y to.
    That would depend on the visual effect you want. Try different changes to see what looks right.

  19. #19
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Re: Collision Detection?

    Gaah, I can't get it :-/

    Here is the code, tried to make some comments so that you can understand it better.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class ac extends JPanel {
    	public int xp = 10, yp = 110, speed = 1;
    	
    	public ac(ab l) {
    		
    		l.addKeyListener(new KeyAdapter() {
    			public void keyPressed(KeyEvent ke) {
    				Rectangle[] rec = {new Rectangle(0, 100, 100, 100)}; //Rectangle;
    				Point pt = new Point(xp, yp); //Current Character Position;
    				
    				if(ke.getKeyCode() == KeyEvent.VK_W) { //Upward
    					
    					if(rec[0].contains(pt)) {
    						yp -= sp;
    						repaint();
    					} else {
    						
    					}
    					
    				}
    				if(ke.getKeyCode() == KeyEvent.VK_S) { //Downward
    					
    					if(rec[0].contains(pt)) {
    						yp += speed;
    						repaint();
    					} else {
    						
    					}
    					
    				}
    				if(ke.getKeyCode() == KeyEvent.VK_A) { //Left
    					if(rec[0].contains(pt)) {
    						xp -= speed;
    						repaint();
    					} else {
    						
    					}
    				}
    				if(ke.getKeyCode() == KeyEvent.VK_D) { //Right
    					if(rec[0].contains(pt)) {
    						xp += speed;
    						repaint();
    					} else {
    						
    					}
    				}
    				
    			}
    		});
    		
    	}
    	
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		
    		//Level;
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 100, 100, 100);
    		//Level;
    		
    		g.setColor(Color.WHITE);
    		g.fillRect(xp, yp, 10, 10);
    		
    	}
    }
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  20. #20
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Re: Collision Detection?

    The first thing that says "sp", in the upward movement are supposed to be "speed".
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

Page 1 of 2 12 LastLast

Similar Threads

  1. Collision Detection, a little bit buggy.
    By iamsothecoolest in forum New To Java
    Replies: 10
    Last Post: 08-04-2011, 12:03 AM
  2. Java3D collision detection
    By abbeywell in forum Java Gaming
    Replies: 5
    Last Post: 04-27-2011, 02:33 PM
  3. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 12:38 AM
  4. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 06:13 PM
  5. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 08:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •