Different principle of physics working with the same code

UPD: I just gave up and increased the size of the go to six times the size of the first project

Note: I apologize if the question title was worded incorrectly. I didn’t know how to properly describe my problem.

Tried to learn how to make a platformer from the guides. In the first project, the character moves and jumps very smoothly and pleasing to the eye.

local GRAVITY = 100

function init(self)
	self.velocity = vmath.vector3()
	msg.post(".", "acquire_input_focus")
	self.move = true
end

function fixed_update(self, dt)
	self.velocity.y = self.velocity.y - GRAVITY * dt

	if self.ground_contact then
		self.velocity.y = 0
	end
		
	local position = go.get_position()
	position = position + self.velocity 
	go.set_position(position)
	self.velocity.x = 0 
end

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response")
	and message.group == hash("earth") then
		self.ground_contact = true;
	end
	
	if message_id == hash("contact_point_response")
	and message.group == hash("ledge") then
		self.ground_contact = true
		self.jumping = true
	end
end

function on_input(self, action_id, action)
	if action_id ~= hash("jump") then
		if action_id == hash("right") then
			self.velocity.x = self.velocity.x + 20
			if self.jumping then
				self.ground_contact = false
				self.jumping = false
			end
		elseif action_id == hash("left") then
			self.velocity.x = self.velocity.x - 20
			if self.jumping then
				self.ground_contact = false
				self.jumping = false
			end
		end
	elseif action.pressed and self.ground_contact == true and action_id == hash("jump") then
		self.ground_contact = false
		self.velocity.y = 35
	end
end

In the second, the code (if you ignore the if-else) is identical, but the character jerks rather unpleasantly instead of jumping smoothly

local GRAVITY = 100
local JUMP = 20 
local MOVE_SPEED = 100
function init(self)
	msg.post(".", "acquire_input_focus")

	self.velocity = vmath.vector3()

	self.downfall = true -- True - падение | False - удержание на месте

end

function fixed_update(self, dt)

	self.velocity.y = self.velocity.y - GRAVITY * dt

	if not self.downfall then
		self.velocity.y = 0
	end

	self.velocity.x = self.velocity.x * dt
	
	local position = go.get_position()
	position = position + self.velocity
	go.set_position(position)

	self.velocity.x = 0
	
end

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response")
	and message.own_group == hash("hands") then
		self.downfall = false 
	end
end

function on_input(self, action_id, action)
	
	if action_id == hash("right") then
		self.velocity.x = self.velocity.x + MOVE_SPEED
	end

	if action_id == hash("left") then
		self.velocity.x = self.velocity.x - MOVE_SPEED
	end

	--Прыжок
	if action_id == hash("jump") 
	and self.downfall == false 
	and action.pressed then
		self.velocity.y = JUMP
		self.downfall = true 
	end

end

The only difference between them is that in the first one I zoomed all go objects, and in the second one I tried to realize zooming through zoom (I use an orthographic camera))

(first project) TestPlatformerProject.zip (90.8 KB)
(second project) RiseToMountin.zip (259.9 KB)

What I see is that the 2 code are much different.
For example this one doesn’t exist in the first code.

What do you mean of that?

Yes, there are definitely differences, but they have to do with logic. When I did the first project it was according to the guide, but I wanted to write the logic myself. The physics is pretty much identical.

About self.velocity.x, I thought that’s the way to do it so that movement works the same regardless of the number of fps, maybe I misunderstood the principle of multiplication by dt

You would do it here:

position = position + self.velocity * dt

Multiplying by dt changes the logic from “add this amount of velocity per frame” to “add this amount of velocity per second”.

You’re maybe confused by this line:

self.velocity.y = self.velocity.y - GRAVITY * dt

That one is fine because it says “reduce y velocity by this amount of gravity per second”.

Multiplying self.velocity.x by dt is meaningless.

2 Likes