line array and image collision detection
line array and image collision detection
line array and image collision detection
line array and image collision detection
line array and image collision detection
line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection
line array and image collision detection line array and image collision detection
line array and image collision detection
Go Back  Xtreme Visual Basic Talk > > > line array and image collision detection


Reply
 
Thread Tools Display Modes
  #1  
Old 07-17-2013, 01:15 PM
cozzeh cozzeh is offline
Newcomer
 
Join Date: Jul 2013
Posts: 1
Default line array and image collision detection


Hi,
I am working on a side scroll game for my computing project and cant figure out how to make collision detection work properly. I've tried various different ways myself, but I then found a great way written by IcePlug.

However, this only works for a single line and i am using an array, please can anyone adapt the code to use an array of lines.
Thanks.

This is the original code:

Code:
Public Function LSContact(L1 As Line, S1 As Control) As Boolean
Dim M As Double, B As Double
Dim L As Single, RG As Single, T As Single, BO As Single
If L1.X1 < L1.X2 Then
    L = L1.X1: RG = L1.X2
Else
    L = L1.X2: RG = L1.X1
End If
If L1.Y1 < L1.Y2 Then
    T = L1.Y1: BO = L1.Y2
Else
    T = L1.Y2: BO = L1.Y1
End If

'testing the endpoints to see if the line goes through the square.
If L1.X2 - L1.X1 = 0 Then
    M = 350000
Else
    M = (L1.Y2 - L1.Y1) / (L1.X2 - L1.X1)
End If
B = L1.Y1 - M * L1.X1
If M > 0 Then
'Sloping downwards
    If (S1.Top + S1.Height) > M * S1.Left + B And S1.Top < M * (S1.Left + S1.Width) + B Then
'This checks if the top right corner is above the line and the bottom left is below the line.
        If S1.Left + S1.Width > L And S1.Left < RG And S1.Top + S1.Height > T And S1.Top < BO Then
'This checks if the Line Rect is intersecting the control
                LSContact = True
        End If
    End If
Else
'Sloping upwards
    If S1.Top < M * S1.Left + B And (S1.Top + S1.Height) > M * (S1.Left + S1.Width) + B Then
'This checks if the top left corner is above the line and the bottom right corner is below the line.
        If S1.Left + S1.Width > L And S1.Left < RG And S1.Top + S1.Height > T And S1.Top < BO Then
'This checks if the Line Rect is intersecting the control
            LSContact = True
        End If
    End If
End If

Last edited by cozzeh; 07-17-2013 at 02:14 PM.
Reply With Quote
  #2  
Old 07-17-2013, 02:53 PM
Gruff's Avatar
Gruffline array and image collision detection Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

All you should have to do is call this function in a loop
that iterates through your array of lines checking for true or false after each call.

The hard part has been done for you. don't change this function.

Something like
Code:
For i as integer = 0 to Number_of_lines - 1 if LSContact(LineArray(i), Control) then ' Collided with a line. ' Exit Game Loop or whatever. end if Next i
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
line array and image collision detection
line array and image collision detection
line array and image collision detection line array and image collision detection
line array and image collision detection
line array and image collision detection
line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection line array and image collision detection
line array and image collision detection
line array and image collision detection
 
line array and image collision detection
line array and image collision detection
 
-->