### Author Topic: Slope Physics Tutorial  (Read 10403 times)

0 Members and 1 Guest are viewing this topic.

#### squidgetx

• Food.
• CoT Emeritus
• LV10 31337 u53r (Next: 2000)
• Posts: 1881
• Rating: +503/-17
• rawr.
##### Slope Physics Tutorial
« on: March 29, 2011, 04:44:32 pm »
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:
Code: [Select]
0->YRepeat getKey(15)ClrDrawY+1->YPxl-On(5,Y)DispGraphEnd

Wait, but that’s way too fast! Let’s try the same example, but with inflation of values by 256
Code: [Select]
0->YRepeat getKey(15)ClrDrawY+1->YPxl-On(5,Y/256)DispGraphEndSee? 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.
Code: [Select]
Degrees | Radians | Circle | Binary Degrees0       | 0       | 0      | 045      | π/4     | 1/8    | 3290      | π/2     | 1/4    | 64180     | π       | 1/2    | 128270     | 3π/2    | 3/4    | 292360     | 2π      | 1      | 256But 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:
Code: [Select]
Degrees | sin | cos | Axe sin | Axe cos 0       |  0  |  1  |     0   |   12830      | .5  | .87 |    64   |    9345      | .71 | .71 |    53   |    5390      |  1  |  0  |   128   |     0180     |  0  |  -1 |     0   | 65409270     | -1  |  0  | 65409   |     0Basically, 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?
Code: [Select]
G*sin(θ)//128*sin(θ)//128 || G*cos(θ)//128*sin(θ)//128It’s messy, but at least it works.

Part 6: Wait! How do I figure out the angle?
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….
Code: [Select]
{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….
Code: (LUT) [Select]
Data(51+64,45+64,32+64)Data(0,32,45,51)->GDB1I 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.
Code: (pixel testing loop) [Select]
.Note- make sure this runs before your sprite is drawn on screen0->{L1}^^rFor(B,0,1)For(A,0,7)If pxl-Test(X+B,Y+A-3)A-3->{B+L1}EndEndEnd{{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
As I learn more, I’ll add more; Future additions include collision with an angled surface, and flying and other random stuff. Happy programming!
« Last Edit: March 29, 2011, 05:15:53 pm by squidgetx »

#### meishe91

• Super Ninja
• LV11 Super Veteran (Next: 3000)
• Posts: 2946
• Rating: +115/-11
##### Re: Slope Physics Tutorial
« Reply #1 on: March 29, 2011, 05:07:34 pm »
Added to the tutorials page. It looks awesome.
Spoiler For Spoiler:

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

#### SirCmpwn

• Guest
##### Re: Slope Physics Tutorial
« Reply #2 on: March 29, 2011, 05:08:02 pm »
Tan(A) is equal to sin(A)/cos(A).  Do we have inverse sine/cosine?

#### squidgetx

• Food.
• CoT Emeritus
• LV10 31337 u53r (Next: 2000)
• Posts: 1881
• Rating: +503/-17
• rawr.
##### Re: Slope Physics Tutorial
« Reply #3 on: March 29, 2011, 05:09:54 pm »
Thanks Meishe
And no inverse trig routines at all, unfortunately. LUT ftw

#### Deep Toaster

• So much to do, so much time, so little motivation
• LV13 Extreme Addict (Next: 9001)
• Posts: 8215
• Rating: +758/-15
##### Re: Slope Physics Tutorial
« Reply #4 on: March 29, 2011, 06:20:02 pm »
You don't know how much this will help me for a little project of mine Now just a couple more tutorials on advance physics and I should be able to start!

#### ZippyDee

• Posts: 729
• Rating: +83/-8
• Why not zoidberg?
##### Re: Slope Physics Tutorial
« Reply #5 on: March 29, 2011, 06:21:12 pm »
You don't know how much this will help me for a little project of mine Now just a couple more tutorials on advance physics and I should be able to start!
What else do you need to know?

Pushpins 'n' stuff...

#### squidgetx

• Food.
• CoT Emeritus
• LV10 31337 u53r (Next: 2000)
• Posts: 1881
• Rating: +503/-17
• rawr.
##### Re: Slope Physics Tutorial
« Reply #6 on: March 29, 2011, 07:17:43 pm »
You don't know how much this will help me for a little project of mine Now just a couple more tutorials on advance physics and I should be able to start!
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
« Last Edit: March 29, 2011, 07:17:55 pm by squidgetx »

#### ZippyDee

• Posts: 729
• Rating: +83/-8
• Why not zoidberg?
##### Re: Slope Physics Tutorial
« Reply #7 on: March 30, 2011, 02:15:26 am »
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: March 30, 2011, 03:49:38 am by ZippyDee »

Pushpins 'n' stuff...

#### DrDnar

• LV7 Elite (Next: 700)
• Posts: 546
• Rating: +97/-1
##### Re: Slope Physics Tutorial
« Reply #8 on: March 30, 2011, 02:39:40 am »
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: March 30, 2011, 02:44:53 am by DrDnar »
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

#### Builderboy

• Physics Guru
• CoT Emeritus
• LV13 Extreme Addict (Next: 9001)
• Posts: 5673
• Rating: +613/-9
• Would you kindly?
##### Re: Slope Physics Tutorial
« Reply #9 on: March 30, 2011, 01:30:58 pm »
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 http://ourl.ca/7764

This looks like an awesome guide I especially like the drawings and charts, it really helps with making everything easy to understand 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?

#### squidgetx

• Food.
• CoT Emeritus
• LV10 31337 u53r (Next: 2000)
• Posts: 1881
• Rating: +503/-17
• rawr.
##### Re: Slope Physics Tutorial
« Reply #10 on: March 30, 2011, 03:21:34 pm »
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

#### ZippyDee

• Posts: 729
• Rating: +83/-8
• Why not zoidberg?
##### Re: Slope Physics Tutorial
« Reply #11 on: March 30, 2011, 03:27:38 pm »
I actually WAS wondering why it was going over that hill. Also, I threw together a decent atan routine in axe. I'll post it. It can almost certainly be optimized.

Pushpins 'n' stuff...

#### Deep Toaster

• So much to do, so much time, so little motivation
• LV13 Extreme Addict (Next: 9001)
• Posts: 8215
• Rating: +758/-15
##### Re: Slope Physics Tutorial
« Reply #12 on: March 30, 2011, 03:37:53 pm »
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

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 I have it planned for senior year.

#### ZippyDee

• Posts: 729
• Rating: +83/-8
• Why not zoidberg?
##### Re: Slope Physics Tutorial
« Reply #13 on: March 30, 2011, 03:44:49 pm »
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.

Pushpins 'n' stuff...

#### 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.
##### Re: Slope Physics Tutorial
« Reply #14 on: March 30, 2011, 04:09:33 pm »
Wow, that looks incredible, squidgetx!  Great job!
By any chance can you post the source to the example program there?  It's pretty cool.
If I'm wrong, please correct me!
Unfinished Projects:
 Elmgon 14% Basic Movement Demo Homescreen Game Pack 80% Basic Latest Release Cube Droid Saves the Galaxy 65% Axe Demo Detonate 70% Axe
Completed Projects:
Exodus | Midnight |Drifter | Axe Snake | Jump! | Factory Theta | Spider | Plot Drop | Papi Jump | Numb3rs | Nibbler | Boost | Duel Tile Map Editor | Homescreen Map Editor | Key Group Check | Oasis