Solved

Need the 2D equation for calculateing the collision with stright lines at differant angles

Posted on 2004-10-06
3
380 Views
Last Modified: 2010-04-07
Hello Experts,

I Need the 2D equation for calculateing the collision with stright lines at differant angles.(makeing a game in shockwave)
I will explain in a diagram.....

X
                         


                                      Y

Ok do you see X and Y, imagine there is a line joining them,
I need an equation or equations to calculate if a point hits the line.
Idealy if this equation would only work on the line width height and roataion it wolud be cool , however I would be very thankful just for the equation.

Also if you could throw in somemore colision detection for more roatated shapes like roatated rectangles, that would be good.
k thanks

0
Comment
Question by:FatNewbie
  • 3
3 Comments
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 125 total points
ID: 12286988
You have a line, on a grid (the screen co-ords) between point A and point B.  You want to know when point P will collide with this line.  

Bear in mind that the collision between point P and line AB will only occur in two spots along that line.  The x coord of one collision will be the same as the x coord of P (Px).  The y coord of the second collision will be the same as the y coord of P (Py).  As both the point and the line moves, these coords will chage, but the relationship remains the same: The collisions will occur at (Px, Uy) and (Ux, Py), where U is an unknown.  U can be thought of, now, as a coord, U(x,y), which must be solved.

You first need the gradient of that line:

    m = difference in y / difference in x
    m = (By - Ay) / (Bx - Ax)

Then, to get Ux, you would run through all the points between Ay and Py, adding the gradient, m, to an initial value of Ax (example in BASIC):

    Ux = Ax
    For i = Ay to Py
        Ux = Ux + m
    Next i

As you should see, this is the same as multiplying the difference between Ay and Py by m and adding the result to Ax.  This gives the following formula:

    Ux = Ax + (Py - Ay)m

or, as a coded equation:

    Ux = Ax + ((Py - Ay) * m)

You can now adapt this function to find Uy:

                             Px = Ax + (Uy - Ay)m
                      Px - Ax = (Uy - Ay)m  
              (Px - Ax) / m = Uy - Ay
    ((Px - Ax) / m) + Ay = Uy

Therefore, in longhand form:

   Collision #1 = [Px, (Ax + ((Py - Ay) * ((By - Ay)/(Bx - Ax)))]
   Collision #2 = [((Px - Ax) / ((By - Ay) / (Bx - Ax)) + Ay), Py]

Now all you need to do is clean up the code a little:

   If Px is not within the boundaries of AB, Collision #1 will never occur
   If Py is not within the boundaries of AB, Collision #2 will never occur
   If By = Ay then the gradient calculation will cause a divide by zero error; get around this
        by making Ay = Ay + 0.001 (it will cause slight errors in your results but it should not
        be noticable on screen).

Hope this helps you get started.  

J.

(NB:  Depending on the screen coord layout, you may find you have to swap/invert the method for finding the gradient,  e.g., instead of m = (By - Ay) / (Bx - Ax):

    m =  (Bx - Ax) / (By - Ay)     or
    m = -(By - Ay) / (Bx - Ax)     or
    m = -(Bx - Ax) / (By - Ay)     etc

Use some trial and error to get this to work to your liking)
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12287075
I wrote the following in QBASIC (for speed, you understand!!) to backup the theory I posted above.  I hope you can follow it

      SCREEN 13

      TYPE coord
              x       AS DOUBLE
              y       AS DOUBLE
      END TYPE

      DIM A AS coord                  'LinePointer
      DIM B AS coord                  'LinePointer
      DIM P AS coord                  'Point
      DIM C AS coord                  'Collision
      DIM M AS DOUBLE                 'Gradient

      A.x = 300:      A.y = 10
      B.x = 100:      B.y = 70
      P.x = 230:      P.y = 30

      LINE (A.x, A.y)-(B.x, B.y), 14
      CIRCLE (P.x, P.y), 1, 4

      M = (B.x - A.x) / (B.y - A.y)

      C.x = A.x + ((P.y - A.y) * M)
      C.y = A.y + ((P.x - A.x) / M)

      LINE (P.x, P.y)-(P.x, C.y), 12
      LINE (P.x, P.y)-(C.x, P.y), 12

If you can run it, see what it does and if it helps you...

J
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12287275
If you _do_ manage to run the above code, try changing the values of A.x, A.y, B.x, B.y, P.x and P.y...

J.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a commercial website built, but where do I start?? 5 259
3D Surface or Implicit Fairing Visual Studio 4 115
GPU processing of c++ library 7 103
copyEvens challenge 6 98
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now