#### Archived

This topic is now archived and is closed to further replies.

# Modelling a slope in a 2D sidescroller game

This topic is 5404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there! I´m working on a 2D sidescroller game and I´m trying to implement a method that I´ve seen in most games which consists on slowing the player´s speed down when climbing a slope and speeding it up when descending it. I´ve though a way to solve it and I´d like to receive some feedback about it: * Store inside the level ".dat" file the angle in degrees (theta) of the slope. During the game (pseudocode): if(The player is standing on a slope) { fPlayerSpeedX = fPlayerSpeed * cos(theta in radians); fPlayerSpeedY = fPlayerSpeed * sin(theta in radians); if(the player is descending the slope) { fPlayerSpeedX = fPlayerSpeedX * SOME_CONSTANT; fPlayerSpeedY = fPlayerSpeedY * SOME_CONSTANT; } } fDeltaX = fPlayerSpeedX * dwDiffTime; fDeltaY = fPlayerSpeedY * dwDiffTime; // Move the player fDeltaX and fDeltaY pixels in the X and // Y axis, respectively. The idea behind this code is very simple: if the player is standing on a slope, his speed in both the X and Y axis is calculated. If he is descending it, both speeds are multiplied by SOME_CONSTANT (which is greater than 1) and sin(theta in radians) to achieve that effect of incrementing the speed. I´m sure there are better ways to solve this problem, that´s why I´m posting my method so I can hear your feedback. All your comments will be appreciated! Thanks in advance for your input! --Ignacio Liverotti iliverotti@hotmail.com

##### Share on other sites
The way most games do it, it falls out of the physics code. There is some acceleration downwards. If there is no ground at the current level in the direction the player is moving, then part of that acceleration will be taken into account by the physics integrator, until you reach ground contact again, which builds up kinetic energy.

##### Share on other sites
AP, thanks for your reply. It seems that the solution is much more complex than what I´ve thought. Can you tell me where can I find the equations to model that effect? I mean, I know that the answer is "In a Physics textbook", but under which topic?

As a side note: What do you mean by physics integrator?

--Ignacio Liverotti
iliverotti@hotmail.com

##### Share on other sites
Physics integrator :

The simplest is to use an Euler integrator, it's not very stable but it should be suitable for your game. Sum the forces that are acting on your objet, you get the acceleration using the relation F=ma. You multiply your dwDiffTime by the acceleration (change of velocity is the acceleration multiplied by dt -> dwDiffTime) : dv = a*dwDiffTime.

You add this dv to your velocity (the velocity at the previous frame) : v += dv. Do the same with this new speed (change of position is the velocity multiplied by dt) : dr = v*dwDiffTime. Add the delta of position to the previous position : r += dr. And voila, you got your new position !

a,dv,v,dr,r can be vectors with 2 or more components. You must keep intact the value of r and v between 2 frames as you are using them in the next "physics update". This is somehow what you were doing in your code but you could keep the y speed component at 0 when descending the slope. Your player is walking straight forward and the gravity does the rest on the Y axis. You just need to add the acceleration and implement wind, gravity, friction,...whatever forces you'd like to simulate.

[edited by - bucheron on September 1, 2003 6:27:56 PM]

[edited by - bucheron on September 1, 2003 6:29:43 PM]

• 36
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631359
• Total Posts
2999539
×