1 Attachment(s)

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:

Code :

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:

Attachment 1536

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>

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?

Re: Collision detection between vector and rectangle (MATH)

Quote:

Originally Posted by

**aesguitar**
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

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.

Re: Collision detection between vector and rectangle (MATH)

Quote:

Originally Posted by

**Zachary1234**
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. http://www.youtube.com/watch?v=uNkIxzFvn1w

Re: Collision detection between vector and rectangle (MATH)

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.

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.

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.

Code java:

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.

Re: Collision detection between vector and rectangle (MATH)

Quote:

Originally Posted by

**aesguitar**
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.

Code java:

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:

http://www.youtube.com/watch?v=lyn2AfNDeP0

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.

Re: Collision detection between vector and rectangle (MATH)

Quote:

Originally Posted by

**aesguitar**
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.