2

So I'm trying to make my first game on android. The thing is I have a small moving ball and I want it to bounce from a line that I drew. For that I need to find if the x,y of the ball are also coordinates of one dot from the line.

I tried to implement these equations about lines

x=a1 + t*u1

y=a2 + t*u2 => (x-a1)/u1=(y-a2)/u2 (t=t which has to be if the point is on the line)

where x and y are the coordinates I'm testing, dot[a1,a2] is a dot that is on the line and u(u1,u2) is the vector of the line.

heres the code:

       public boolean Collided()
   {
       float u1 =Math.abs(Math.round(begin_X)-Math.round(end_X));
       float u2 =Math.abs(Math.round(begin_Y)-Math.round(end_Y));          
       float t_x =Math.round((elect_X - begin_X)/u1);
       float t_y =Math.round((elect_Y - begin_Y)/u2);
       if(t_x==t_y)
       {
           return true;
       }
       else
       {
           return false;
       }       
   }

points [begin_X,end_X] and [begin_Y,end_Y] are the two points from the line and [elect_X,elect_Y] are the coordinates of the ball

theoretically it should work, but in the reality the ball most of the time just goes straight through the line or bounces somewhere else where it shouldn't

albert
  • 8,285
  • 3
  • 19
  • 32
Anderiel
  • 173
  • 2
  • 13
  • How about checking collision of two line segments (without rounding): [begin_x,begin_y]:[end_x,end_y] and [elect_x,elect_y]:[previous_elect_x,previous_elect_y] ? – Aki Suihkonen Oct 08 '12 at 14:36
  • Check this -> http://stackoverflow.com/questions/1073336/circle-line-collision-detection – japrescott Oct 08 '12 at 14:38
  • When I read the title of this question, I immediately thought http://en.wikipedia.org/wiki/The_Dot_and_the_Line. – cobaltduck Oct 08 '12 at 15:25

3 Answers3

0

I'm not sure what your code is doing, but looks weird you do some operations on x coordinates of your data, then on y, and at the end you want them to be equal;

go and try hereShortest distance between a point and a line segment and then if distance == 0 (or smaller on equal to radius of your ball) you will have collision

Community
  • 1
  • 1
user902383
  • 8,420
  • 8
  • 43
  • 63
0

The issue lies in the fact that you're testing whether the dot 'hits' the line that you want it to bounce from. I'm assuming that you're incrementing the position of your dot every frame with a small amount.

Say your dot is placed at [1,1], your line runs from [0,0] to [5,0], the velocity of your dot is 1 unit per second and the direction is [-1,0]. I'm assuming your calculating the increment based on the time per frame to allow for smoother animation.

What's happening is the following:

  1. Dot at [1,1]
  2. Time per frame = 0,7
  3. Dot is moved to [0.3,0]
  4. Test intersection = false
  5. --- next frame ---
  6. Dot at [0.3,0]
  7. Time per frame = 0.5 (this usually varies per frame)
  8. Dot is moved to [0.2,0]
  9. Test intersection = false

So the tests say there hasn't been an intersection because your testing discrete positions of your dot.

As Aki Suihkonen suggests you want to test for line intersection between a line formed by the last position + the current position and the line that you want your dot to collide with.

java.awt.geom.Line2D.linesIntersect(double X1, double Y1, double X2, double Y2, double X3, double Y3, double X4, double Y4) allows you to check for these intersections easily.

JeroenWarmerdam
  • 412
  • 2
  • 9
0

Your math is OK, but you the code isn't.

It's simpler to use the genearl line equation y = y1 + a(x - x1) where a = (y2 - y1) / ( x2 - x1) , being (x1,y1) and (x2,y2) to points from the line.

To get ball the distance from the line when the ball is at point (bx,by)use:

double a = (y2 - y1) / (x2 - x1);
double distance = (by - y1 - a * (bx - x1)) * Math.cos(Math.atan(a));

Now you can compare if Math.abs(distance) is bellow a specific value (i.e. ball diameter) to confirm collision.

Note: this only works for non vertical lines. If you have a vertical line just use:

double distance = bx - x1;

good luck.

Luis
  • 11,978
  • 3
  • 27
  • 35