Click to See Complete Forum and Search --> : Collision detection
pollier
Dec 3rd, 2000, 11:08 PM
Okay - I need collision detetion. I know what my formula does, but I'm not exactly sure of the equation.
Suppose you have two points, P1, and P2. Each has an X1, Y1, X2, and a Y2. Basically they're lines. I know if they're parallel or not, but if they aren't parallel, how can I find the point at which the two lines will interect? I just need the X and Y coordinates. Thanks.
________________________
The Answer to Life, The Universe, And Everything...
is...
is...
42.
kedaman
Dec 3rd, 2000, 11:29 PM
Here's exactly what you need, i hope you understand the UDT's :)
Function ISCross(lne1 As Cline, lne2 As Cline, intersection As Coordinate) As Boolean
Dim dx1!, dy1!, dx2!, dy2!, n!, s!
dx1 = lne1.coord2.X - lne1.coord1.X
dy1 = lne1.coord2.Y - lne1.coord1.Y
dx1 = lne2.coord2.X - lne2.coord1.X
dy2 = lne2.coord2.Y - lne2.coord1.Y
If (dx2 * dy1 - dy2 * dx1) = 0 Then
'they are parallell
ISCross = False
Exit Function
End If
n = (dx1 * (lne2.coord1.Y - lne1.coord1.Y) + dy1 * (lne1.coord1.X - lne2.coord1.X)) / (dx2 * dy1 - dy2 * dx1)
t = (dx2 * (lne1.coord1.Y - lne2.coord1.Y) + dy2 * (lne2.coord1.X - lne1.coord1.X)) / (dy2 * dx1 - dx2 * dy1)
ISCross = s >= 0 And s <= 1 And n >= 0 And n <= 1
'the intersection is then:
If ISCross Then intersection.X = X1 + t * dx: intersection.Y = Y1 + t * dy
End Function
HarryW
Dec 12th, 2000, 01:10 AM
Try this:
Function ISCross(lne1 As Cline, lne2 As Cline, intersection As Coordinate) As Boolean
Dim dx1!, dy1!, dx2!, dy2!, n!, s!
dx1 = lne1.coord2.X - lne1.coord1.X
dy1 = lne1.coord2.Y - lne1.coord1.Y
dx2 = lne2.coord2.X - lne2.coord1.X
dy2 = lne2.coord2.Y - lne2.coord1.Y
If (dx2 * dy1 - dy2 * dx1) = 0 Then
'they are parallell
ISCross = False
Exit Function
End If
n = (dx1 * (lne2.coord1.Y - lne1.coord1.Y) + dy1 * (lne1.coord1.X - lne2.coord1.X)) / (dx2 * dy1 - dy2 * dx1)
s = (dx2 * (lne1.coord1.Y - lne2.coord1.Y) + dy2 * (lne2.coord1.X - lne1.coord1.X)) / (dy2 * dx1 - dx2 * dy1)
ISCross = s >= 0 And s <= 1 And n >= 0 And n <= 1
'the intersection is then:
If ISCross Then intersection.X = X1 + s * dx: intersection.Y = Y1 + s * dy
End Function
Just a couple of things that look like they're wrong chaged. Just typos.
kedaman
Dec 12th, 2000, 01:35 AM
Sorry about that.. Maybe got tired
HarryW
Dec 12th, 2000, 01:51 AM
Yeah that was probably it ;)
Jotaf98
Dec 12th, 2000, 11:20 AM
Hum...I think I once saw a function like that, but it consisted of an If with 4 conditions (not sure if this works). The guy who did it said it was the fastest method. It needed the type Rect (hope you know what I'm talking about!).
public function RectInRect(rRect1 as rect, rect2 as rect) as boolean
if rect1.top < rect2.top and _
rect1.left < rect2.left and _
rect1.bottom > rect2.bottom and _
rect1.right > rect3.right then RectInRect = True
end function
Of course, I'm not sure if it works. Give it a try.
pollier
Dec 12th, 2000, 05:58 PM
Thanks, but it's still not totally corrected...
There are still some different variables, for example X1 suddenly pops up in the last line, and so does Y1, dx, and dy, and none of these have been used in an earlier equation.
YoungBuck
Dec 13th, 2000, 02:06 AM
I think this is the function Jotaf is talking about
Declare Function IntersectRect Lib "user32" Alias "IntersectRect" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
lpDestRect = Buffer to return the coordinates of the actual area of which lpSrc1Rect and lpSrc2Rect intersect, most likely not needed for what you are looking for
lpSrc1Rect and lpSrc2Rect = The rectangles which define the two images you want detect if they collide
You will have to run a loop to test all Images on the screen to see if they intersect.
kedaman
Dec 13th, 2000, 05:05 AM
Sorry about that (again:o)
last line should be
If ISCross Then intersection.X = lne1.coord2.X + s * dx1: intersection.Y = lne1.coord2.Y + s * dy1
pollier
Dec 13th, 2000, 02:12 PM
Thanks, I have yet to test it, and I'll let you know if it works.
I am not using the IntersectRect stuff because the things I am intersecting are not rectangular. So you don't need to tell me the rectangle things.
Jotaf98
Dec 14th, 2000, 09:42 AM
You're right, you're not using rectangular objects, you're using square objects ;) (joke)
YoungBuck, thanks for the function! But I once saw it without the DLL... Wait, I now remember it:
If r1.Left > r2.Right And _
r1.Left < r2.Right And _
r1.Top > r2.Bottom And _
r1.Top < r2.Bottom Then
'Return True
End If
This is the exact code, give it a try! r1 is the first rect, r2 is the second one. If you want, I can explain the RECT type too, it's not very complex :)
Jotaf98
Dec 14th, 2000, 09:45 AM
Oops, sometimes I'm soooo stupid: you said you were using lines and I completely forgot it, this one is for testing if an object is on top of the other one :p
Jotaf98
Dec 14th, 2000, 09:47 AM
Just another thing, and sorry for posting this much:
I'm just curious about where in agame intercecting 2 lines is needed...
Xero
Dec 14th, 2000, 12:36 PM
I have another question about collision detection. I'm using IntersectRect for collision detection in a tile-based environment. Its using an array Map(#,#) = 0 or 1, 0 being water, 1 being grass. And i need to have it detect when the player is on a water(0) tile and not allow them to move over it. uhh.... help!
Jotaf98
Dec 16th, 2000, 10:46 AM
That one's easy!
You have the player's coordinates: playerx and playery.
When the player presses the key to move left, use this piece of code:
If Map(playerx - 1, playery) = tileGrass Then
playerx = playerx - 1
End If
Same thing for right...
If Map(playerx + 1, playery) = tileGrass Then
playerx = playerx + 1
End If
This one's for moving up:
If Map(playerx, playery - 1) = tileGrass Then
playery = playery - 1
End If
And this one's for moving down:
If Map(playerx, playery + 1) = tileGrass Then
playery = playery + 1
End If
That should do it! All you need now is to draw the player at the new coordinates!
kedaman
Dec 16th, 2000, 01:45 PM
'your detection of keys goes here, and they should only set the offsets.
if map(x+xoffset,y+yoffset)>0 then
x=x+xoffset
y=y+yoffset
end if
Xero
Dec 16th, 2000, 03:00 PM
aaaah... mucho coolness. Thanks!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.