## Marble labyrinth game + source code

Share with us how are you using the powerrrr of the force

Moderator: Alain

### Marble labyrinth game + source code

I put together a week hack of a marble labyrinth game using the new Mac version of Newton (cheers guys). It has a fairly neat fake reflection effect. I thought you'd enjoy playing it (or at least seeing it -- for everyone without a Mac) and having the source code. It obviously had to support the AMS (apple-motion-sensor) for tilting on Apple's new laptops.

You can get it here: http://will.thimbleby.net/physics . I'm struggling to get the physics exactly how I want -- so if anyone has suggestions to get marbles rolling-around fast let me have 'em.

cheers --Will
willThimbleby

Posts: 6
Joined: Sat Nov 20, 2004 5:19 pm

With and example in the tutorial 10 using joint.

This joint is very special for game with balls, like that demo, pool games, pinball, bowling, etc. And it is extremely easy to use.
Julio Jerez
Moderator

Posts: 11086
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Dude awesome, I see you did not have problems with materials.
I am impressed.
Julio Jerez
Moderator

Posts: 11086
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

To make the ball more responsive you need to set the viscous linear and angular damping coefficient to zero.
There is a default value of 0.1 for both in Newton. But you can override it

This two calls

Code: Select all
`float damp[3];damp[0] = 0.0f;damp[1] = 0.0f;damp[2] = 0.0f;NewtonBodySetLinearDamping (boxBody, 0.0f);NewtonBodySetAngularDamping (boxBody, damp);`
Then to make more natural you can assign a dryfriction joint to the ball, and with some adjustment you can make it to behave almost 100% like the real thing
Julio Jerez
Moderator

Posts: 11086
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

how did you manage to do the ball reflection effect.. ?? (as it looks cool)

cleathley

Posts: 63
Joined: Sat May 22, 2004 12:19 am
Location: Perth, Australia

There is a good example in C/OpenGL, that explains how to use cube mapping for reflection effects. Hope it helps. Pinball looks great!

http://www.missouri.edu/~irsa39/opengl/
A1200

Posts: 2
Joined: Fri Mar 11, 2005 7:41 pm

Cheers Julio, I'll go off and try that dryfriction joint now.

Heh, glad you think the reflection looks cool, it looks much better moving, and probably even better in pinball I looked for some time for a good way to do reflections and in the end came up with my own. It will be about 6x faster than cube mapping, and looks just as good. It is also easier.

The rough algorithm is:
1. Render a 160 degrees camera positioned on the ball aimed at the main camera into a texture -- making sure the texture is clamped at the edges
2. Draw a hemisphere painted with that texture also aimed at the camera. With the same up vector as the used for reflection the texture.

The texture coordinates are then: (0.5-tan(theta3-M_PI/2)*zeye), (0.5+tan(theta1)*zeye)

where theta3 is the angle swept round the up-vector and theta1 is the angle from the top pole of the sphere. Where zeye = 0.5 / tan(reflectionAngle/2*M_PI/180); and the reflectionAngle is 160 degrees.

I use a 256x256 texture and render the reflection with no shadows but this is probably far too much. And a 160 degrees reflection angle which provides a near perfect reflection, the edges which get clamped are un-noticable and the centre of the reflection is not distorted much. It does look very cool moving and is a fast effect to render.

