Author Topic: Physics Lessons  (Read 51847 times)

0 Members and 1 Guest are viewing this topic.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Physics Lessons
« on: March 13, 2010, 10:58:33 pm »
GRAVITY:

One of the most basic Physics concepts that you might want to implement in a game is gravity.  Whether it be in a platformer, a pinball game, or something else, gravity is a common need.  So to start off, we need to ask ourselves, what it gravity?  Gravity acts on all objects on earth, and pulls the objects towards the surface, we all know that.  But its not merely as simple as decreasing the players position by 1 each frame, things don't fall that way*, as they fall they get faster and faster.  This phenomenom is known as acceleration and is the concept behind gravity and all accelerating physics.

Acceleration is measured in X Meters per second per second.  That is to say, every second, your velocity will increase by X Meters per second.  Since we live in the game world, it would be more somthing like X pixels per frame per frame.  That is to say, every frame, your velocity will increase by X pixels per frame.  Lets say your position starts at 0 and your velocity starts at -3 and you are in an empty world with 1 P/F/F
acceleration (1 pixels per frame per frame).  This chart shows your position, velocity, and acceleration for a few frames

Code: [Select]
Frame Position Velocity Acceleration
0 0 -3 1
1 -3 -2 1
2 -5 -1 1
3 -6 0 1
4 -6 1 1
5 -5 2 1
6 -3 3 1
7 0 4 1
8 4 5 1
9 9 6 1
10 15 7 1

The code for this would look something like

Code: [Select]
0->X
-3->V
1->A

While 1
Output(1,1,X)
X+V->X
V+A->V
End

Notice that even when the velocity is positive, the position can still be negative, and vica versa.  But you can see that the position starts off moving downwards, stops at -6, then starts moving upwards again.  Acceleration stays constant througout the entire simulation.  If you want to get really technical, you can go into how velocity is the derivative of position blah blah blah, but you dont need to know that in a game ;)

HOW TO USE IT:

Implementing acceleration is simple, especialy if your acceleration is constant.  You only need a single variable for your position and one for your velocity.  If you want changing acceleration you could add a variable for that too.

Also note that X velocity is INDEPENDANT of Y velocity.  That is to say, if your character has a velocity in the X direction, and has gravity affecting it, since gravity is only in the Y direction (unless your game is uber cool) the Y acceleration will only affect the Y velocity will only affect the Y position.  Try this example program for size:

Code: [Select]
ZStandard
ZInteger
AxesOff
Clrdraw
0->X
0->Z
0->A
0->B

