Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 132 users online: 10 members, 96 guests and 26 bots. Members: Ashbad, cheapie, DHerls, HOMER-16, ordelore, tifreak8x. Bots: Spinn3r (1), MSN/Bing (1), Magpie Crawler (3), VoilaBot (1), Googlebot (20).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
|
| Author |
Message |
|
SirCmpwn

Coding Knight

Joined: 06 Feb 2010 Posts: 1477 Location: Colorado Springs
|
Posted: 28 Mar 2010 11:54:48 am Post subject: Physics |
|
|
Physics are a very hard thing to pull off in a calculator program, but if you can get realistic physics to work, you will have a very impressive program. This is not to out-perform BuilderBoy's wonderful post on Omnimaga, but to clarify some points he may have made.
Inertia, the key behind how realistic movement is done, is represented as the previous motion. For example, if the object moved 5 pixels in the last frame, the inertia is 5. Inertia is what makes objects keep going. Programatically speaking, you need a single variable to store inertia for each axis (X and Y).
In my opinion, the best way to implement physics is by storing the position in one place, the inertia in one place, and the requested change in one place. Let me explain how this works. The requested change is the amount of force to apply to the object each frame. This means that if the user presses the right arrow, you would supply a reqested change of 1, 0, to move it right. The physics engine, or whatever is updating the location of objects, takes the requested change and adds the inertia (the previous motion) to it. It stores the resulting value back into the inertia. Then, it adds this value to the position, applying the motion.
Now, with this system, an object will go on forever in one direction, which is not good. This is where friction comes into play. Friction is the force that slows objects down. When two objects rub against each other, kinetic energy (movement) is changed to heat energy and the objects slow down. However, if you throw a paper airplane in the air, it eventually slows down as well. This is because of liquid friction, or the friction between objects and a liquid, such as water or the air. The easiest way to implement this programatically is to gradually lose inertia.
The last thing to implementing realistic motion is gravity. Once you have everything else in place, gravity is easy to add. You simply need to subtract one from the Y portion of the requested change.
So all of this may be a little confusing, so here is some psuedo-code:
Code: Start:
0->x
0-Y
0->I ; X inertia
0->J ; Y inertia
0->V ; X requested change
0->W ; Y requested change
Loop:
Draw object
if left is pressed, store -1 to V
if right is pressed, store 1 to V
if up is pressed, store -1 to W
if down is pressed, store 1 to W
if object is not colliding with other object
V+I->V
X+I->X
if object is on ground
I*0.5->I ; if it is on the ground, more friction
else
I*0.75->I ; If it is in the air, less friction
else
0->I ; stop it if it hit another object
if object is not on the ground
J+W-1->J ; Requested change plus gravity
Y+J->Y
J *0.75->Y
else
0->J
goto Loop
This can be improved to support more of Newton's laws of motion, which I will post at a later time. |
|
| Back to top |
|
|
Jacknjellify

Newbie

Joined: 28 Aug 2010 Posts: 2
|
Posted: 28 Aug 2010 07:47:41 pm Post subject: Thoughts on "Physics". Get it? Eee! |
|
|
Very cool!
However, I think that velocity would be a more accurate term than inertia, as inertia is resistance to change in motion.
Here's my attempt, regarding balls because they are rotationally isopachous (so we can forget rotation), disregarding collision with other balls, all in partial pseudocode:
Code:
Lbl I ; Init
(Xmin+Xmax)/2->X
(Ymin+Ymax)/2->Y
5->R ; Radius
0->I ; X velocity
0->J ; Y velocity
Lbl L ; Loop
I*0.9->I ; Friction
J*0.9->J
If LEFT is pressed
I-1->I
If RIGHT is pressed
I+1->I
If UP is pressed
J+1->J
If DOWN is pressed
J-1->J
J+2->J ; Gravity
X+I->X ; Apply velocity
Y+J->Y
If Y<Ymin+R
Then
Ymin+R->Y
abs(J)*0.7->J
End
If X<Xmin+R
Then
Xmin+R->X
abs(I)*0.7->I
End
If X>Xmax-R
Then
Xmax-R->X
-abs(I)*0.7->I
End
Draw the round thing
Goto Loop
This can be improved to support more of Newton's laws of motion, which you will post at a later time. |
|
| Back to top |
|
|
calcdude84se
Power User

Joined: 14 Jun 2010 Posts: 408
|
Posted: 28 Aug 2010 08:17:55 pm Post subject: |
|
|
Aye, and moving 5 pixels per frame, and staying that way, is resisting a change in motion, is it not?
Good job, I must say. Can't wait for more. (I'll read the code later ) _________________ People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster.
-Adam Osborne |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55751 Location: Earth, Sol, Milky Way
|
Posted: 28 Aug 2010 10:10:04 pm Post subject: |
|
|
I can't believe that I never noticed this post of SirCmpwn's before. I'm definitely going to need to look at that pseudocode more carefully, as calcdude said. _________________
 |
|
| Back to top |
|
|
Kllrnohj

PH34R |\/|3

Joined: 24 May 2005 Posts: 8189
|
Posted: 29 Aug 2010 12:41:51 am Post subject: |
|
|
The gravity part is messed up. You have up and down reversed (as in, up causes you to go down, and down goes up).
Also, you can't force yourself to go down faster, nor can you "jump" in midair (and in that pseudocode, you can *only* jump when you are in the air - wtf?)
But really, the actual formulas for gravity and friction are fairly simple - just implement real physics for those two. _________________ There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup) |
|
| Back to top |
|
|
Jacknjellify

Newbie

Joined: 28 Aug 2010 Posts: 2
|
Posted: 30 Aug 2010 08:34:29 pm Post subject: |
|
|
Whoops. I am not used to the Cartesian system in which Y points up instead of down.
What you're referring to as "jumping" was not meant to be jumping. It was supposed to be floating, as if a fan blew it upward.
Take this years-old Flash thing I made for example. |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55751 Location: Earth, Sol, Milky Way
|
Posted: 30 Aug 2010 08:38:25 pm Post subject: |
|
|
| Jacknjellify wrote: | Whoops. I am not used to the Cartesian system in which Y points up instead of down.
What you're referring to as "jumping" was not meant to be jumping. It was supposed to be floating, as if a fan blew it upward.
Take this years-old Flash thing I made for example. | That's actually pretty impressive; I enjoyed playing with it for two or three minutes. I think I might have found one small math glitch, but I couldn't replicate it, so overall, cheers and well done! _________________
 |
|
| Back to top |
|
|
|
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
» Go to Registration page
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.029271 seconds.
|