maybe somebody can help me with this math problem

Monkey Forums/Monkey Code/maybe somebody can help me with this math problem

Jesse(Posted 2011) [#1]
I asked this question in blitzbasic.com but had no luck now I am posting it here. maybe I'll have better luck here or not.
I need to find the time of collision between a rotating line segment and a moving ball in the range between t=0 and t = 1.
I need to find the distance traveled in terms of t
when t = 0 angle is the starting angle(degrees traveled) = 0 and distance = 0
when t = 1 angle is a1 and distance is d1.
I need to find a2 and d2 the image shows all the know and unknown:


all of the black distances except for d1 have been calculated using dot product, cross dot product and scalars. and a couple of formulas that include division. I need the formula or formulas to solve this problem. thanks in advance and thanks for looking.


slenkar(Posted 2011) [#2]
I cast summon warpy


QuietBloke(Posted 2011) [#3]
I dont know what the formulae would be but just thinking back to when I wrote a simple pool game I split the problem into two parts.

So in your case the first thing is to see if the ball has passed through the line in time t.
You can calc the angle the ball was at at t0 and the angle of the ball at t1. if the ball was above the line at t0 and below the line at t1 or visa versa then it must have gone through.

The second part I used was a brute force method.
If the ball is close enough to the line at ( has only just passed through... you need to tweak what is deemed close enough. Lets say .5 pixels ).
Otherwise move the objects back to the starttime. Halve the timestep and move again. Repeat until your close enough.

eg.
t0 -> t1 - passed through but > .5 pixels. Not good enough

Take t0 as start time and set the step to be .5
t0 -> t0.5 not collided
t0.5 -> t1 passed through but > .5 pixel. Not good enough.

Take t0.5 as starttime and set the timestep to be .25
t0.5 -> t0.75 not collided
t0.75 -> t1 passed through but > .5 pixels. Not good enough

Take t0.75 as starttime and set the timestep to be .125
t0.75 -> t.875 passed through < .5 pixel. This is good enough.

The alternative would be when you have decided there is a collision then find out how fast they are heading towards each other set you timestep to be small enough and step through that until collision.

Did that make sense ?


Jesse(Posted 2011) [#4]

So in your case the first thing is to see if the ball has passed through the line in time t.
You can calc the angle the ball was at at t0 and the angle of the ball at t1. if the ball was above the line at t0 and below the line at t1 or visa versa then it must have gone through.


The scenario in the diagram is the condition when the ball penetrates the line at the time when t = 1. the problem is that I want to move back in time to where the ball collides the wall (where the ball is touching the wall). I don't have a problem figuring everything else out.


The second part I used was a brute force method.
If the ball is close enough to the line at ( has only just passed through... you need to tweak what is deemed close enough. Lets say .5 pixels ).
Otherwise move the objects back to the starttime. Halve the timestep and move again. Repeat until your close enough.


that is exactly what I want to avoid. I don't want to have to interpolate as I have tried it before and it just doesn't feel right. I want to figure out the most efficient way. I figured out d3 and d5 in terms of t with two formulas and I am almost sure there is a way to figure out the other one. note that also the line is moving so that complicates things a bit more.
I was thinking that it probably was more in terms of ratios. something like this:
i know that d1 = 1 in terms of t
and that a1 = 1 in terms of t
so the ratios would be:
a1/d1 = ax/d5 ----> a1*d5 = ax*d1
and to solve for ax:
ax = (a1*d5)/d1
this should return in ax the angle the line is when the ball collides with the starting angle and
if I divide ax by 2 it should be the angle the line is at the point of collision(at least my warped mind sees it that way).
the only thing is, that I believe, that this formula does not take into account the radius of the ball, while the formula to figure out d3 and d5 do.

thanks for trying to help. I really appreciate it.


Jesse(Posted 2011) [#5]
here is a blitzmax Mac demo of what I have so far, for anybody that wants to try it ou.
http://www.mediafire.com/?890fj4drgij8rep
I will be porting all of the code to monkey once I get the ball to ball fully implemented and the ball to moving wall code working.

the line sticking from the center of the ball is the speed of the ball from last time frame
to current time frame.


Warpy(Posted 2011) [#6]
Warpy is on holiday, so can't be summoned.

Instead of code, here's a plan of action:
- parameterise the positions, so:
ball: (x1+t*vx, y1+t*vy)
point on line: (x2+lambda*cos(t*speed), y2+lambda*sin(t*speed))
- collision occurs when distance between line and ball is the radius of the ball.
- substitute everything you've got into the formula for the distance between a line and a point
- solve for t

if you look very hard for some code on my site called either groups.bmx or poption, I think I solved this exact problem. I can't say eactly where though.


Jesse(Posted 2011) [#7]
I hope you are enjoying your vacations and sorry to bother you but I am going to need you to explain that a bit more if you don't mind.
I am also not too well skilled at math. The highest I took in school was trigonometry and didn't pay much attention to it so most of the stuff I learned from there I already forgot. Neither did I ever take a physics class. Most of what I know about physics is what I have learned from Tonypa website and a bit from searching the net.
...So, a few things:
can you explain to me what is "lambda"? that's beyond me.
is speed the speed of the line or the ball?
is x1 and y1 the position the ball is when t = 0?

just to note, movement has been integrated with time to prevent to continually multiplying, It is integrated at the beginning of the update cycle(vx*t and vy*t) and removed at the end of the update cycle(vx/t and vy/t). so it's like working with fixed step. I think that's what's confusing me. I guess all that I am working in this case is scalars so in this case t will be the movement scalar.

(vx,vy) ~ 1 in terms of time elapsed

[edit]
I looked at the "groups.bmx" but I couldn't figure anything out. it's probably that you are using a lot more complicated things than I can understand.


Redbeer(Posted 2011) [#8]
I think that when the ball collides with the line, the radius of the ball is perpendicular to that line because the line should be tangent to the ball.
Therefore, if you know a vector to represent the center position of the ball and a vector to represent the line direction at every frame/tick, you can use the cross product of that balls position vector, and the lines direction vector to calculate the "perpendicular" distance.
I think when that distance is equal to the radius of the ball, they've collided. You could then use the velocity vector of the ball, the initial and final position of the ball, and the simulation time to calculate the time at which they contact each other (if that's really what you need).

In the generic solution you can of course get positive and negative answers, which should represent the ball on either side of the line, but if the ball is constrained to a specific coordinate area, it probably won't be a problem, but you may need to use absolute value to compare to the radius.

Whether or not it overlaps and misses the collision really depends on how fast the ball is moving, because if it travels a distance that is "very large" in one frame/tick, in comparison to the radius of the ball, you may miss the collision point/time on the next calculation.


Jesse(Posted 2011) [#9]
if you look at the diagram, the diagram illustrates one frame tick. the line movement from start point to end point is how far the ball moves for one frame/tick. I can figure out all of the information on the diagram except for the values in read. d2 and a2 is what I am trying to find out.
I can find the exact position the ball collides with with a static line even if the movement of the ball is any speed grater than the radius by doing scalar multiplications and division.
the equation fallows:
dtn = (radius-dt1)/(dt2-dt1) 

dtn is the scalar distance from the ball to the wall.
to find the exact point of collision:
ballCollisionx = ballx+dtn*ballvx
ballCollisiony = bally+dtn *ballvy
in this case there is no need to step through the path until a possible collision point is reached. Just perfect!
so in other words that is not the problem.
the problem is: At what point do the rotating line and the moving ball meet?
and I want to find a single formula that can solve that.
I am still trying to figure out what warpy did in his program.