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: Collision detection between vector and rectangle (MATH)

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Collision detection between vector and rectangle (MATH)

    As of now, my code checks if either the vectors endpoints are between the rectangles y-values and if they are, it does the same for the x-values, however this is not correct, more andvanced math needs to be used because let's say the y-points of the vector is between the box's y-tops and bottom and x is between the x-boundries it the hitbox becomes a square instead of a line like the vector, I need help to get there. The problem is clear when viewing the graphics because I've made several small rectangles that go red if they collide according to my (faulty) code.

    Here's my code for the collision in my rectangle class:

    	if(swordEndY + swordY > y && swordY < y + size || swordY > y && swordY + swordEndY < y + size) { //Fixa denna
     
     
     
    			if((swordX /*+10*/ > x && swordX+ swordEndX /*+ 10*/ < x + size) || (swordX + swordEndX > x && swordX < x + size)){ // Denna funkar
     
    					touches=true;
     
    			}
     
    			else{touches=false;}
     
    		}

    (If touches is set to true the box turns red)


    ...And its results:

    Munin Vector1.jpg

    The pictures are not in order and the blue line is the vector and red squares are "hit".


    [UPDATE]

    Video for better understanding http://<a href="http://youtu.be/uNkI...NkIxzFvn1w</a>


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Collision detection between vector and rectangle (MATH)

    Ok, let me clarify something, in the pictures, only the green squares that the blue line touches should be red?

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Collision detection between vector and rectangle (MATH)

    Quote Originally Posted by aesguitar View Post
    Ok, let me clarify something, in the pictures, only the green squares that the blue line touches should be red?
    Yes, the green boxes are only supposed to be red when the blue vector is in direct contact.

    --- Update ---

    You know what? I'll post a video

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision detection between vector and rectangle (MATH)

    Your picture examples indicate you want to stick in a 2D java sort of world.

    The simplest way I see to consider your problem at all is to use a supplied

    Java2DSE supplied language approach.

    Consider java.awt.Rectangle.

    You can test to see if a Retangle may be created with zero height and width.

    This will do to have your point. You can use a point to be the intersection point (Vector tip, presumably)

    for your collision. If you want to test across the length of a Vector, consider java.awt.geom.Line2D

    -The trick to remember is that Rectangle inherits from Rectangle2D as well.

    And that there are methods

    Rectangle.intersection(rectangle2)

    and Rectangle2D.intersectLine(line)

    Which brings all the magic together.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Collision detection between vector and rectangle (MATH)

    Quote Originally Posted by Zachary1234 View Post
    Your picture examples indicate you want to stick in a 2D java sort of world.

    The simplest way I see to consider your problem at all is to use a supplied

    Java2DSE supplied language approach.

    Consider java.awt.Rectangle.

    You can test to see if a Retangle may be created with zero height and width.

    This will do to have your point. You can use a point to be the intersection point (Vector tip, presumably)

    for your collision. If you want to test across the length of a Vector, consider java.awt.geom.Line2D

    -The trick to remember is that Rectangle inherits from Rectangle2D as well.

    And that there are methods

    Rectangle.intersection(rectangle2)

    and Rectangle2D.intersectLine(line)

    Which brings all the magic together.
    Ok, is this way of cheating (not doing it from scratch) very costful?

    --- Update ---

    For some reason the first post seems vanished. However the problem is that my code acts exactly like I thought it would, and would like help to build collision detection between a rectangle and a vector from the ground up. In the video below you can clearly see that many boxes that turn red doesn't collide with the blue vector, and they shouldn't.

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Collision detection between vector and rectangle (MATH)


  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Collision detection between vector and rectangle (MATH)

    well, one way to try to fix your collision problem is build a point class of some sort, and build an array or ArrayList of the points where the squares are. You could also keep another array/ArrayList of the points that your sword vector is on.

    So if one of your squares is at (1,3) and you sword is from (0,0) to (5,1), you could use the distance formula ( sqrt( (x2-x1)^2 + (y2 - y1)^2)) and some trig to find if the touch.

    The Distance formula will get you the length of your sword. Trigonometrically, here is the formula you can use: tan^-1((y2-y1)/(x2-x1)). That will give you the degree of the angle relative to the start base of the sword. Next, use the same process on the detection squares. If the distance is smaller and the angle is negligible in difference or equal for the length of the sword ( the longer the sword, the smaller the angle can be) then it is touching the square, otherwise false.

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Collision detection between vector and rectangle (MATH)

    I think I'll go with the easy way and check for squares at each end, the only thing that would look weird then would be if the sword is angled at around 45° and gets thrown on to a corner of a rectangle, however for me it is a small price to pay, besides when you get further into the game it (the sword) will rotate so fast you won't even notice.

    Please feel free to keep track of my game, it is due before Christmas so if I can figure out how to turn it into an exe or jar by than I can send it to you if you'd like.

  9. #9
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Collision detection between vector and rectangle (MATH)

    Well, what you can do is, use the distance formula and inverse tangent like so. another thing, that box your current test uses is great because those are the only points you have to check.

     
    public boolean touches(int x, int y)
    {
         double sword = Math.sqrt((swordEndX-swordBaseX)*(swordEndX-swordBaseX) + (swordEndY-swordBaseY)*(swordEndY-swordBaseY)), point = Math.sqrt((x - swordBaseX)*(x - swordBaseX) + (y - swordBaseY)*(y - swordBaseY));  //length of the sword and length of the line created by the point based off of the base of the sword
     
         double swordAngle = Math.toDegrees(Math.atan((swordEndY-swordBaseY)/(swordEndX-swordBaseX))), pointAngle = Math.toDgrees(Math.atan((x-swordBaseX)/(y - swordBaseY))); //angles of the point and end of sword based on the base of the sword
     
         if(sword>=point)  //if the sword is longer than the point, then the point lies within the affectable range
         {
               if(Math.abs(swordAngle-pointAngle)<=5) //or some other value based on trial and error
               {
                      return true;
               }
         }
     
         return false;
     
    }

    you could iterate through the points that your current algorithm uses to save time. This should be very accurate if my math is correct.

  10. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Collision detection between vector and rectangle (MATH)

    Quote Originally Posted by aesguitar View Post
    Well, what you can do is, use the distance formula and inverse tangent like so. another thing, that box your current test uses is great because those are the only points you have to check.

     
    public boolean touches(int x, int y)
    {
         double sword = Math.sqrt((swordEndX-swordBaseX)*(swordEndX-swordBaseX) + (swordEndY-swordBaseY)*(swordEndY-swordBaseY)), point = Math.sqrt((x - swordBaseX)*(x - swordBaseX) + (y - swordBaseY)*(y - swordBaseY));  //length of the sword and length of the line created by the point based off of the base of the sword
     
         double swordAngle = Math.toDegrees(Math.atan((swordEndY-swordBaseY)/(swordEndX-swordBaseX))), pointAngle = Math.toDgrees(Math.atan((x-swordBaseX)/(y - swordBaseY))); //angles of the point and end of sword based on the base of the sword
     
         if(sword>=point)  //if the sword is longer than the point, then the point lies within the affectable range
         {
               if(Math.abs(swordAngle-pointAngle)<=5) //or some other value based on trial and error
               {
                      return true;
               }
         }
     
         return false;
     
    }

    you could iterate through the points that your current algorithm uses to save time. This should be very accurate if my math is correct.
    I could use this (though I already know the angle so that tan^-1 business isn't neccessary), but I took the easy way out and made a list of squares with collision detection of their own.

    Demonstration:


  11. #11
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Collision detection between vector and rectangle (MATH)

    Well, you could try either way and see which one is more efficient. (count the number of frames with your method and my method) The more efficient the algorithm, the better your performance will be, so if there's a lot of things happening that need to be calculated, you'll be saving power from here and there. Frankly, whatever works, but, I prefer efficiency over easy.

  12. #12
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Collision detection between vector and rectangle (MATH)

    Quote Originally Posted by aesguitar View Post
    Well, you could try either way and see which one is more efficient. (count the number of frames with your method and my method) The more efficient the algorithm, the better your performance will be, so if there's a lot of things happening that need to be calculated, you'll be saving power from here and there. Frankly, whatever works, but, I prefer efficiency over easy.
    I take some bad decisions so that I can complete it in time.

Similar Threads

  1. Trouble with Collision Detection
    By Gravity Games in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 6th, 2012, 02:00 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