Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
22 May, 2013, 21:03:45 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1] 2   Go Down
  Print  
Author Topic: Slope Physics Tutorial -  (Read 2265 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 18:24:17
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1831


Topic starter
Total Post Ratings: +477

View Profile
« on: 29 March, 2011, 22:44:32 »
+12

Slope Physics Tutorial
Well basically I hate slope physics. So in order to help myself figure out what the hell is going on here, I’m going to write a tutorial! Makes perfect sense. Let’s get started. First, a little background. If you are relatively well-versed with physics in an Axe environment, you can skip this part.

Part 1: Gravity.
First off let’s introduce the concept of gravity. Universal force that acts downwards. All objects fall at the same rate; or rather they all accelerate at the same rate. According to Newton, Force=mass*acceleration. More force=more acceleration. In real life, the acceleration of gravity is about 9.8 meters/second/second. Which makes sense: meters/second is a measure of speed, so meters/second/second is a measure of acceleration.
In our game world, we’ll set gravity to whatever quantity we want.

Part 2: Integer Math
Well, we’ve got no decimals. That is certainly a problem. Well, to simulate decimals, we can inflate all our values by a certain number. The standard is 256, although theoretically you could use any value. How does this work?...Let’s go with an example. Let’s do a simulation of an object being affected by gravity!
We want the y position to accelerate every time, so let’s set up the following code with gravity being 1 pixel:

1
2
3
4
5
6
7
8
0->Y
Repeat getKey(15)
ClrDraw
Y+1->Y
Pxl-On(5,Y)
DispGraph
End


Wait, but that’s way too fast! Let’s try the same example, but with inflation of values by 256

1
2
3
4
5
6
7
8
0->Y
Repeat getKey(15)
ClrDraw
Y+1->Y
Pxl-On(5,Y/256)
DispGraph
End
See? Each time you add 1, it’s like you’re adding 1/256th of a pixel….Anyway, let’s move on to…

Part 3: The Slope

So now, let’s draw a diagram of all the forces acting on an object on a slope. Forces are vectors; in other words, they are numbers with direction. And they can be split into different components.

 Remember, sin is opposite/hypotenuse, and cos is adjacent/hypotenuse.

Now, we can see that the force dragging an object down a hill is equal to the force of gravity times the sine of the angle. Remember that F=ma, so the force of gravity is mg. In games, unless you’re planning to change the mass, I would set it to 1 for simplicity’s sake. So we get F=g. Easy enough. The force that pulls you down the hill is equivalent to gsinθ, with the direction of θ. The Fn up there is called the normal force. Basically, it just cancels out the y-component of gravity, which is why we don't talk about them much.

Notating Vectors
We can either express vectors in component, or in magnitude+direction formats. In games, it is more common to store vectors as components, so let’s start with that.

The problem is, how do you increment your objects’ velocity diagonally? Well, let’s move on to some more trig….

And there you have it. Every frame, increment x velocity by gsinθcosθ and y velocity by gsinθsinθ
But wait…Is it really that simple?

Part 5: Applying this concept in an Axe environment
Here is the tricky part. Axe sets us up with a great coding environment designed on speed and efficiency. Unfortunately, this means no decimals. Which is a HUGE problem.

Axe’s sine and cosine functions are a little bit different from your traditional sine and cosine functions. First of all, they use a system of binary degrees Basically, one period or revolution is 256. You can use the conversion factor of 32/45 to convert from traditional degrees to binary degrees, or a factor of 128/π to convert from radians.

1
2
3
4
5
6
7
Degrees | Radians | Circle | Binary Degrees
0       | 0       | 0      | 0
45      | π/4     | 1/8    | 32
90      | π/2     | 1/4    | 64
180     | π       | 1/2    | 128
270     | 3π/2    | 3/4    | 292
360     | 2π      | 1      | 256
But that’s not the end of our problems. Since there are no decimals, Axe’s sine and cosine functions have an amplitude of -127 to +128 (or something like that, I don’t remember the exact limits). Take a look:

1
2
3
4
5
6
7
Degrees | sin | cos | Axe sin | Axe cos
0       |  0  |  1  |     0   |   128
30      | .5  | .87 |    64   |    93
45      | .71 | .71 |    53   |    53
90      |  1  |  0  |   128   |     0
180     |  0  |  -1 |     0   | 65409
270     | -1  |  0  | 65409   |     0
Basically, the decimals that the traditional sine and cosine values output are multiplied by 256, rounded off, and then have the value 128 subtracted from them.
So how do we apply this in Axe?

1
2
G*sin(θ)//128*sin(θ)//128 || G*cos(θ)//128*sin(θ)//128
It’s messy, but at least it works.

Part 6: Wait! How do I figure out the angle?Huh?
Ah, I thought you’d never ask.
Well, there are a few ways to do this. In traditional math and physics, we have this handy tool called the inverse tangent function. Unfortunately, we don’t get that in axe so....

Method 1: Tilemap.
Yay! Everybody loves tilemapping. Fast, easy, etc. Basically, all you have to do here is give each tile an angle associated with it. Then, if you detect that your player is *on* the tile, apply the slope physics. I would use pixel testing for collision here.
How do you figure out what tile you’re standing on? Well first, make sure you’re standing on something….If not, apply gravity. Otherwise….

1
{Y/8+8*(map width)+(X/8)+(pointer to map location)}
should give you the tile you are standing on

Method 2: Pixel based method
This method is a bit more complex.  Basically, we shall look at the two pixels directly below us to determine the angle of the slope.

(The numbers in parentheses are binary degrees) Then, we’ll store this information in a lookup table (LUT)  since we don’t get a tan^-1 function….

1
2
Data(51+64,45+64,32+64)
Data(0,32,45,51)->GDB1
I believe that this method works because the lines on the screen are all pixelated anyway. The total slope physics generated will all average each other out.
Note: This method can be easily adapted to memory reads instead of pixel tests if for some reason you have pixel heights stored in memory. Also, it may be easier for some to store the precomputed sin and cos values instead of the angle, for possible increased accuracy or speed.

1
2
3
4
5
6
7
8
9
10
11
12
.Note- make sure this runs before your sprite is drawn on screen
0->{L1}^^r
For(B,0,1)
For(A,0,7)
If pxl-Test(X+B,Y+A-3)
A-3->{B+L1}
End
End
End
{{L1}-{L1+1}+GDB1}
.^^this returns the angle value

Part 7: Multiple or Dynamic Slopes
All right, now we know how to deal with one slope. But what about slopes that change direction and stuff? Here is the time to use the other notation for vectors: magnitude-angle.

Basically, it’s relatively straightforward. Remember how the acceleration on the ball on the slope was Gsin θ? Well, changing the velocity with magnitude-angle notation is *almost* as simple as just adding gsinθ to the speed (S). Then, you have to store theta somewhere as the angle. To retrieve the x and y velocities, we can break the speed back into components: X speed=Scos θ and the Y speed= Ssin θ

However, one tricky part is that the acceleration angle and the velocity angles are not the same. Do not rely on using the same angle for both of them, as that will turn out…not so pretty. Here’s an example of what it might look like…

Correcting this problem, we get this


(Credit to Runer112 on the concept on how to make this hilly terrain)

…And that’s all I have for now Undecided
As I learn more, I’ll add more; Future additions include collision with an angled surface, and flying and other random stuff. Happy programming! Smiley
« Last Edit: 29 March, 2011, 23:15:53 by squidgetx » Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
meishe91
Super Ninja
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 02 May, 2013, 23:54:14
Date Registered: 05 March, 2010, 05:39:48
Posts: 2965


Total Post Ratings: +102

View Profile WWW
« Reply #1 on: 29 March, 2011, 23:07:34 »
0

Added to the tutorials page. It looks awesome.
Logged





For the 51st time, that is not my card! (Magic Joke)
SirCmpwn
Guest
« Reply #2 on: 29 March, 2011, 23:08:02 »
0

Tan(A) is equal to sin(A)/cos(A).  Do we have inverse sine/cosine?
Logged
squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 18:24:17
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1831


Topic starter
Total Post Ratings: +477

View Profile
« Reply #3 on: 29 March, 2011, 23:09:54 »
0

Thanks Meishe
And no inverse trig routines at all, unfortunately. LUT ftw
Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
Deep Thought
So much to do, so much time, so little motivation
Administrator
LV13 Extreme Addict (Next: 9001)
*
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 19:18:47
Date Registered: 19 May, 2009, 08:00:00
Location: The Universe
Posts: 7813


Total Post Ratings: +706

View Profile WWW
« Reply #4 on: 30 March, 2011, 00:20:02 »
0

You don't know how much this will help me for a little project of mine shocked Now just a couple more tutorials on advance physics and I should be able to start! Cheesy
Logged




ZippyDee
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 12 May, 2013, 10:03:36
Date Registered: 21 March, 2011, 03:15:07
Location: Yes.
Posts: 704


Total Post Ratings: +73

View Profile
« Reply #5 on: 30 March, 2011, 00:21:12 »
0

You don't know how much this will help me for a little project of mine shocked Now just a couple more tutorials on advance physics and I should be able to start! Cheesy
What else do you need to know?
Logged

There's something about Tuesday...


Pushpins 'n' stuff...

squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 18:24:17
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1831


Topic starter
Total Post Ratings: +477

View Profile
« Reply #6 on: 30 March, 2011, 01:17:43 »
0

You don't know how much this will help me for a little project of mine shocked Now just a couple more tutorials on advance physics and I should be able to start! Cheesy
What else do you need to know?

I'm curious as well....my physics class is done with mechanics for now. What would you like to know about? Collisions? And nice to hear it was helpful Cheesy
« Last Edit: 30 March, 2011, 01:17:55 by squidgetx » Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
ZippyDee
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 12 May, 2013, 10:03:36
Date Registered: 21 March, 2011, 03:15:07
Location: Yes.
Posts: 704


Total Post Ratings: +73

View Profile
« Reply #7 on: 30 March, 2011, 08:15:26 »
0

You know, a way to do simple sidescrolling terrain is to have a list of y coordinates at evenly spaced x coordinates and then just draw lines between the points. However, unless you use pixel-based collision there, you'd need an arctan function in order to get the angles. To solve that issue, you can instead store an initial y coordinate followed by a list of angles and generate a set of corresponding y values based on those angles.

Maybe I'll throw together a simple atan LUT and stick it in a subroutine. Shouldn't be too hard.
« Last Edit: 30 March, 2011, 09:49:38 by ZippyDee » Logged

There's something about Tuesday...


Pushpins 'n' stuff...

DrDnar
LV6 Super Member (Next: 500)
******
Offline Offline

Last Login: Today at 07:38:08
Date Registered: 29 October, 2010, 00:08:46
Posts: 460

Total Post Ratings: +76

View Profile
« Reply #8 on: 30 March, 2011, 08:39:40 »
0

Sonic Retro has a complete guide on how to duplicate the physics of the Genesis/Megadrive Sonic games. Sonic has several features that were uncommon at the time, such as curvy slopes and the ability to control your jump height. The latter feature is implemented by having Sonic continue to accelerate after leaving the ground, up to a limit.
« Last Edit: 30 March, 2011, 08:44:53 by DrDnar » Logged

"The tools which would teach men their own use would be beyond price."—The Republic
Builderboy
Physics Guru
LV13 Extreme Addict (Next: 9001)
*************
Online Online

Gender: Male
Last Login: Today at 21:00:53
Date Registered: 20 April, 2009, 00:28:53
Location: Ravenholm
Posts: 5642


Total Post Ratings: +589

View Profile
« Reply #9 on: 30 March, 2011, 19:30:58 »
0

Sonic Retro has a complete guide on how to duplicate the physics of the Genesis/Megadrive Sonic games. Sonic has several features that were uncommon at the time, such as curvy slopes and the ability to control your jump height. The latter feature is implemented by having Sonic continue to accelerate after leaving the ground, up to a limit.
I've got that covered Wink http://www.omnimaga.org/index.php?topic=5167.0

This looks like an awesome guide Cheesy I especially like the drawings and charts, it really helps with making everything easy to understand Smiley I had a question about the final program though, do you use regular X Y coordinates for the ball, and simply increment them by the velocity and thats it?  Or is there something extra going on where it is moved vertically until it is right on the slope?
Logged

squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 18:24:17
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1831


Topic starter
Total Post Ratings: +477

View Profile
« Reply #10 on: 30 March, 2011, 21:21:34 »
0

Well, I increment X by the calculated velocity every frame, but the Y position is simply changed every frame to the height of the slope. Theoretically the y position *should* change by that much anyway, but I made it act that way for speed and accuracy.

And if anyone is wondering how I got over that hill, it's because I increased gravity while on the left slope Tongue
Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
ZippyDee
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 12 May, 2013, 10:03:36
Date Registered: 21 March, 2011, 03:15:07
Location: Yes.
Posts: 704


Total Post Ratings: +73

View Profile
« Reply #11 on: 30 March, 2011, 21:27:38 »
0

I actually WAS wondering why it was going over that hill. Tongue Also, I threw together a decent atan routine in axe. I'll post it. It can almost certainly be optimized.
Logged

There's something about Tuesday...


Pushpins 'n' stuff...

Deep Thought
So much to do, so much time, so little motivation
Administrator
LV13 Extreme Addict (Next: 9001)
*
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 19:18:47
Date Registered: 19 May, 2009, 08:00:00
Location: The Universe
Posts: 7813


Total Post Ratings: +706

View Profile WWW
« Reply #12 on: 30 March, 2011, 21:37:53 »
0

Quote from: squidgetx
I\'m curious as well....my physics class is done with mechanics for now. What would you like to know about? Collisions? And nice to hear it was helpful Grin

Basically, I need to know how to make a square (instead of a circle in your example) slide along a curved surface (turning the appropriate amount), with momentum and friction.

I really need to take Physics shocked I have it planned for senior year.
Logged




ZippyDee
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 12 May, 2013, 10:03:36
Date Registered: 21 March, 2011, 03:15:07
Location: Yes.
Posts: 704


Total Post Ratings: +73

View Profile
« Reply #13 on: 30 March, 2011, 21:44:49 »
0

Can you explain that a bit more? I think if I can understand exactly what you're trying to do, I may be able to help.
Logged

There's something about Tuesday...


Pushpins 'n' stuff...

ztrumpet
The Rarely Active One
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Today at 03:10:30
Date Registered: 08 November, 2009, 21:10:12
Location: Michigan
Posts: 5687


Total Post Ratings: +360

View Profile
« Reply #14 on: 30 March, 2011, 22:09:33 »
0

Wow, that looks incredible, squidgetx!  Great job! Grin
By any chance can you post the source to the example program there?  It's pretty cool. Cheesy
Logged

Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.809 seconds with 30 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.