ID:2170644
 
(See the best response by Kaiochao.)
Problem description:
How would i go about making a gravity system for a side scroller type game i
tried using just y++ and y-- but that wasn't the feel that i wanted i was
using Forum accounts system the first time and i liked the feel of his gravity
system but how would i go about starting.

i'm not asking for a full code just maybe a push of what i have to use and how to use it.


In my opinion better way is use pixel_z but im not sure....
This demo is good exemle how it's work.

Kumorii wrote:
Don't forget to check BYOND's resource library next time!
Best response
Gravity in platformers is basically a constant downward acceleration.

Acceleration is basically a change in velocity that happens over time.

Velocity is basically a change in position that happens over time.

Position (of a movable atom) can be changed by either setting the positional variables or calling Move().

Setting positional variables (e.g. y++ or loc = ...) is discouraged, as it doesn't call any procs that expect to be called whenever movement occurs, such as the and Entered() of areas and turfs.

Calling Move() can be done either by calling it directly, or using the global procs that call it, such as walk() and step().

Doing something over time basically means you need a loop that periodically repeats certain behavior, such as taking a step in some direction.
world
New()
..()
spawn while(TRUE)
// Do something. This happens once every world tick.

// For example, call Update() on every mob in existence.
for(var/mob/m) m.Update()

// Wait for the next tick.
sleep tick_lag

mob
proc
// This is called once every tick (because of the global Tick()'s behavior).
Update()
/* Determine acceleration by adding to velocity.
Add velocity downwards if in the air.
Add velocity upwards if touching the ground and trying to jump, or if falling too fast (terminal velocity).
Add velocity rightwards if trying to move right, or if moving too fast to the left (speed limiter).
Add velocity leftwards if trying to move left, or if moving too fast to the right (speed limiter).
Decrease horizontal velocity if not trying to move (drag).
*/


// Move by the velocity.
// (simple example; requires pixel movement; doesn't support sub-pixel movements)
step_size = max(abs(velocity_x), abs(velocity_y))
Move(loc, dir, step_x + velocity_x, step_y + velocity_y)

// Velocity can be represented as a pair of numbers.
// It needs to persist between calls to Update() (Newton's first law);
// like position and unlike acceleration.
var
velocity_x = 0 // Pixels to the east to move
velocity_y = 0 // Pixels to the north to move

^This is basically what Forum_account's Pixel Movement and Sidescroller libraries do.
* Instead of mob.Update(), it's mob.movement(), called every tick by world.movement() in world.movement_loop().
* Acceleration is applied in mob.gravity() (for gravity) and mob.action() (through the mob.move() and mob.climb() procs, for handling player input).
* Velocity is applied in mob.movement() by calling mob.pixel_move(mob.vel_x, mob.vel_y).

Why not just use Forum_account's Sidecroller? You probably won't end up with anything vastly different, and you probably won't make any improvements that you couldn't have just made on top of using the library.
Thank you.
I don't wanna use it because i want to try to get everything out that i don't really need and not use the hole entire lib such as the keyboard lib if i don't have to, the less i have to use the better.

The last time i used the lib it work flawlessly till i tried using a dense object wall then i could walk right through it i even posted the bug and even restarted the project with just it and no one could find the problem so if i could write my own then i could see the problems more easier i hope.

but once again thank you
There's no shame in using what Kaiochao has written out for you, he basically wrote up a snippet on how to build the foundation you want to achieve.

On a side note, while forum_account's sidescroller library has it's share of difficulties for those who are unfamiliar with it. It's also the fastest way to cook up a sidescroller on BYOND. If you find yourself dabbling with it again and experience any issues, feel free to give me a shout(Just make sure you provide the code troubling you).
In response to Flame Guardian
Flame Guardian wrote:
There's no shame in using what Kaiochao has written out for you, he basically wrote up a snippet on how to build the foundation you want to achieve.


