Page 2 of 2 FirstFirst 12
Results 21 to 40 of 40
Like Tree1Likes

Thread: Collision Detection?

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    Your code does not compile and is missing a main method to be able to execute it.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    Can you explain what is supposed to happen when the WASD keys are pressed and when the white square goes to the side of the black square?

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

    Default Re: Collision Detection?

    I think I actually managed to solve it.
    Thanks for all help,
    Ah
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    Part of your problem was that the object is larger than the point at the top left corner.

    Some suggestions:

    Java Code:
    class ac extends JPanel {
    	public int speed = 1;
    
            Rectangle[] rec = {new Rectangle(100, 100, 100, 100)}; //Rectangle
    	Rectangle pt = new Rectangle(120, 130, 5,5 ); //Current Character Position;
    
            in the key listeners
    		pt.y -= speed;
    
    
    and in the paint method:
    
    		g.setColor(Color.BLACK);
    		g.fillRect(rec[0].x, rec[0].y, rec[0].width, rec[0].height);
    		//Level;
    	
    		g.setColor(Color.WHITE);
    		g.fillRect(pt.x, pt.y, pt.width, pt.height);
    Last edited by Norm; 09-04-2011 at 10:08 PM.

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

    Default Re: Collision Detection?

    Now I encountered even one more problem...
    My character is walking in the white areas, so he is walking into the cubes.
    But when he are supposed to change cube, I mean when I add,

    if(rec[0].contains(pt) *Here the thing I want to show starts* || rec[1].contains(pt))

    he stays in the rec[0] area. :-/ How can I solve this?
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

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

    Default Re: Collision Detection?

    Oh, did'nt see you wrote something. I'll take a look.
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

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

    Default Re: Collision Detection?

    But he is still getting stuck in the walls.
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    What stops the moving of the small white square when you are pressing one of the WASD keys?
    Look at your logic there.
    Do you need to move the small white square back inside of the black square.

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

    Default Re: Collision Detection?

    He stops when hitting the walls. It's like he is going one pixel too much.
    Is there any way to tell him that he can't walk that extra pixel?
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    He stops when hitting the walls
    What does your logic do when HE hits a wall? What happens to future presses of the key that moved HIM to hit the wall.

    Is there any way to tell him that he can't walk that extra pixel?
    Test BEFORE changing the x,y if HE will hit a wall and don't move HIM if the new x,y hits a wall.

    Have the "boundary" wall one or two pixels inside of the visual wall.

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

    Default Re: Collision Detection?

    Hmm, what do you mean with a boundary wall? :o
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    A rectangle inside of the visual rectangle.
    Visual at 10, 10, 100, 100
    boundary at 11, 11, 98, 98

    Not really necessary, your code should detect a wall and not go past or into it.

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

    Default Re: Collision Detection?

    Are you sure there are no other word then constaints(); i can use in order to see the walls?
    He is going one pixel too much every time.
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    He is going one pixel too much every time.
    Your code should detect that BEFORE the movement
    Say the wall is at x = 100
    and the object is at x = 99 and moving to the right (+x)
    If the distance to move is 1, 99+1 is into the wall => Don't make the move, stay at x=99

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    no other word then constaints()
    Do you mean methods?
    Read the API doc to see if any other methods would be of use.

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

    Default Re: Collision Detection?

    Your code should detect that BEFORE the movement
    Say the wall is at x = 100
    and the object is at x = 99 and moving to the right (+x)
    If the distance to move is 1, 99+1 is into the wall => Don't make the move, stay at x=99
    Hmm, the code is testing it before the movement :-o
    How do you want me to stay at 99? -.-
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    Basically: if the move will be into the wall do not make it. What order can you set, test and change the values that will prevent moving into the wall. Look at the order you are doing those things.

    Post the lines of code you are using to keep the object from moving into a wall.

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

    Default Re: Collision Detection?

    Can't I like test collision for only one side of the cube?
    For an example when it hits the left wall the left movement stops.
    That would make me to solve it.

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

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Collision Detection?

    Can't I like test collision for only one side of the cube?
    Why only one side? Is this just for testing some new code?

    Post the lines of code you are using to keep the object from moving into a wall.

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

    Default Re: Collision Detection?

    I think I actually managed to solve it now.
    Made 2 different rectangles, one for left/up & one for right/down.
    Works perfect!

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

Page 2 of 2 FirstFirst 12

Similar Threads

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