|
-
Dec 4th, 2000, 12:08 AM
#1
Thread Starter
New Member
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.
-
Dec 4th, 2000, 12:29 AM
#2
transcendental analytic
Here's exactly what you need, i hope you understand the UDT's 
Code:
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 12th, 2000, 02:10 AM
#3
Frenzied Member
Try this:
Code:
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.
Harry.
"From one thing, know ten thousand things."
-
Dec 12th, 2000, 02:35 AM
#4
transcendental analytic
Sorry about that.. Maybe got tired
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 12th, 2000, 02:51 AM
#5
Frenzied Member
Yeah that was probably it
Harry.
"From one thing, know ten thousand things."
-
Dec 12th, 2000, 12:20 PM
#6
Frenzied Member
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!).
Code:
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.
-
Dec 12th, 2000, 06:58 PM
#7
Thread Starter
New Member
Uh...
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.
Code:
Sub PostMessage(Message As String)
On Error Goto Debug
Forum.Messages.Post Message, Name
Exit Sub
Debug:
For ctr = 1 To Bugs.Count
Bugs(ctr).Squish
If Bugs(ctr).Status <> vbDead Then
Flamethrower.Target = Bug(ctr)
Flamethrower.Burn
End If
Next ctr
Resume
End Sub
-
Dec 13th, 2000, 03:06 AM
#8
Fanatic Member
I think this is the function Jotaf is talking about
Code:
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.
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Dec 13th, 2000, 06:05 AM
#9
transcendental analytic
Sorry about that (again )
last line should be
If ISCross Then intersection.X = lne1.coord2.X + s * dx1: intersection.Y = lne1.coord2.Y + s * dy1
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 13th, 2000, 03:12 PM
#10
Thread Starter
New Member
Thanks
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.
Code:
Sub PostMessage(Message As String)
On Error Goto Debug
Forum.Messages.Post Message, Name
Exit Sub
Debug:
For ctr = 1 To Bugs.Count
Bugs(ctr).Squish
If Bugs(ctr).Status <> vbDead Then
Flamethrower.Target = Bug(ctr)
Flamethrower.Burn
End If
Next ctr
Resume
End Sub
-
Dec 14th, 2000, 10:42 AM
#11
Frenzied Member
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:
Code:
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
-
Dec 14th, 2000, 10:45 AM
#12
Frenzied Member
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
-
Dec 14th, 2000, 10:47 AM
#13
Frenzied Member
Just another thing, and sorry for posting this much:
I'm just curious about where in agame intercecting 2 lines is needed...
-
Dec 14th, 2000, 01:36 PM
#14
Lively Member
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!
-
Dec 16th, 2000, 11:46 AM
#15
Frenzied Member
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:
Code:
If Map(playerx - 1, playery) = tileGrass Then
playerx = playerx - 1
End If
Same thing for right...
Code:
If Map(playerx + 1, playery) = tileGrass Then
playerx = playerx + 1
End If
This one's for moving up:
Code:
If Map(playerx, playery - 1) = tileGrass Then
playery = playery - 1
End If
And this one's for moving down:
Code:
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!
-
Dec 16th, 2000, 02:45 PM
#16
transcendental analytic
Code:
'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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 16th, 2000, 04:00 PM
#17
Lively Member
aaaah... mucho coolness. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|