While 1
PtOff(X,Z,2
X+A->X
Z+B->Z
PtOn(X,Z,2
getKey->K
A+(K=26)-(K=24)->A
B+(K=25)-(K=34)->B
End

Its a fun example of the power of acceleration, see if you can add gravity to it!  You might have some interesting results.

WHAT TO WATCH OUT FOR:

Acceleration is not always applied, for example when you are standing on a platform, both your velocity and your acceleration are 0.  More on this will be covered in the next section 'Collisions'
« Last Edit: November 18, 2012, 04:12:40 am by Builderboy »

_player1537

  • Guest
Re: Physics Lessons
« Reply #1 on: March 13, 2010, 11:05:52 pm »
Builderboy,  thank you.  I look forward to the next section  ;D

Offline Radical Pi

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1143
  • Rating: +5/-2
    • View Profile
    • RealityRevolution
Re: Physics Lessons
« Reply #2 on: March 13, 2010, 11:18:39 pm »
Good tutorial! I can't wait for what you have to say on collisions with acceleration; I've always wondered how to prevent accelerating through something.
One of these days I'll get a sig I'm really proud of.

Offline trevmeister66

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1009
  • Rating: +14/-5
    • View Profile
Re: Physics Lessons
« Reply #3 on: March 13, 2010, 11:25:43 pm »
Wow, that is amazing. *trevmeister66 starts messing around with his new found knowledge
Projects:    nameless RPG: 1.0%  |  Reverse Snake v1.5: 100%  |  Secret Project: 5%  |  DUNGEON: 70%

My MW2 Blog <-- Please visit :)

Offline {AP}

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 981
  • Rating: +74/-3
  • I am Webmaster!
    • View Profile
    • Removed From Game
Re: Physics Lessons
« Reply #4 on: March 13, 2010, 11:27:25 pm »
All this does is remind me of how I never paid enough attention in Physics class. xP
Nice to see my wasted education in code form though. I sometimes have a hard time taking real life concepts and converting them to code effectively.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Physics Lessons
« Reply #5 on: March 13, 2010, 11:29:56 pm »
This is interesting. It might be more suitable in animations or very simple games, though, since extreme gravity calculations can take a while in TI-BASIC. Otherwise, maybe Axe Parser. Maybe it could also help other kind of programmers too. Maybe it could help for a Sonic game too :P (since the one on UTI seems to never be going anywhere...)

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Physics Lessons
« Reply #6 on: March 14, 2010, 12:40:13 am »
Thanks, that is pretty sweet. I'm gonna have to go over it more though since I still don't quite understand. What would you even use though for your gravity acceleration? I mean gravity in real life is 9.81... m/s2 (and 32... ft/s2 but I don't think that would work very well on the graph/home screen. But I don't know.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Physics Lessons
« Reply #7 on: March 14, 2010, 12:56:00 am »
I sometimes have a hard time taking real life concepts and converting them to code effectively.
Yeah that can be a very hard challenge.  Especially when talking about really complicated things like rebound angles and the like D:

This is interesting. It might be more suitable in animations or very simple games, though, since extreme gravity calculations can take a while in TI-BASIC. Otherwise, maybe Axe Parser. Maybe it could also help other kind of programmers too. Maybe it could help for a Sonic game too :P (since the one on UTI seems to never be going anywhere...)
Yeah, one thing thats hard about implementing physics well is that it often times takes a lot of processing time.

What would you even use though for your gravity acceleration? I mean gravity in real life is 9.81... m/s2 (and 32... ft/s2 but I don't think that would work very well on the graph/home screen. But I don't know.
Well in Portal i believe i used 1/64 pxl per frame per frame, but it depends heavily on your frame-rate.  The best way to find a good acceleration is just to run some tests.  As a general rule you don't want your player moving too fast because that makes collision very difficult.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Physics Lessons
« Reply #8 on: March 14, 2010, 01:07:09 am »
Hmmm, ok. That makes sense. Thanks.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline AaroneusTheGreat

  • Moderator
  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 287
  • Rating: +26/-1
    • View Profile
Re: Physics Lessons
« Reply #9 on: March 14, 2010, 03:12:52 am »
Quote
Well in Portal i believe i used 1/64 pxl per frame per frame, but it depends heavily on your frame-rate.  The best way to find a good acceleration is just to run some tests.  As a general rule you don't want your player moving too fast because that makes collision very difficult.

Sometimes you can get around the collision detection issue with fast motion by pre-calculating the next position, and calculate where it would actually collide if it were going point by point, then simply change the position of the object to that collision point, thereby bypassing any graphical glitches you might have in your detection. It can be a pretty simple section of code within your collision detection subroutine.

What I would really like to know is how you solved the problem of implementing angular momentum in addition to gravity when you come out of a portal from another direction. I was having a time trying to figure that out.

EDIT:

I read back over some of the tutorial, it says you're doing a section on collision detection, I'm interested to see what methods you have to implement this and what kind of uses you're going to cover. It can be a really tricky thing, especially in an environment like Portal, any 3D environment, or any environment involving uneven surfaces or curved objects. I'm sure you've got a few tricks up your sleeve from what I've seen you do so far. :D
« Last Edit: March 14, 2010, 03:17:04 am by AaroneusTheGreat »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Physics Lessons
« Reply #10 on: March 14, 2010, 03:29:49 am »
As a general rule you don't want your player moving too fast because that makes collision very difficult.
True, else stuff like this happens :P

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Physics Lessons
« Reply #11 on: March 14, 2010, 10:02:03 am »
Very nice topic.  ;)

Really easy to follow and learn. Collision is a must for next update. :P
Hobbing in calculator projects.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Physics Lessons
« Reply #12 on: March 14, 2010, 11:17:16 am »
That's really awesome Builderboy!  Thanks, and I can't wait for collisions!   
 *ZTrumpet thinks about making some sort of physics game with Axe with this new knowledge... ;D

Offline jsj795

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1105
  • Rating: +84/-3
    • View Profile
Re: Physics Lessons
« Reply #13 on: March 14, 2010, 11:19:55 am »
One of my first games actually involved the gravity, like the helicopter... But it was super slow, and I think it could've been a lot faster if I used Builderboy's code!

You taught me that Physics class can definitely be applied in some area :)


Spoiler For funny life mathematics:
1. ROMANCE MATHEMATICS
Smart man + smart woman = romance
Smart man + dumb woman = affair
Dumb man + smart woman = marriage
Dumb man + dumb woman = pregnancy
2. OFFICE ARITHMETIC
Smart boss + smart employee = profit
Smart boss + dumb employee = production
Dumb boss + smart employee = promotion
Dumb boss + dumb employee = overtime
3. SHOPPING MATH
A man will pay $2 for a $1 item he needs.
A woman will pay $1 for a $2 item that she doesn't need.
4. GENERAL EQUATIONS & STATISTICS
A woman worries about the future until she gets a husband.
A man never worries about the future until he gets a wife.
A successful man is one who makes more money than his wife can spend.
A successful woman is one who can find such a man.
5. HAPPINESS
To be happy with a man, you must understand him a lot and love him a little.
To be happy with a woman, you must love her a lot and not try to understand her at all.
6. LONGEVITY
Married men live longer than single men do, but married men are a lot more willing to die.
7. PROPENSITY TO CHANGE
A woman marries a man expecting he will change, but he doesn't.
A man marries a woman expecting that she won't change, and she does.
8. DISCUSSION TECHNIQUE
A woman has the last word in any argument.
Anything a man says after that is the beginning of a new argument.

Girls = Time * Money (Girls are a combination of time and money)
Time = Money (Time is money)
Girls = Money squared (So, girls are money squared)
Money = sqrt(Evil) (Money is also the root of all evil)
Girls = sqrt(Evil) squared (So, girls are the root of all evil squared)
Girls = Evil (Thus, girls are evil)
*Girls=Evil credit goes to Compynerd255*

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Physics Lessons
« Reply #14 on: March 14, 2010, 08:23:01 pm »
btw, since there are people who are visual, it might be a good idea to have some eye candy of the examples in action. Just a suggestion for the tutorial