Re: [2005] Line Collision
Re: [2005] Line Collision
Re: [2005] Line Collision
Thanks but what do I put for the the a1, a2, b1 and b2 arguments?
I presume the A1 and A2 arguments are angles. Are the B1 and B2 arguments how long the line is?
At the moment my code is:
Code:
SegmentsIntersect(5,5,50,5,??,??,??,??)
What are the bold arguments?
Code:
' Return True if the segments intersect.
Private Function SegmentsIntersect(ByVal X1 As Single, _
ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As _
Single, ByVal A1 As Single, ByVal B1 As Single, ByVal _
A2 As Single, ByVal B2 As Single) As Boolean
Dim dx As Single
Dim dy As Single
Dim da As Single
Dim db As Single
Dim t As Single
Dim s As Single
dx = X2 - X1
dy = Y2 - Y1
da = A2 - A1
db = B2 - B1
If (da * dy - db * dx) = 0 Then
' The segments are parallel.
SegmentsIntersect = False
Exit Function
End If
s = (dx * (B1 - Y1) + dy * (X1 - A1)) / (da * dy - db * _
dx)
t = (da * (Y1 - B1) + db * (A1 - X1)) / (db * dx - da * _
dy)
SegmentsIntersect = (s >= 0# And s <= 1# And _
t >= 0# And t <= 1#)
' If it exists, the point of intersection is:
' (x1 + t * dx, y1 + t * dy)
End Function
Re: [2005] Line Collision
At a glance I'd guess that the Xs and Ys are the end points of one line and the As and Bs are the end points of the other.
Re: [2005] Line Collision
With the arguments set to the end points of the lines it doesn't work: :(
Code:
e.Graphics.DrawLine(Pens.Black, 5, 5, 50, 50)
e.Graphics.DrawLine(Pens.Black, 50, 5, 20, 80)
e.Graphics.DrawString(SegmentsIntersect(5, 5, 50, 5, 50, 50, 20, 80), Me.Font, Brushes.Black, 50, 50)
[EDIT]
What I'm trying to do is get it so I can check if the direction the circle is heading in will make the circle collide with the square(see picture).
http://www.rkwebcreations.co.uk/collision.png
[EDIT]
Re: [2005] Line Collision
Why dont you use the values of the line, rather than hand written co-ords remove room for error. If that still fails can you post up your app. I may have time to take a look
Pino
1 Attachment(s)
Re: [2005] Line Collision
Re: [2005] Line Collision
Ok, can you give me the lowdown on what you are trying to achieve?
Re: [2005] Line Collision
I've got a picturebox and I want it to be able to check if it is going to collide with something in the direction it is currently going in.
If it does collide it will change direction until it finds somewhere where it will not collide but can still get past the object it was going to collide with.