Jump Mechanic Differences between positive and negative x values

Hi,

I am developing a 2D platformer and I am working on the jumping mechanic.
I have used the v = u + at kinematic equation for the formula for a smooth jump.

When I run the game I am able to jump to the left (in the negative direction) smoothly with a gentle curve.
However, when I jump to the right (in the positive direction) the character teleports to the right rather than moving in a smooth gentle curve like when I jump to the left.

Here is the code snippet that handles the jumping:

self.velocity is applied in the update function and self.ground is set to false so the player cannot jump in the air.
self.direction is set when the player moves left or right to identify which way the player is wanting to jump.

Is there any reason for the changes in the jump between negative and positive x values?

Hey, could you also post your update function? At least the chunk where you perform the movement.

Here is the section of the update function that relates to movement.

Please use the three backticks ``` to define a code segment. Images aren’t very readable, and also not searchable on the forum:

local example = 0

As for your code, I’d suggest keeping a variable self.direction which can be -1, 0 or 1.
Set it to 0 after each update, and set it to either -1 or 1 after the corresponding keypress.
Then the velocity could be calculated like so:

self.velocity = self.velocity + vmath.vector3(self.direction,  5, 0) * dt

I added dt there in order to make the code a bit more future proof, and not framerate dependent.

I am not sure but your dynamic is a bit wrong… It should be something along this line:

self.velocity = self.velocity + self.gravity * dt
self.pos = self.pos + self.velocity * dt

So that gravity influence on position is quadratic and not linear.

I think the problem you are experiencing is due to:

self.velocity * speed

if I understand correctly this is always positive and, in generale, looks quite odd :slight_smile:

But, of course, I am only trying to guess from the code snippet you posted. So, I may be wrong!

Ciao!

3 Likes

Here is the code snippets done properly:

update function for movement:

local position = go.get_position() 
	local pos = position + self.velocity * speed * dt
	go.set_position(pos + self.gravity * dt)
	self.velocity = vmath.vector3() 

Jump handling:

if action_id == hash('space') and self.ground == true then
		if self.direction == 'right' then
			--v = u + at
			self.velocity = self.velocity + vmath.vector3(5,5,0) * 5
			self.ground = false
		end
		if self.direction == 'left' then
			--v = u + at
			self.velocity = self.velocity + vmath.vector3(-5,5,0) * 5
			self.ground = false
		end
		if self.direction == '' then
			--v = u + at
			self.velocity = self.velocity + vmath.vector3(0,5,0) * 5
			self.ground = false
		end
	end

Thank you very much for the feedback, I will implement your suggestions and see if it works.

1 Like

I’m a little unsure of what you mean.
What do you mean by quadratic and not linear? and why does that matter?

Acceleration, in your case gravity, is the rate of change of velocity:

vel = vel + dt * acc

and velocity is the rate of change of pos:

pos = pos + dt * vel

If you “unwrap” these finite difference equations you get:

pos = initial_pos + initial_vel * T + 0.5 * gravity * T^2

and you have a T^2 multiplied by the gravity. This is a quadratic dependence of the position by the gravity.

1 Like