Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Potential energy! (lots of images)

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 8th Apr 2012 09:26 Edited at: 8th Apr 2012 09:28
I was reading about magnetism and it was getting really hard to understand without drawing it out myself. Unfortunately this isn't about magnetism yet, but it IS about potential energy!

Basically, a potential is a field of conservative forces - so if a mass is thrown while subjected to a potential, the value of it's kinetic energy plus its potential energy will remain the same (eg mechanical energy is conserved).

Two great examples of conservative forces are gravity and electrostatics.

The force of gravity is G*m*M*r^(-2). To get the potential energy at some distance r, we integrate with respect to r, giving the potential energy, U=-G*m*M*r^(-1). The potential assumes the object being attracted has a mass of one, so we graph u=-G*M/r. That curve is called the potential. The potential of an object on earth's surface is given by u=g*h. The potential for electrostatics is in the form u=-G*M/r, but it allows for M to be negative.

Plotting this is easy. You have your potential function which is a function of x and y. From x and y (and the position of the mass) you can determine r and plot the function. If you add two masses (so your equation is u=-G*M1/r1-G*M2/r2) you get something like this:


(I take the sine/cosine of u to get pretty colors)

There is however another useful way to visualize potentials: using field lines and equipotentials.

Equipotentials are easy, they're all values of x and y such that u is constant. For the above image, the equipotentials look like this:


Field lines are the opposite. They're the lines going along the path of MAXIMUM change in potential energy. They're also perpendicular to the equipotentials. They look like this:

aand both together:



For fun I reversed the value of M on the larger mass to see what it would look like:

hmm... needs some adjustments:


...Okay, so my program isn't a great artist, but the tech is cool!


Anyways, here's the source code:




For those of you interested in how it works, you need a bit of calculus. The key term here is GRADIENT. The gradient of u consists of the vector of both partial derivatives of u. so the gradient=(du/dx,du/dy) (partial derivatives). It turns out that this vector is in the direction of the maximum rise in potential. From that you can find the force of the potential (-gradient) and the perpendicular values (gradient rotated by 90 degrees).

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 9th Apr 2012 14:44
I don't know whether I'm in math or geography class.


Good work though.

"everyone forgets a semi-colon sometimes." - Phaelax
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Apr 2012 13:49
Nicely done fuzzy. This actually falls in line with some stuff I wanted to add into my asteroids game.

"You're not going crazy. You're going sane in a crazy world!" ~Tick

Login to post a reply

Server time is: 2024-03-29 10:42:30
Your offset time is: 2024-03-29 10:42:30