[somehow Solved] Moving platforms

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Lirija
Prole
Posts: 28
Joined: Tue Mar 14, 2017 11:42 am
Contact:

[somehow Solved] Moving platforms

Post by Lirija »

Hi guys, have you some hint in how to make something move over a moving platform?
As of now the player moves with the platform but if i want to move around the moving plaftorm i have some glitchy behaviour, which is even more evident when the platform speed is almost = player speed;
here some relevant code (all of this is inside player:update() by the way):

(I tried coming up with two things but one made that slow /glitchy behaviour; the other one shoved the player away from the platform)

Code: Select all


-- HOW IT MOVES:

	if isDown("left") then

		self.dir = -1			--self.direction
		self.dx = self.dir * self.spd

	elseif isDown("right") then

		self.dir = 1
		self.dx = self.dir * self.spd
	end

	if love.keyboard.wasPressed("z") and self.on_ground then
		self.dy = self.jumpspd
		self.on_ground = false
		self.otherdx = 0
	end

	self.on_ground = false


	self.dx = math.abs(self.dx) < 10 and 0 or self.dx - self.dir * self.friction


	self.dy = self.dy > self.maxvspd and self.maxvspd or self.dy + self.gravity


-- WHAT I TRIED:

--A	local fakespd = self.dx * self.otherdx ~= 0 and self.dx -sign(self.dx) * self.otherdx or self.dx


--B	self.dx = self.dx * self.otherdx < 0 and self.dx -sign(self.dx) * self.otherdx or self.dx

--------------

	local	goal_x = self.x + (self.dx + self.otherdx) * dt
	local	goal_y = self.y + self.dy * dt

	local cols 

	self.x, self.y, cols = self.world:move(self, goal_x, goal_y, self.filter)

-- COLLISION RESOLUTION

	if not g_WAIT then
		for i, c in ipairs (cols) do

			-- walls are the only things without id

			if not c.other.id then
				if c.normal.y == -1 then
					self.dy = 0

					if self.current_animation == "fall" then self.current_animation = "ground" end

					self.on_ground = true

				elseif c.normal.y == 1 and not c.other.id then
					self.dy = -self.dy/4
				end

				if c.normal.x ~= 0 and not c.other.id then
					self.dx = 0
				end


			elseif c.other.id == "box" then
				if c.normal.y == -1 then
					self.dy = 0
					
					self.otherdx = c.other.otherdx 
					-- the box itself is on top of an enemy and is such that was box.otherdx = enemy.dx

					if self.current_animation == "fall" then self.current_animation = "ground" end
					self.on_ground = true
				end	
				if c.normal.x ~= 0 and self.on_ground then
					c.other.dx = math.floor(self.dx / 2)
				end

			elseif c.other.id == "enemy" then				
				self:death()
			end
		end
	end
end
Last edited by Lirija on Wed Jun 20, 2018 1:44 pm, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Moving platforms

Post by pgimeno »

When the player is on a platform, it should move at a speed which is the player's "normal" speed (zero if stopped, or nonzero if it's walking) PLUS the platform's speed. That should do it.
User avatar
Lirija
Prole
Posts: 28
Joined: Tue Mar 14, 2017 11:42 am
Contact:

Re: Moving platforms

Post by Lirija »

When the player is on a platform, it should move at a speed which is the player's "normal" speed (zero if stopped, or nonzero if it's walking) PLUS the platform's speed. That should do it.
I think i did that in this bit here

Code: Select all

	local	goal_x = self.x + (self.dx + self.otherdx) * dt
But the problem i was referring to arises when the two velocities have different signs and equal value;
so i was thinking about incresing player.dx when he walks in a direction opposite to the platform, something like

Ok meanwhile i was writing i kinda solved the problem this way:
when the player is moving opposite the moving platform i disable the relative velocity of the platform, now it's smooth.

Code: Select all

-- if self.dx and self.otherdx are opposite
 if self.dx * self.otherdx < 0 then
  	self.otherdx = 0
 end

local goal_x = self.x + (self.dx + self.otherdx) * dt

Post Reply

Who is online

Users browsing this forum: No registered users and 52 guests