Results 1 to 3 of 3
  1. #1
    damienrenner is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Exclamation Basic - Confusion with collision detection between squares

    Heyy, I have a game where the user controls one square but I want to collect collision with another square. I know the basic layout for doing this, However, I always get my more than and less than and + and - mixed up thus i was wondering if anyone could correct them for me ?



    Java Code:
    	if (character.y > collectTwo.y && character.y + characterWidth < collectTwo.x + 10)
    					if (character.x > collectTwo.x && character.x + characterWidth < collectTwo.x-10)
    This size of the collectTwo item is 10 x 10.



    Thank you so much!!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Basic - Confusion with collision detection between squares

    I always get my more than and less than and + and - mixed up
    Me too! Especially in multiple dimensions and with the computer's coordinate system being upside down. I find it easiest to think logically.

    Two 1-d line segments will interest iff the max of their left hand ends < the min of their right hand ends. (*) For rectangles this condition must hold in both dimensions simultaneously if they intersect, and for "upright" rectangles the joint condition is sufficient.

    (*) But I've slipped into geometry with "left" and "right". This is reasonable in your case, I think. But what I really mean is given segments a_1->a_2 and b_1->b_2 that max(min(a_1,a_2),min(b_1,b_2)) < min(max(a_1,a_2),max(b_1,b_2))
    Last edited by pbrockway2; 04-25-2012 at 02:08 AM.

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Basic - Confusion with collision detection between squares

    How about:
    Java Code:
    if ( Math.abs(a.x - b.x) < (a.width()  + b.width())/2 &&
         Math.abs(a.y - b.y) < (a.height() + b.height())/2 )
    I made it completly up so I did not test it, so be careful. ;)

Similar Threads

  1. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 04:29 PM
  2. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  3. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  4. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 01:38 AM
  5. Magic Squares, input confusion
    By bengiles89 in forum New To Java
    Replies: 6
    Last Post: 04-29-2010, 02:13 AM

Tags for this Thread

Posting Permissions

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