Sorry i was late to reply.
I’ll elaborate a little on the questions and clarify a couple things as well.
but im looking through the code and not seeing how you actually made him jump to a max height or is the above like setting the -JumpAcceleration shifting him 35 pixels and then you are adding gravity?
When the player jumps a simple Acceleration upwards is added to the players total velocity that is fairly large but only added one frame then the jump button is ignored for some amount of jump reset time. Every frame after the frame he jumped his total velocity X is slowed partially by friction or his total Y velocity is pulled down by gravity.
im unsure why they were using Delta Time twice. one for veloctiy and one for position
I have to admit i wasnt sure where you were going with this comment?
To clarify what i said earlier.
- velocity and speed.
Velocity proper is (a Vector representation) of Speed which is a (float value).
Aka velocity has a X and Y component (or XYZ component in 3d).
Speed is just a scalar a float s.
You can convert between velocity and speed equivalently.
by pythagoras
speed = squareRoot( velocity.X * velocity.X + velocity.Y * velocity.Y);
normalDirection = new Vector2( velocity.X / speed, velocity.Y / speed);
velocity = normalDirection * Speed;
(since they are the same sometimes the words and the ideas become misnomers)
Be aware of the difference between a direction and a normalized direction.
A Normalize direction can be scaled by a Scalar to give a velocity from a speed properly.
Acceleration is also a Vector or a Scalar in the exact same manner and often we say simply a object speeds up by some amount every second i.e. it is accelerating by some speed or thrusting.
You will usually need to apply the vector version (Velocity) as a addition to the position as opposed to a scalar speed so conversion is typically done.
Velocity is a constant motion (aka things in motion stay in motion unless a force is applied to it)
Velocity is altered by variable Accelerations each frame.
Accelerations are generated by conceptual forces (conceptual forces are the ideas of what affects your game objects each frame e.g. thrust gravity friction wind ect…).
These ideas are defined in terms of accelerations per second.
Aka…
GravityPerSecond
GroundFrictionPerSecond
AirFrictionPerSecond
PlayerKeyPressAccelerationPerSecond
The player then has his
CurrentVelocityPerSecond V note elapsed time needs to be be applied before adding to position.
and his
CurrentPositionThisFrame P
You may accumulate the different accelerations in a frame then apply them to change the players velocity which at the very end will update the players position.
For example.
When player moves right he generates a force that Accelerates him Vector2.Right * playerSpeed, aka it’s a acceleration that will be added to his total velocity. The force of gravity also accelerates him down changing the velocity.
The velocity of the player himself is de-Accelerated by friction which can be simulated by having a direction opposite the players Velocity or maybe just the X component of it finding the normal of that result a amount of speed is applied a a counter acceleration. Or the total velocity can just be muliplied by a value like .99f.
A pseudo implementation.
first define the following vectors.
Let P stand for Position Now.
Let V stand for Total Velocity Now.
Let A stand for Total Acceleration this frame.
Let E stand for ElapsedTime or the portion of time that has passed between the last frame and the current (the portion of a second elapsed).
Let K stand for my defined kinetic Acceleration (of velocity or speed x and y) that i want to see a game object increase its velocity or speed by per second, when i press a key. E.g. 10 pixels per second We wont use this atm well redefine it as ak shortly for simplicity as it depends on which key is pressed and maybe our current total velocity as well or however we want it too.
Further so we define a bunch of acceleration forces represented as scalars (aka floats
) you set these individually to get things to feel right in your game.
Let ag stand for the force of gravity, acceleration per second.
Let aw stand for the force of wind resistance, acceleration per second.
Let ar stand for the force of ground or running resistance, acceleration per second.
Let ak stand for the force of player key press, acceleration per second.
(in truth we have forward back and up for a platformer we switch on which key is pressed and if the action is possible to do
)
We need normalized directions for the forces or vectors of force for each.
ng = Vector2.Normalize(new Vector2(0, -1)); // downwards direction
nw = Vector2.Normalize(V) * -1f; // negated velocity direction.
nr = Vector2.Normalize(V) * -1f; // negated velocity direction.
nk = SomeVectorReturningFunctionDependingOnKeyPresses();
we add together all the forces that accelerate our object tempered by the elapse portion of time
A = (ng * ag * E) + (nw * aw * E) + (nr * ar * E) + (nk * ak * E);
the above gives us Acceleration per this frame which is a portion of a second.
we alter the velocity by the total accelerations from all the forces acting on our player for that frame.
V = V + A;
note the above velocity v is per second.
Every update we normally displace the position of our player by the current velocity he has per second for this portion of time elapsed.
P = P + V * E;
Now by the above steps if we were in a space game removing all friction values then we essentially have newtonian motion the player will stay in motion unless he thrusts.
By the above steps in a platformer with friction, provided we apply enough gravity and enough acceleration for running and jumping, aka we set our per second values up to make it behave how we like. Then we have our similar normal everyday motion.
Now while collisions are technically friction in game we handle them specially to because modeling such a thing accurately would be ridiculously expensive and over complicated.
Often for things like climbing ladders we cheat and just turn off gravity and collisions on the ladder just do some special behavior to replace movement.
Note some people will shortcut the math and determine the Acceleration per second total then before adding to velocity multiply by elapsed time that is short form of what i did long form for clarity.
It is math wise equal so you come out with the same numbers.
Accelerations xy
Velocity xy
Positional displacement xy.
A = (vg * ag) + (nw * aw) + (nr * ar) + (nk * ak);
V = V + A * E;
P = P + V * E;
while you store scalars to apply against (or to scale) normalized direction vectors, since these forces impart accelerations in some direction like gravity applys a downward acceleration to a objects total velocity.
ak … playersSpeedPerSecond = 10; // our dude runs fast or slow ect.
ag … gravitySpeedPerSecond = 3; // it never turns off and it stays the same.
aw … windResistanceSpeedPerSecond = 0.2f; // when the player jumps the air itself slows him down.
ar …groundResistanceSpeedPerSecond = 3; // friction keeps us from staying in motion.