Math help..

Monkey Forums/Monkey Programming/Math help..

Paul - Taiphoz(Posted 2013) [#1]
Here is the basic theory, I have a ball moving fast enough that it's current position and then next position are far enough apart that the ball will step over the object of interest.

The solution I am planning to use is to use Circle to Line intersection to see if the a line drawn from the balls current position to the balls new position intersects with any objects of interest.

As per the diagram bellow.



I plan to use 3 lines, one from each side of the ball, points A and B, so my plan is take the center of the ball, get the angle to the balls next position, then use this +/- 90 and push out ball radius to get the x,y of points A and B, then repeat that for the balls new position and I should have the A starting x,y and ending x,y to run my line intersection test on.

I am wondering if anyone has a better math formula for getting points A,B something better than my method ?

When an intersection is detected, my plan to get the balls collision position is to use the third lines collision point and then push the ball back until its distance from the object of interest is equal to both their radius.

So basically just wondering if my approach to this is sound, or if there is a better mathematical way to go about getting some of these values.


Markus(Posted 2013) [#2]
i believe you can use one line intersect and make the big circle bigger with the radius from ball.

the line circle intersect tells you the intersect point,with it you
have a position , a direction vector and direction * length you can add to the start vector.
with the normal vector at collision (direction from middle point to collision point) you can calculate the reflection vector.


Paul - Taiphoz(Posted 2013) [#3]
I have all the collision response done, this is purely about making sure the ball does not miss something it should be hitting no matter how fast it's moving.

Not sure how only using one line would work for example ???


Xaron(Posted 2013) [#4]
What about Box2d?


programmer(Posted 2013) [#5]
What about Box2d?
Monkey Box2d port is very slow.


muddy_shoes(Posted 2013) [#6]
I'm pretty sure it's fast enough to provide for the collision checking being asked about. I suspect Taiphoz would rather roll his own rather than deal with working out how to incorporate Box2D though.


Gerry Quinn(Posted 2013) [#7]
The idea with one line is that if a ball shrinks to a point and the obstacles it collides with get bigger by the radius of the ball, you have the same collisions.

Imagine the common case where a ball of 1-inch radius bounces around in a square box 12-inches in diameter.

The bounces will be the same as for an infinitesimally small ball bouncing around in a 10-inch box...

[Strictly speaking it is the collisions that will be the same - if you are doing complicated bounces involving spin etc. you have to work them out, but you can still use this method to detect the collisions in the first place.)


Markus(Posted 2013) [#8]
jo, Gerry had explained this :)

here i draw a pic for my fantasy:
https://docs.google.com/file/d/0B942ivL2fTGcbnpHWWk5UDJhZGM/edit?usp=sharing


AdamRedwoods(Posted 2013) [#9]
The idea with one line is that if a ball shrinks to a point and the obstacles it collides with get bigger by the radius of the ball, you have the same collisions.

This.

In miniB3D, the collisions are all line vs. sphere, or line vs. plane (move the plane out of distance radius). except in the broadphase, i convert the line of motion to a massive sphere, and do a sphere-to-sphere bounds check. then i do the finer checks.

i'd be surprised if box2D is too slow for most things.


Paul - Taiphoz(Posted 2013) [#10]
Id rather not delve into box2D for this, I do plan at some point to use Box2D for something tho.

Sorry if this sounds really dumb but I don't quite get your one line idea don't suppose you could draw a little pic to illustrate it ?


tiresius(Posted 2013) [#11]
Could you increase the number of times you update physics per step so it is a smaller time slice and less prone to this behavior? I've done this in the past, just a thought.


AdamRedwoods(Posted 2013) [#12]

note: v0=x0,yo ; v1=x1,y1

the left is circle-circle collision. you could solve this, though it wouldn't be pretty, solving the x^2+y^2=r formulas, etc.

the right side shows the circle shrinking and the collision time is the same. this will get you the circle's center coordinates at the point of intersection (vi0), but if you have the collision time (time=0.0 to 1.0) then you can solve given your line equation v0 + vf0*t = vi0. (v0, vf0, vi0 are x,y vectors)

if you need the intersection point, it would be a tad more complex. you would need to create a new line from vi0 to v1 and do a line-line intersection.

note you'll also get two solutions, so you need to find out which one is smaller (ie. the earlier collision).

All the math is here:
http://paulbourke.net/geometry/circlesphere/

EDIT:edited for clarity


AdamRedwoods(Posted 2013) [#13]
When an intersection is detected, my plan to get the balls collision position is to use the third lines collision point and then push the ball back until its distance from the object of interest is equal to both their radius.

as tiresius noted, you can also use smaller time steps.

the way miniB3D does it (collision response) is to create a new starting and finish position and retest for collisions.
-new starting would be the point of intersection.
-the new end would be the original movement vector projected onto the collision plane plus radius. this way the sliding energy is conserved a bit.

to do that in 2d, i believe it's just the dot product of a normal onto the movement line (line-segment projection?).


Paul - Taiphoz(Posted 2013) [#14]
thanks for insight guys. gives me something to read over, I will have a bash tomorrow and see where I get with it..


Markus(Posted 2013) [#15]
re: you could draw a little pic to illustrate it ?
i had edited my entry above with a google link to my image.
for a correct collision between a rolling ball and a other ball you need
a tube to circle collision. now reduce the tube radius in mind and add it to the
circle. this is the one line-circle idee. :)
i believe you can better understand if you move the ball in mind or on paper along a line over
or beside a obstacle.


Nobuyuki(Posted 2013) [#16]
brilliant on the sweep stuff, guys. I was thinking of doing it Taiphoz's way; never thought to increase the radius of everything else and use simple line segments


Jesse(Posted 2013) [#17]
This is how it's done using dynamic collision.