Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 


Submitted by Lee Mazurek, posted on January 28, 2002




Image Description, by Lee Mazurek



Here is a charged particle simulator I wrote for physics class. The teacher was using some electron hockey game one day, and he made a comment about how expensive it was. I then set out to program it myself, and sell my own particle simulator cheaply. Hopefully schools will buy it.

The program uses SDL in combination with OpenGL. There is not really anything special about the graphics, and the physics are brute force unoptimized. One of the ways I cheat on the physics however is to allow the graphics card to do the interpolation of the potential field rather than doing it all myself.

It is nice to get a complete program done, as I have been programming for quite some time and have not made anything that was finished (I have a huge code base of highly optimized 3/4 finished game libraries). My project to end all projects is still pending.

The simulator also include a game mode of course where the goal is to get the particle into the goal. Try it out, it was rather amusing to see my roomates be engrossed by such a simple game.

Editor's Note: The demo is available for download here (162k)


[prev]
Image of the Day Gallery
www.flipcode.com

[next]

 
Message Center / Reader Comments: ( To Participate in the Discussion, Join the Community )
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Jan Niestadt

January 28, 2002, 06:06 AM

Very cool, I'm going to try it! I made something similar when I was in high school. Ours was really nothing special (very clunky interface) but the teacher was extremely impressed, maybe he's even using it today.. ;-)

 
Jan Niestadt

January 28, 2002, 06:10 AM

Hmm, if I start it, it opens a windows and immediately closes it again.. What could be wrong?

 
Luke Philpot

January 28, 2002, 06:30 AM

Hey, that looks neat! Keep up the good work, ok?

 
Francois Beaune

January 28, 2002, 07:02 AM

Hi ! Does not work for me either. It opens a window, then immediately closes, without even an error message on the console.

 
MK42

January 28, 2002, 07:08 AM

Hi!

This looks neat. I tried the download and it doesn't work (opens window, then closes) ... are you missing the data files?

- Marco

 
Kurt Miller

January 28, 2002, 07:33 AM

His app looks to run fine over here, so I don't think its missing any data.

 
FunkyMonk

January 28, 2002, 07:44 AM

