Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Ball simulation

This thread is locked; no one can reply to it. rss feed Print
Ball simulation
Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Hi guys, (and gals if there are any),

I was inspired to make a ball simulation based on https://www.allegro.cc/forums/thread/617607, and I now have a fully functional if partially broken simulator.

It's pretty stable, but there must be some small condition I'm missing that allows balls to escape my 'pen'.

{"name":"611793","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/9\/b95642beba27f3d707743eea718cb23e.png","w":1026,"h":801,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/9\/b95642beba27f3d707743eea718cb23e"}611793

As you can see, there are many balls with many different velocities and accelerations, and the intercept times are all calculated properly, except for every once in a while, a collision is failed to be detected. And a few balls still escape the pen.

Can anyone help me figure out what is going on?

I narrowed it down to this ;

1. An intercept is detected.
2. I advance time to the first intercept
3. At this point, the balls should touch, but not overlap (distance = 0)
4. Sometimes the ball overlaps
5. Time is advanced to the next intercept
6. Sometimes its so quick that the overlapping ball gets hit by another ball, driving it even further into the first ball. No collision can be detected because they already overlap.

In an attempt to remedy #4 I used the normal direction and "Pushed" the balls away from each other by half the difference between the distance and the total radial distance.

What can I do to prevent them from overlapping in the first place? "Rewind time" by a small amount so they never overlap in the first place?

I need to work on the numerical stability of the program as well, if anyone has any tips on that I would appreciate it.

I know I said never to move objects into overlap positions, but I'm not sure how to do that here. I feel like I'm going to have to 'cheat' somehow.

All the code is on Github here :

https://github.com/EdgarReynaldo/Merlin

GullRaDriel
Member #3,861
September 2003
avatar

I build it on my comp using git allegro source and shared library (had to modify the project options)

It never escaped yet...

And I tried stressing it !

Proof:

{"name":"611794","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/6\/26b76e8283779a5d7de76e5ec545daf6.png","w":1033,"h":812,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/6\/26b76e8283779a5d7de76e5ec545daf6"}611794

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

That's amazing! How did you get them to stay put! ???

EDIT
What did you compile them with? What OS and architecture are you using?

Pull from git and try again. This time try with only 10-20 large balls (small balls can be launched with the RMB. The farther you are from the middle of the screen with the mouse, the faster they will go. Leave a little space.

I added a workspace project. I can add dynamic linking projects too if you want.

dakatt
Member #10,695
February 2009

I can't build the project but that's my lack of build exp rather than your fault. I don't know if it will help (considering one of the issues in my engine is the problem you set out to solve) but this is what I have so far

https://github.com/DaKatt/P2D

To setup create a CP2D_Engine object in main, then use ->Update() and ->Draw() in the main loop. To create a body use ->Add_Body(). I don't know how I'd incorporate the collision times that you spoke of before Edgar but maybe splitting the overlap and collision resolution functions could achieve a similar result?

Chris Katko
Member #1,881
January 2002
avatar

I just want to re-iterate that I freakin' love the "graph" like presentation. My brain is like "OOoooOOOhhh that could be a game."

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If you build it they will come.

Join me, and together we can rule the galaxy as Father and Son!

video

Dakatt said:

I can't build the project but that's my lack of build exp rather than your fault. I don't know if it will help (considering one of the issues in my engine is the problem you set out to solve) but this is what I have so far

It should be easier now. I included a workspace with a static library set up. You just have to tell it where to find allegro. Load the workspace in CB and rebuild.

dakatt said:

https://github.com/DaKatt/P2D

To setup create a CP2D_Engine object in main, then use ->Update() and ->Draw() in the main loop. To create a body use ->Add_Body(). I don't know how I'd incorporate the collision times that you spoke of before Edgar but maybe splitting the overlap and collision resolution functions could achieve a similar result?

If you're going to use intercept times, then you need a collision table. You can see how I did it in my Merlin (Interceptor) library here :

https://github.com/EdgarReynaldo/Merlin

Check this line in your p2d_body.h file :

https://github.com/DaKatt/P2D/blob/master/p2d_body.h#L73

distance will never be negative, fabs is unnecessary. Just a tip.

Could it be because of the default rounding mode? Do I have to set that somehow?

GullRaDriel
Member #3,861
September 2003
avatar

I compiled it with gcc version 6.3.0 (MinGW.org GCC-6.3.0-1) on windows 7 x64 with latest allegro5 from git.

Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/
mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c
++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libs
tdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I'm not sure what I did, but some tinkering seemed to stabilize things.

Try the binaries for the ball bin and the intercept graph :

alpha1.zip

{"name":"611797","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/b\/ab9cd722d7d8cc77d2aa886d9a3384c0.png","w":1026,"h":801,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/b\/ab9cd722d7d8cc77d2aa886d9a3384c0"}611797

{"name":"611798","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/9\/796fc2a0ec62437603f9f296a2c7ca6a.png","w":1026,"h":801,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/9\/796fc2a0ec62437603f9f296a2c7ca6a"}611798

I let the ball bin run for like 15 minutes and none escaped, so? ???

But definitely try the intercept graph (IGraph.exe). Controls are LMB, RMB, and MMB. LMB moves the ball. RMB sets the velocity. MMB sets the acceleration. S stops the ball. P pauses and unpauses the simulation. R toggles redraw.

GullRaDriel
Member #3,861
September 2003
avatar

Got it fine too even with more than 450 little balls.

I have waited until there was no more overlapping, and none escaped.

{"name":"611803","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/a\/0a79f69212a6b11997f9d43540029d67.png","w":1303,"h":861,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/a\/0a79f69212a6b11997f9d43540029d67"}611803

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

GullRaDriel
Member #3,861
September 2003
avatar

In which order do you do things ?
You have to generate new position, check if collide, then only if not swap old and new positions. If moving it make it collide it has to stay where it is and have its velocity/whateverelse reversed according to the bounce.
The best case would be to compute the intersect point of colliding, and then move accordingly to the first non colliding position, and reverse velocity/whateverelse according to the bounce.
So ?

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: