vendredi 19 septembre 2008

nds Pinball 1 lines and points


Just fun, this image seen at www.gooncity.com

We tried to make PINBALL on the nds. (or a minigolf, whatever takes a ball to bounce.)
A good exercise to reduce calculations and use the most simple numbers, as few doubles as possible!
The first algorithm is just a simple (naïve) reflection idea.
The ball has a position and a speed, and every time we calculate we advance the ball with its speed. We see if the ball crosses a line, if so, calculate the reflection.

We needed a point (2D) and a line struct.

The POINT struct:
typedef struct intPoint{
double x; //these members have to become int's to save space and calc's!)
double y;
}POINT;

with a few useful methods like:
dot
length
distance
checkPointBetween (just see if a point lies between two other points

nice was to discover that the sqrt (of math.h) does not function under the devkitpro with structmembers,
so we had to write our own sqrt algorithm (haha!). Found this:

double mySqrt (double value)
{
const double tol = 0.000005; // relative error tolerance
double oldval, newval;

oldval = value; // take value as first approximation
newval = (oldval + value/oldval)/2;
while (fabs((newval-oldval)/newval) > tol)
{
oldval = newval;
newval = (oldval + value/oldval)/2;
}
return newval;
}

For the line:
typedef struct Line{
POINT p1;
POINT p2;
double cx ;//these members have to become int's to save space and calc's!)
double cy ;
double cc ;
}LINE;

with methods like:
reflection
intersection
projection
(using only geometrical methods)


All went well, (after a lot of debugging) but one can simple see that
the ball can always escape if just colliding on the point between to lines of a side.

Two escape route scenarios:



The solution might be to look after reflection if the line between the old ball position
and the new ball position crosses a line. If so, reverse speed...

But there is another way to tackle the problem, using triangles.
The field is partitioned in triangles.
The ball is always in a triangle, so only three lines to deal with.
Calculate the dcrossing and determine if the side is closed or open. If open the ball goes to the new trangle, if closed we get a reflection.
Only problem: it is too fast, (haha). Because only crossing points are calculated we have to restrain the ball to get an even movement.

Screenshot from a Devcpp prog:(with a graphic window)


Problem: if the ball is moving right on a point. We have to reflect it back.

Then we need a way to generate the triangle information.

we found Delaunay triangulation, together with some nice JAVA progs:
http://www.cs.cornell.edu/Info/People/chew/Delaunay.html
http://goanna.cs.rmit.edu.au/~gl/research/comp_geom/delaunay/delaunay.html

to be continued