• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

#ActualAphton

Posted 23 January 2013 - 11:32 AM

Damn formatting =/

#6Aphton

Posted 23 January 2013 - 11:30 AM

If you are interested in the math behind it:

We have two lines f and g
f: A + i * AB
g: C + j * CD

First, we need to find out, whether they are parallel
If they are parallel, then their slope must be equal
parallel: AB.Y/AB.X == CD.Y/CD.Y
If they are parallel, return false

At this point, we know they are not paralle and two non-parallel lines in 2D intersect so this is valid:
A + i * AB = C + j * CD
We can rewrite that to:
I.  A.X + i * AB.X = C.X + j * CD.X
II. A.Y + i * AB.Y = C.Y + j * CD.Y

We determine j in I:
j = (A.X + i*AB.X - C.X) / CD.X
now we can substituate j in II
II. A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y

After some tranformation (you can skip this):
> A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y
> A.Y + i*AB.Y = C.Y + (A.X/CD.X + i*AB.X/CD.X - C.X/ CD.X) * CD.Y
> A.Y + i*AB.Y = C.Y + A.X*CD.Y/CD.X + i*AB.X*CD.Y/CD.X - C.X*CD.Y/ CD.X
> i*AB.Y - i*AB.X*CD.Y/CD.X = C.Y + A.X*CD.Y/CD.X - C.X*CD.Y/ CD.X - A.Y
> i*(AB.Y - AB.X*CD.Y/CD.X) = C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y
> i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)

We get:
i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)
We determined j previously. We simply insert the value of i into that formula and get thevalue for j. We need both to determine, whether they really intersect

As for the intersection, since we have i, we can calculate the point of intersection by inserting it into
f: A + i * AB

return InRange(i, 0, 1) && InRange(j, 0, 1)-> if both i and j are in that range, they intersect!


You have to extrude AB so that it intersects with the square around it 100%. Then you have to execute that above for each side of the square!

#5Aphton

Posted 23 January 2013 - 11:28 AM

<p>If you are interested in the maths:</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>
</p>
<pre>
We have two lines f and g
f: A + i * AB
g: C + j * CD

First, we need to find out, whether they are parallel
If they are parallel, then their slope must be equal
parallel: AB.Y/AB.X == CD.Y/CD.Y
If they are parallel, return false

At this point, we know they are not paralle and two non-parallel lines in 2D intersect so this is valid:
A + i * AB = C + j * CD
We can rewrite that to:
I.&nbsp;&nbsp;A.X + i * AB.X = C.X + j * CD.X
II. A.Y + i * AB.Y = C.Y + j * CD.Y

We determine j in I:
j = (A.X + i*AB.X - C.X) / CD.X
now we can substituate j in II
II. A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y

After some tranformation (you can skip this):
&gt; A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y
&gt; A.Y + i*AB.Y = C.Y + (A.X/CD.X + i*AB.X/CD.X - C.X/ CD.X) * CD.Y
&gt; A.Y + i*AB.Y = C.Y + A.X*CD.Y/CD.X + i*AB.X*CD.Y/CD.X - C.X*CD.Y/ CD.X
&gt; i*AB.Y - i*AB.X*CD.Y/CD.X = C.Y + A.X*CD.Y/CD.X - C.X*CD.Y/ CD.X - A.Y
&gt; i*(AB.Y - AB.X*CD.Y/CD.X) = C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y
&gt; i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)

We get:
i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)
We determined j previously. We simply insert the value of i into that formula and get thevalue for j.
We need both to determine, whether they really intersect

As for the intersection, since we have i, we can calculate the point of intersection by inserting it into
f: A + i * AB

return InRange(i, 0, 1) &amp;&amp; InRange(j, 0, 1)-&gt; if both i and j are in that range, they intersect!

</pre>
<p><span><img alt="happy.png" src="http://public.gamedev.net//public/style_emoticons/default/happy.png" /></span><br />
What you have to do now is to extrude AB by a specific factor so that it always intersects with the square around it.</p>
<p>Then you simply execute that above for all the 4 sides</p>

#4Aphton

Posted 23 January 2013 - 11:27 AM

If you are interested in the maths:

