Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3332362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:40:49+00:00 2026-05-17T23:40:49+00:00

After some research, I’ve decided to use the Liang-Barsky line clipping algorithm in my

  • 0

After some research, I’ve decided to use the Liang-Barsky line clipping algorithm in my 2D game. Google did not deliver on any VB.NET implementations of this algorithm but plenty C/++ ones. Therefore, as I have knowledge in C++, decided to port one found on Skytopia over to VB.Net. Unfortunately, it does not work with:

Public Class PhysicsObject
    Public Function CollideRay(ByVal p0 As Point, ByVal p1 As Point, ByRef clip0 As Point, ByRef clip1 As Point) As Boolean
        Dim t0 As Double = 0.0
        Dim t1 As Double = 1.0
        Dim xdelta As Double = p1.X - p0.X
        Dim ydelta As Double = p1.Y - p0.Y
        Dim p, q, r As Double

        For edge = 0 To 3
            ' Traverse through left, right, bottom, top edges
            If (edge = 0) Then
                p = -xdelta
                q = -(AABB.Left - p0.X)
            ElseIf (edge = 1) Then
                p = xdelta
                q = (AABB.Right - p0.X)
            ElseIf (edge = 2) Then
                p = -ydelta
                q = -(AABB.Bottom - p0.Y)
            ElseIf (edge = 3) Then
                p = ydelta
                q = (AABB.Top - p0.Y)
            End If

            r = q / p

            If p = 0 And q < 0 Then Return False ' Don't draw line at all. (parallel line outside)

            If p < 0 Then
                If r > t1 Then
                    Return False ' Don't draw line at all.
                ElseIf r > t0 Then
                    t0 = r ' Line is clipped!
                End If
            ElseIf p > 0 Then
                If r < t0 Then
                    Return False ' Don't draw line at all.
                ElseIf r < t1 Then
                    t1 = r ' Line is clipped!
                End If
            End If
        Next

        clip0.X = p0.X + t0 * xdelta
        clip0.Y = p0.Y + t0 * ydelta
        clip1.X = p0.X + t1 * xdelta
        clip1.Y = p0.Y + t1 * ydelta

        Return True        ' (clipped) line is drawn
    End Function

    Public AABB As Rectangle
End Class

I’m using the class/method like:

    Dim testPhysics As PhysicsObject = New PhysicsObject
    testPhysics.AABB = New Rectangle(30, 30, 20, 20)

    Dim p0, p1 As Point
    p0 = New Point(0, 0)
    p1 = New Point(120, 120)

    Dim clip0, clip1 As Point
    clip0 = New Point(-1, -1)
    clip1 = New Point(-1, -1)

    GlobalRenderer.Graphics.DrawLine(Pens.LimeGreen, p0, p1)

    If testPhysics.CollideRay(p0, p1, clip0, clip1) Then
        GlobalRenderer.Graphics.DrawLine(Pens.Magenta, clip0, clip1)
    End If

However, the CollideRay method fails on its 3rd edge iteration (edge = 3), r < t0, therefore the function returns false.

I’m wondering if anyone can spot some issue with my CollideRay function which would result in this behaviour, because I’m well and truly stumped.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:40:50+00:00Added an answer on May 17, 2026 at 11:40 pm

    The code assumes a different coordinate system, note that topEdge is larger than bottomEdge in the linked web page. Your test works with normal graphics coordinates where Bottom is larger than Top. You have to swap the bottom and top arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After some research, it is my understanding that this is not possible. I can
After some research: How to create a Zip File and some google research i
After some research i found a piece of code which shows how to expand
after some research, we opted for working with Drupal on our next project and
As always after some research I was unable to find anything of real value.
I am currently writing a paper on the Android platform. After some research ,
After some find and replace refactoring I ended up with this gem: const class
After some time I wanted to update my git repo, and then something went
After some stupid musings about Klingon languages, that came from this post I began
After some years of experience in the field, it seems to me that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.