same with me :-(

 
MK42

January 28, 2002, 07:49 AM

Hmmz, strange. OK, for debugging purposes, here's my current configuration:

AMD 1.4 GHz
512 MB DDR-RAM
GF3 64 MB (Driver 23.11)
Windows XP Pro

Maybe it's XP?

- Marco

 
Orangy Tang

January 28, 2002, 07:49 AM

This is really rather neat, looks good and the game is strangly adictive.. How is the surface done? A highly tessalated surface and gourad shading? Works well if thats what it is (although you might want to add an option to increase/decrease the amount of accuracy..)

 
opla

January 28, 2002, 07:57 AM

it opens a window and close it immediately.
I'm using a GeForce3 under Windows 2000.

GLtrace gives :

-------------------------------------------------------------------------------
GLTrace2 version 2.2, copyright 2000 by Phil Frisbie, Jr. phil@hawksoft.com
GLTrace2 comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions; see the file license.txt.
-------------------------------------------------------------------------------
OpenGL provider: E:WINNTSystem32opengl32.dll
Start time: Mon Jan 28 12:56:12 2002
-------------------------------------------------------------------------------

wglChoosePixelFormat()
wglDescribePixelFormat()
wglSetPixelFormat()
wglDescribePixelFormat()
wglGetPixelFormat()
wglDescribePixelFormat()
wglGetPixelFormat()
wglDescribePixelFormat()
wglCreateContext()
wglGetPixelFormat()
wglGetPixelFormat()
wglDescribePixelFormat()
wglMakeCurrent()
glViewport()
glMatrixMode()
glLoadIdentity()
glOrtho()
glMatrixMode()
glLoadIdentity()
glEnable()
glClearColor()
glClearDepth()
glDepthFunc()
glEnable()
glEnable()
glShadeModel()
glHint()
glGenTextures()
glBindTexture()
glTexImage2D()
glTexParameteri()
glTexParameteri()
glBindTexture()
glClear()
glLoadIdentity()
glTranslated()
wglSwapBuffers()
glClear()
glLoadIdentity()
glTranslated()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
glBlendFunc()
glBindTexture()
glColor4f()
glColor4f()
glBlendFunc()
glBindTexture()
wglSwapBuffers()
wglMakeCurrent()
wglDeleteContext()

Call Counts:
glBegin 75
glBindTexture 58
glBlendFunc 56
glClear 2
glClearColor 1
glClearDepth 1
glColor3f 95
glColor4f 56
glDepthFunc 1
glEnable 3
glEnd 75
glGenTextures 1
glHint 1
glLoadIdentity 4
glMatrixMode 2
glOrtho 1
glShadeModel 1
glTexCoord2f 1436
glTexImage2D 1
glTexParameteri 2
glTranslated 2
glVertex3f 1812
glViewport 1
wglChoosePixelFormat 1
wglCreateContext 1
wglDeleteContext 1
wglDescribePixelFormat 5
wglGetPixelFormat 4
wglMakeCurrent 2
wglSetPixelFormat 1
wglSwapBuffers 2


Closing output file

 
Kurt Miller

January 28, 2002, 07:59 AM

">>Maybe it's XP?"

Nope, I'm running XP Pro also. TNT2 gfx card;
I checked if it could be because my desktop is 16-bit while most people use 32-bit, but it works either way.

 
jurgen

January 28, 2002, 07:59 AM

Yeah,

that is a really neat application. I am going to try that one out myself.



jurgen

 
MrZed

January 28, 2002, 08:15 AM

Very impressive work and picture!
Can you say something more about your calculations? I am very interested in this because i work on a project which deals with the measurement of electrical potentials in an electrolytical trough. My physics teacher said, that calculations of it are very complex and expensive.
If somebody wants to know more about my work, he can watch out old screenshots of it here: http://www.flipcode.com/cgi-bin/msg.cgi?showThread=10-03-2001&forum=iotd&id=-1


 
=[Scarab]=

January 28, 2002, 08:53 AM

Works perfectly for me. Very fun to play indeed!
And it's a very nice project overall as well.

 
psykotic

January 28, 2002, 08:55 AM

Can you say something more about your calculations? I am very interested in this because i work on a project which deals with the measurement of electrical potentials in an electrolytical trough. My physics teacher said, that calculations of it are very complex and expensive.

If you want to find an analytic expression for the electric field based on a set of stationary point charges, you need to look into electrostatics which usually involves a lot of vector calculus. If you just need to evaluate the force acting on a test point charge at a number of points in a space, then it's a piece of cake. Since fields obey the principle of superposition, you can calculate the eletric field due to each of the point charges for a point and sum them to get the resulting field. The electric force can be obtained from Coulomb's law:

F = [(k * q) r] / |r|^3

where k = 8.99 * 10^9 N m^2 / C^2, q is the point charge and r is the difference between the position of the point charge and test point -- it is a vector pointing from the point charge to the test point. Calculate the force due to each point charge and add them together to get the resulting force at a particular point. Do this for each point and you're set.

 
Maj

January 28, 2002, 09:19 AM

Same open and close bug for me. Win2K SP2, GF2GTS, Det 23.11.

 
Wim Libaers

January 28, 2002, 09:30 AM

I don't think this is directly applicable to your case. Calculating the interaction of fields from point sources in a vacuum isn't that hard. The movement is harder, but there are numerical integrators of sufficient quality available. Working with real electrodes in an elektrolyte is a very different thing. Stirring the bath? Just diffusion? Convection? How much current/voltage? Which reactions happen? Material of electrodes? Size of container and electrodes? All of that matters. If you're not careful with the measuring electrode, that can influence the results too.

 
BLooD

January 28, 2002, 09:36 AM

Same error.

P4
GeForce3 Ti 500
WinXP

BLooD =(

 
DirtyPunk

January 28, 2002, 10:19 AM

You may wish to post things like this directly to the author in future.

As other people actually have to deal with this reading through posts.

 
RyseFtk

January 28, 2002, 10:41 AM

Same error.

XP, Radeon 8500, P3. Opens, displays something for about a frame (hard to spot but has text in it) then closes again. Am happy to run a version that logs stuff for you, if you want.

 
Buster

January 28, 2002, 11:13 AM

Hey that's pretty cool. Do ya'll remember that arcade game Reator with the cool bumpin' music? This could be made into a game similar to that!

 
337

January 28, 2002, 11:18 AM

Hello all, thanks for the positive feedback.

About which calculations it uses, psyoktic was right, it uses Coulumbs law.
The equation is really f = kq/r^2, not cubed, but thank you for the fine explanation.
To calculate the potential at each point, I used potential = kq/r. Both of these superimpose upon eachother by adding them for each charge. Those are apparently not the correct equations for moving charges, but they work fine over small intervals. After calculating the force, its just standard f=mass*accelleration, and position = position+velocity*time.
The only problem I had with this approach was that because I was not integrating the force over the entire interval, occasionally I would have inaccuracies when the interval was too large. It was particularly problematic when the distance between them approached 0. The program does 2 things to compensate, the time interval decreases when force gets too large, and the force is clamped at an unrealistic maximum.

About the potential and field drawing... as OrangyTang said so well "How is the surface done? A highly tessalated surface and gourad shading?" The potential field is just a tesselated surface with OGL color interpolation. I did not fix the tesselation interval for either the force or potential field, but I did not find it appropriate to put in the gui. If I was with it, I would put dynamic tesselation in, but I swore to myself I would stop adding features. (The problem is instead of doing my physics homework I am writing that physics app, whereas I should be doing homework).

About the not running on some people's computers, thanks for telling me. It ran on pretty much anyone's computer I tried it on, and because I am using SDL, I assumed I would have few graphics related deaths. Please download a logged distribution from http://mazurek.net/physics/logdistro.zip. Thanks opla for the glcall dump, but I am slightly confused by it, there are a lot of glcalls made by sdl, so its difficult to place exactly in the application where things went wrong.

Lastly, thanks to all those who downloaded everything, flipcode is one of the few great remaining dev sites because of the people who visit and maintain it.

 
337

January 28, 2002, 11:46 AM

Another suggestion for those whos computers it won't work on (all you people with nice graphics cards apparently), try the screensaver mode. It does not print any text to screen, maybe the text blitting function is not right, it is the only part that actually sets blending and texture ops. To do screensaver mode, run the app with -screensaver from the command line, or -screensaver in a shortcut. Thanks for your patience.

 
MK42

January 28, 2002, 11:53 AM

Tried the logged version and got:


APPLICATION LOG START
Time: Mon Jan 28 17:52:10 2002
log initialized file:C:cppelectron hockeylog.cpp line:37
initializing display file:C:cppelectron hockeyscenegraph.cpp line:81
set viewport/enabled blending, depthtest, smoothshading file:C:cppelectron hockeyscenegraph.cpp line:118
setting window caption file:C:cppelectron hockeyscenegraph.cpp line:125
pumping messages file:C:cppelectron hockeyscenegraph.cpp line:302
clearing gl window file:C:cppelectron hockeyscenegraph.cpp line:325
swapping buffers using SDL_GL_SwapBuffers file:C:cppelectron hockeyscenegraph.cpp line:366
pumping messages file:C:cppelectron hockeyscenegraph.cpp line:302
clearing gl window file:C:cppelectron hockeyscenegraph.cpp line:325
swapping buffers using SDL_GL_SwapBuffers file:C:cppelectron hockeyscenegraph.cpp line:366
shutting down display file:C:cppelectron hockeyscenegraph.cpp line:135





Also tried the screensavermode ... doesn't work! Oh, I do see something for a second ... some text and some numbers on the bottom ... it's kinda hard to spot anything else in that split-second!

- Marco

 
psykotic

January 28, 2002, 01:16 PM

The equation is really f = kq/r^2, not cubed, but thank you for the fine explanation.

Please reread my message. The magnitude of the force is what you just wrote but since the direction of the forces caused by the point charges are unlikely to be identical, you need to work in terms of vectors. Let me show you how my vector-based equation reduces to your equation when I take the magnitude of the vector.

F = [(k * q) r] / |r|^3
|F| = 1/|r|^3 * (k * q) * |r|
|F| = (k * q) / |r|^2

and not so surprisingly, this reduces to the scalar equation. I am somewhat puzzled as to how you are able to implement this program of yours using the scalar equation since you seem to be unfamiliar with the vector form...

 
Hiro Protagonist

January 28, 2002, 01:37 PM

It crashed to the desktop for me too, so I ran it under a debugger. Interestingly enough, this fixed the problem. Unfortuantely, because it fixed the problem, I was unable to debug it =)

For those of you running WinXP, go to your command prompt, change directories to the demo directory and type the following:


ntsd -gGx app.exe



This will launch the app under the debugger. It will probably work.

Good job.

 
Hiro Protagonist

January 28, 2002, 01:49 PM

This could allude to the fact that you have some sort of race condition in your code. Running under the debugger generally will slow an app down. If the problem is fixed by slowing an application down, then probably the problem is with the speed of the machine. Make sure you don't have any hardcoded timers set, etc. Check your main game loop for bad logic. The problem could be with SDL but I doubt it since I have run several demos using the API.
If you would like me to look at your source I would.

Good luck.

 
davepermen

January 28, 2002, 02:29 PM

its not some sort of crash of gl for sure, else he would not call all the gl functions to release gl properly.. its some logic in your app shutting down sdl cleanly

 
ecko_53

January 28, 2002, 02:31 PM

Opened then shut right down, here is the debug file:

APPLICATION LOG START
Time: Mon Jan 28 14:29:28 2002
log initialized file:C:cppelectron hockeylog.cpp line:37
initializing display file:C:cppelectron hockeyscenegraph.cpp line:81
set viewport/enabled blending, depthtest, smoothshading file:C:cppelectron hockeyscenegraph.cpp line:118
setting window caption file:C:cppelectron hockeyscenegraph.cpp line:125
pumping messages file:C:cppelectron hockeyscenegraph.cpp line:302
clearing gl window file:C:cppelectron hockeyscenegraph.cpp line:325
swapping buffers using SDL_GL_SwapBuffers file:C:cppelectron hockeyscenegraph.cpp line:366
pumping messages file:C:cppelectron hockeyscenegraph.cpp line:302
clearing gl window file:C:cppelectron hockeyscenegraph.cpp line:325
swapping buffers using SDL_GL_SwapBuffers file:C:cppelectron hockeyscenegraph.cpp line:366
shutting down display file:C:cppelectron hockeyscenegraph.cpp line:135

 
davepermen

January 28, 2002, 02:35 PM

screen

thats what we all can see. i guess its the start of the application.
do you close on escape or so with GetAsyncKeyState()? those functions sometimes return wrong walues and you have to precheck this way:
GetAsyncKeyState(XXX);
if(GetAsyncKeyState(XXX)) do_something_for_example_shutdown();

i dunno.. i just catched the screen during the milliseconds i could see the "black screen"

 
This thread contains 52 messages.
First Previous ( To view more messages, select a page: 0 1 ... out of 1) Next Last
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.