1. We have two lines f and g
2. f: A + i * AB
3. g: C + j * CD
4.
5. First, we need to find out, whether they are parallel
6. If they are parallel, then their slope must be equal
7. parallel: AB.Y/AB.X == CD.Y/CD.Y
8. If they are parallel, return false
9.
10. At this point, we know they are not paralle and two non-parallel lines in 2D intersect so this is valid:
11. A + i * AB = C + j * CD
12. We can rewrite that to:
13. I. A.X + i * AB.X = C.X + j * CD.X
14. II. A.Y + i * AB.Y = C.Y + j * CD.Y
15.
16. We determine j in I:
17. j = (A.X + i*AB.X - C.X) / CD.X
18. now we can substituate j in II
19. II. A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y
20.
21. After some tranformation (you can skip this):
22. > A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y
23. > A.Y + i*AB.Y = C.Y + (A.X/CD.X + i*AB.X/CD.X - C.X/ CD.X) * CD.Y
24. > A.Y + i*AB.Y = C.Y + A.X*CD.Y/CD.X + i*AB.X*CD.Y/CD.X - C.X*CD.Y/ CD.X
25. > i*AB.Y - i*AB.X*CD.Y/CD.X = C.Y + A.X*CD.Y/CD.X - C.X*CD.Y/ CD.X - A.Y
26. > i*(AB.Y - AB.X*CD.Y/CD.X) = C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y
27. > i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)
28.
29. We get:
30. i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)
31. We determined j previously. We simply insert the value of i into that formula and get thevalue for j.
32. We need both to determine, whether they really intersect
33.
34. As for the intersection, since we have i, we can calculate the point of intersection by inserting it into
35. f: A + i * AB
36.
37. return InRange(i, 0, 1) && InRange(j, 0, 1)-> if both i and j are in that range, they intersect!

What you have to do now is to extrude AB by a specific factor so that it always intersects with the square around it.

Then you simply execute that above for all the 4 sides

#3Aphton

Posted 23 January 2013 - 11:25 AM

If you are interested in the maths:

We have two lines f and g
f: A + i * AB
g: C + j * CD

First, we need to find out, whether they are parallel
If they are parallel, then their slope must be equal
parallel: AB.Y/AB.X == CD.Y/CD.Y
If they are parallel, return false

At this point, we know they are not paralle and two non-parallel lines in 2D intersect so this is valid:
A + i * AB = C + j * CD
We can rewrite that to:
I.  A.X + i * AB.X = C.X + j * CD.X
II. A.Y + i * AB.Y = C.Y + j * CD.Y

We determine j in I:
j = (A.X + i*AB.X - C.X) / CD.X
now we can substituate j in II
II. A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y

After some tranformation (you can skip this):
> A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y
> A.Y + i*AB.Y = C.Y + (A.X/CD.X + i*AB.X/CD.X - C.X/ CD.X) * CD.Y
> A.Y + i*AB.Y = C.Y + A.X*CD.Y/CD.X + i*AB.X*CD.Y/CD.X - C.X*CD.Y/ CD.X
> i*AB.Y - i*AB.X*CD.Y/CD.X = C.Y + A.X*CD.Y/CD.X - C.X*CD.Y/ CD.X - A.Y
> i*(AB.Y - AB.X*CD.Y/CD.X) = C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y
> i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)

We get:
i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)
We determined j previously. We simply insert the value of i into that formula and get thevalue for j. We need both to determine, whether they really intersect

As for the intersection, since we have i, we can calculate the point of intersection by inserting it into
f: A + i * AB

return InRange(i, 0, 1) && InRange(j, 0, 1)-> if both i and j are in that range, they intersect!

What you have to do now is to extrude AB by a specific factor so that it always intersects with the square around it. Then you simply execute that above for all the 4 sides

#2Aphton

Posted 23 January 2013 - 11:22 AM

If you are interested in the maths:

We have two lines f and gf: A + i * ABg: C + j * CDFirst, we need to find out, whether they are parallelIf they are parallel, then their slope must be equalparallel: AB.Y/AB.X == CD.Y/CD.YIf they are parallel, return falseAt this point, we know they are not paralle and two non-parallel lines in 2D intersect so this is valid:A + i * AB = C + j * CDWe can rewrite that to:I.  A.X + i * AB.X = C.X + j * CD.XII. A.Y + i * AB.Y = C.Y + j * CD.YWe determine j in I:j = (A.X + i*AB.X - C.X) / CD.Xno we can substituate j in IIII. A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.YAfter some tranformation (you can skip this):> A.Y + i*AB.Y = C.Y + ((A.X + i*AB.X - C.X) / CD.X) * CD.Y> A.Y + i*AB.Y = C.Y + (A.X/CD.X + i*AB.X/CD.X - C.X/ CD.X) * CD.Y> A.Y + i*AB.Y = C.Y + A.X*CD.Y/CD.X + i*AB.X*CD.Y/CD.X - C.X*CD.Y/ CD.X> i*AB.Y - i*AB.X*CD.Y/CD.X = C.Y + A.X*CD.Y/CD.X - C.X*CD.Y/ CD.X - A.Y> i*(AB.Y - AB.X*CD.Y/CD.X) = C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y> i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X);We get:i = (C.Y + (A.X - C.X)*CD.Y/CD.X - A.Y) / (AB.Y - AB.X*CD.Y/CD.X)We determined j previously. We simply insert the value of i into that formula and get thevalue for j. We need both to determine, whether they really intersectAs for the intersection, since we have i, we can calculate the point of intersection by inserting it into f: A + i * ABreturn InRange(i, 0, 1) && InRange(j, 0, 1)-> if both i and j are in that range, they intersect!

What you have to do now is to extrude AB by a specific factor so that it always intersects with the square around it. Then you simply execute that above for all the 4 sides

PARTNERS