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 6821219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:31:09+00:00 2026-05-26T21:31:09+00:00

I am developing a game with Flixel as a base, and part of what

  • 0

I am developing a game with Flixel as a base, and part of what I need is a way to check for collisions along a line (a line from point A to point B, specifically). Best way to explain this is I have a laser beam shooting from one ship to another object (or to a point in space if nothing is overlapping the line). I want the line to reach only until it hits an object. How can I determine mathematically / programatically where along a line the line is running into an object?

I could try measuring the length of the line and checking points for collision until one does, but that seems like way too much overhead to do every frame when I’m sure there is a mathematical way to determine it.

Edit: Before checking an object for collision with the line itself, I would first eliminate any objects not within the line’s bounding box – defined by the x of the left-most point, the y of the top-most point, the x of the right-most point, and the y of the bottom-most point. This will limit line-collision checks to a few objects.

Edit again: My question seems to still not be fully clear, sorry. Some of the solutions would probably work, but I’m looking for a simple, preferably mathematical solution. And when I say “rectangle” I mean one whose sides are locked to the x and y axis, not a rotatable rectangle. So a line is not a rectangle of width 0 unless it’s at 90 or -90 degrees (assuming 0 degrees points to the right of the screen).

Here’s a visual representation of what I’m trying to find:
Line Collision Detection

  • 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-26T21:31:09+00:00Added an answer on May 26, 2026 at 9:31 pm

    So, you have a line segment (A-B) and I gather that line segment is moving, and you want to know at what point the line segment will collide with another line segment (your ship, whatever).

    So mathematically what you want is to check when two lines intersect (two lines will always intersect unless parallel) and then check if the point where they intersect is on your screen.
    First you need to convert the line segments to line equations, something like this:

    typedef struct {
        GLfloat A;
        GLfloat B;
        GLfloat C;
    } Line;
    
    static inline Line LineMakeFromCoords(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {
        return (Line) {y2-y1, x1-x2, (y2-y1)*x1+(x1-x2)*y1};
    }
    
    static inline Line LineMakeFromSegment(Segment segment) {
        return LineMakeFromCoords(segment.P1.x,segment.P1.y,segment.P2.x,segment.P2.y);
    }
    

    Then check if they intersect

    static inline Point2D IntersectLines(Line line1, Line line2) {
        GLfloat det = line1.A*line2.B - line2.A*line1.B;
         if(det == 0){
        //Lines are parallel
                return (Point2D) {0.0, 0.0};  // FIXME should return nil
         }else{
                return (Point2D) {(line2.B*line1.C - line1.B*line2.C)/det, (line1.A*line2.C - line2.A*line1.C)/det};
         }  
    }
    

    Point2D will give you the intersect point, of course you have to test you line segment against all the ship’s line segments, which can be a bit time consuming, that’s were collision boxes, etc enter the picture.

    The math is all in wikipedia, check there if you need more info.

    Edit:

    Add-on to follow up comment:

    Same as before test your segment for collision against all four segments of the rectangle, you will get one of 3 cases:

    1. No collision/collision point not on screen(remember the collision tests are against lines, not line segments, and lines will always intersect unless parallel), taunt Player for missing 🙂
    2. One collision, draw/do whatever you want the segment you’re asking will be A-C (C collision point)
    3. Two collisions, check the size of each resulting segment (A-C1) and (A-C2) using something like the code below and keep the one with the shortest size.

      static inline float SegmentSizeFromPoints(Vertice3D P1, Vertice3D P2) {
           return sqrtf(powf((P1.x - P2.x),2.0) + pow((P1.y - P2.y),2.0));
      }
      

    The tricky bit when dealing with collisions, is figuring out ways of minimizing the number of tests you have to make.

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

Sidebar

Related Questions

I'm developing a game, a big part of this game, is about scrolling a
I'm developing a game for iPhone and is pretty far along, but one thing
I am currently developing a game which simulates an operating system. Therefore i need
I'm developing a game, that is running in a separate thread. Now I need
Hi i am developing game level editor.Currently I am using win32 with directX.But with
I'm developing a game for windows for learning purposes (I'm learning DirectX). I would
I'm developing chess game for Android ( androidchess.appspot.com ). If I want to add
I'm developing a game using XNA and C# and was attempting to avoid calling
I am developing a game with dozens of levels and each level has a
I'm developing a game in XNA. It would be nice for it to be

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.