i am using the snippet foundation he gave me and it is helping me quite well, i just needed a clearer understanding and it helped quite well with all the wording and how to do it.

On a side note, while forum_account's sidescroller library has it's share of difficulties for those who are unfamiliar with it. It's also the fastest way to cook up a sidescroller on BYOND. If you find yourself dabbling with it again and experience any issues, feel free to give me a shout(Just make sure you provide the code troubling you).


If i use forum_account's again i will surly page you.

In response to DistantWorld101
DistantWorld101 wrote:
If i use forum_account's again i will surly page you.

And no one would blame you for being surly.

But like Kaiochaio said, basically this is a question of physics. When you throw a ball in the air, it follows an arc called a parabola. It has an upward velocity to start, and then gravity reduces and ultimately reverses that by acceleration. The math works out like this:

y = -gt2/2 + v0t + y0
v = v0 - gt
a = -g

t is the number of seconds that have passed.
y0 is the initial height, and y is the height at time t.
v0 is the initial upward velocity, and v is the upward velocity at time t.
a is the upward acceleration. g is the amount of acceleration from gravity.

You can define these units in terms of pixels and ticks in a game. You'll probably want to define them based on how high the jump arc should be. If you want to be able to jump up by 4 tiles, then you need to set that arc height to be 4 tiles plus a little bit extra for wiggle room. And to find the maximum height of the jump:

v0 - gt = 0
gt = v0
t = v0 / g

Assuming y0 = 0:

y = -gt2/2 + v0t
y = -g(v0/g)2/2 + v02/g
y = -v02/2g + v02/g
y = v02/2g

This shows that the arc height relates to both the speed of your jump, and the force of gravity. Doubling your jump speed will quadruple the arc height; and doubling gravity will cut the arc height in half.

First consider gravity. Let's say you have 32x32 tiles. What does that represent in real life? If your character is 32x64, and you can call that about 6 feet tall (give or take), then that gives a starting point. The force of gravity in the real world is about 32 ft/s2. 3 feet per 32 pixels, and let's say 40 FPS, leads to this:

32 ft/s2 × (32 px/ 3 ft) × (1 s / 40 ticks)2 = 1024 px / 600 ticks2 ≈ 1.71 px/ticks2

It's easier on everyone to round that up, so let's make it 2. That's a bit higher gravity than on Earth, but it's good enough.

So now back to arc height. If you want a jump of about 4 tiles, with a little give, the arc height should be a little above 128px. Let's shoot for 144.

y = v02/2g
2gy = v02
2(2)(144) = v02
24 = v0

Bingo. No rounding needed, even. The velocity of the jump should, in this case, be 24 px/tick.

But let's say the gravity is too high. This does seem like an awfully fast jump, and lower gravity would probably be more fun for gameplay. So trying for g=1 px/tick2, the calculation changes:

2(1)144 = v02
v0 ≈ 16.97 ≈ 17

So in that case, an initial jump of 17px is your best bet. At 16px, the formula would give you an arc height of 128 exactly, which would make jumping 4 tiles up a little too difficult.

Of course you're not limited to integers, but if you stray into decimals you'll probably want a way of tracking fractional step size changes, which BYOND doesn't do internally.

Sidescrollers also have other factors to consider:

- You may want a terminal velocity for any falling object, to simulate wind resistance. Terminal velocity for a human is roughly 120 MPH, or 176 ft/s.
- If your character falls faster than 1 tile at a time, collision detection gets more complicated too. Prepare for that.
- Walking up and down slopes is a special case, but one you'll want to handle well. Basically you want to assume a character on the ground stays on the ground, instead of walking out into the air and falling constantly. Likewise walking upslope shouldn't count as walking into a wall.
- Realistic jumping means you lose control over your horizontal momentum while in the air. More players are used to Mario jumping, where you maintain horizontal control to fine-tune the jump. It's unrealistic but it plays a lot better.