If you want real code, have a look at the Controller.m file in my source code (http://www.adlam.plus.com/will/downloads/LabyrinthSrc.zip) the functions renderReflection and renderBall are what you want to look at. They are straight forward C/OpenGL.
willThimbleby

Posts: 6
Joined: Sat Nov 20, 2004 5:19 pm

I tried the dryfriction joint and I can't say I noticed what it did. One odd thing with the marble rolling, is that if the friction is high the marble never rolls, no matter what angle the slope is. Surely the marble should roll no matter what the friction? Am I doing something wrong?

I haven't got one of these games to hand, but I remember it being very frustrating, you tilt the board slightly and the marble zooms off into a hole. My game isn't frustrating enough yet.
willThimbleby

Posts: 6
Joined: Sat Nov 20, 2004 5:19 pm

When you say friction are you taking about the material friction?
Also what value for the mass and inertial are you using? This is very important because that is what determines whether the ball would roll or not.
Julio Jerez
Moderator

Posts: 11086
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

I see you are using the same setup I used in the demos, but the demos are made to show Newton in the worst possible settings and show that it is still stable.
In your code, these lines are setting the mass and the inertias to arbitrary uncorrelated values and that break the realism.

Code: Select all
`// set the body mass and inertiaNewtonBodySetMassMatrix (ballBody, 4.0f, 4.0f, 4.0f, 4.0f);`
For good behavior you most do like this

Code: Select all
`float Ixx = 0.4f * mass * radius * radius;float Iyy = Ixx;float Izz = Ixx;NewtonBodySetMassMatrix (boxBody, mass, Ixx, Iyy, Izz);`

For example for a ball of radius 0.5 and mass 4.0:
Ixx = 0.4 * 4.0 * 0.5 * 0.5 = 0.4

Why this is important? It is important because physics is nothing but the balancing calculation of the distribution of energy between bodies in motion.

When you place a ball on a slope and let it go down the ramp. At the beginning the total energy is
Et = M * G * H

M = mass
G = gravity
H = the height form where the ball is to where the ball goes.

If the ball ought to roll down without sliding (not energy lost due to friction)
The total energy of the system must be constant

M * G * H0 = Et = M * G * H + M * v ^2 + I * w ^2 (Ignoring the Â½ for the sake of analysis)

What this means is that as the ball goes down part of the energy lost from losing altitude is converted to linear and angular energy.
However since the ball cannot slide then the conversion of energy is correlated by the ratio between mass and inertia, the higher the moment of inertia the more resistance to roll because a small angular velocity will be enough to balance the equation.

Another way to see this is by just the definition of inertia and mass, it is the resistance of a body to change its state of motion under the present of a external force (wasn't Newton great 300 years and we still banking on that law, even thought some people in some forums are trying to re-invent it, with this pseudo physics that is floating around)
So based on that if a body has a very high moment of inertia and it is at rest, it will offer a great resistance to roll, the higher the inertia the greater the resistance.

willThimbleby wrote:I tried the dryfriction joint and I can't say I noticed what it did. One odd thing with the marble rolling, is that if the friction is high the marble never rolls, no matter what angle the slope is. Surely the marble should roll no matter what the friction? Am I doing something wrong?

No true if a ball have a very high inertia will not roll at all, and just slide down the slope if it breaks the static friction (if not, it will stay at rest.) If you check on some college text books you can probable find experiments where they place a Solid ball, a hollow ball, and a box in a ramp and let them slide down. The box get to bottom first, follow it by the hollow ball and the solid ball come a last.

That is explained by the above equation which is written below symbolically.

E = M* V^2 + I * W^2

Ve = linear energy
We = angular energy

The box just slides down so all the potential energy is converted to linear energy, allowing for a higher linear velocity.
The hollow box has very small inertia and very little resistance to roll which means W must grow faster to balance the equation.
The solid ball have larger I, so smaller amount of W is sufficient to balance the equation.

In the end all make sense, a ball with larger inertia will take longer to start to roll, increase the inertia and it will not roll at all.

To conclude, set the inertia correctly, set the viscous friction to zero, and use a rolling friction joint and everything should be OK. Notice that this is not tweaking or managing it is just setting appropriate parameters in order to get result that can be compared to reality.
Julio Jerez
Moderator

Posts: 11086
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Julio Jerez wrote:For good behavior you most do like this...

Hmmm.. yes silly me. Thanks for the good explanation.
willThimbleby

Posts: 6
Joined: Sat Nov 20, 2004 5:19 pm

thanks for the explanation of the mirroring (or reflection) in the ball. I guess I could get away with just rendering the playfield texture at point as the ball in a pinball is generally moving a lot LOL.

I added in the new dry rolling friction joint and it certainly works a lot better on helping the ball roll properly..

cleathley

Posts: 63
Joined: Sat May 22, 2004 12:19 am
Location: Perth, Australia