BrotSagtMist wrote: ↑Fri Dec 09, 2022 4:59 pm
Id say if there is a touch simply replace the player.
I don't understand well, what is "touch simply"? (I'm beginner)
---
darkfrei wrote: ↑Fri Dec 09, 2022 4:59 pm
1) Be sure that you update platform before update of player.
2) Why not hard placing as player.y = platform.y + player.height? (while standing on the platform)
1) I've tried to move the platform first, but I think the coordinates became from world:update(dt), not?
2) I've tried, but maybe I'm missing something (btw it's not - player.height no? because + is below the platform?). And this approach do I have to designate another collider only on the surface?
---
Maybe this code with a simple implementation from my game could be useful:
Code: Select all
local world
Player = {}
local Platform = {}
local deltaMovement = 0
WIDTH = love.graphics.getWidth()
HEIGHT = love.graphics.getHeight()
-- Player
local function jump()
Player.physics.body:applyLinearImpulse(0, -2000)
end
local function move(dt)
if Player.isMovementPlatformY then
--Player.physics.body:setY(Platform.physics.body:getY() - 80)
Player.physics.body:setY(Player.physics.body:getY() + deltaMovement)
end
end
-- Platform
local function platformMove(dt)
local _, py = Platform.physics.body:getPosition()
deltaMovement = Platform.dir * 100 * dt
Platform.physics.body:setY(py + deltaMovement)
Platform.distActual = Platform.distActual + math.abs(deltaMovement)
if Platform.distActual >= Platform.distLimit then
Platform.dir = Platform.dir * -1
Platform.distActual = 0
end
end
function love.load()
-- physics
world = love.physics.newWorld(0, 1000, false)
world:setCallbacks(beginContact, endContact, preSolve, postSolve)
-- PLAYER
Player.isJumping = false
Player.isGrounded = true
Player.isMovementPlatformY = false
-- Physics
Player.physics = {}
Player.physics.body = love.physics.newBody(world, WIDTH/2, HEIGHT/2, "dynamic")
Player.physics.body:setFixedRotation(true)
Player.physics.shape = love.physics.newRectangleShape(40, 80)
Player.physics.fixture = love.physics.newFixture(Player.physics.body,
Player.physics.shape, 1)
Player.physics.fixture:setUserData("Player")
-- PLATFORM
-- Physics
Platform.physics = {}
Platform.physics.body = love.physics.newBody(world, WIDTH/2, HEIGHT/2 + 100, "static")
Platform.physics.shape = love.physics.newRectangleShape(100, 20)
Platform.physics.fixture = love.physics.newFixture(Platform.physics.body,
Platform.physics.shape)
Platform.dir = 1
Platform.physics.fixture:setUserData("Platform")
Platform.distActual = 0
Platform.distLimit = 100
end
function love.update(dt)
world:update(dt)
platformMove(dt) -- Platform
move(dt) -- Player
end
function love.draw()
debug()
end
function beginContact(a, b, coll)
if a:getUserData() > b:getUserData() then a, b = b, a end
if (a:getUserData() == "Platform" and b:getUserData() == "Player") then
Player.isJumping = false
Player.isGrounded = true
Player.isMovementPlatformY = true
end
end
function endContact(a, b, coll)
if a:getUserData() > b:getUserData() then a, b = b, a end
if (a:getUserData() == "Platform" and b:getUserData() == "Player") then
Player.isJumping = true
Player.isGrounded = false
Player.isMovementPlatformY = false
end
end
function preSolve(a, b, coll)
if a:getUserData() > b:getUserData() then a, b = b, a end
if (a:getUserData() == "Platform" and b:getUserData() == "Player") then
Player.isJumping = false
Player.isGrounded = true
end
end
function postSolve(a, b, coll, normalimpulse, tangentimpulse)
end
-- Keyboard
function love.keypressed(key)
if key == "w" or key == "up" then
jump()
end
end
function debug()
-- Debug
for _, body in pairs(world:getBodies()) do
for _, fixture in pairs(body:getFixtures()) do
local shape = fixture:getShape()
if shape:typeOf("CircleShape") then
local cx, cy = body:getWorldPoints(shape:getPoint())
love.graphics.circle("fill", cx, cy, shape:getRadius())
elseif shape:typeOf("PolygonShape") then
love.graphics.polygon("fill", body:getWorldPoints(shape:getPoints()))
else
love.graphics.line(body:getWorldPoints(shape:getPoints()))
end
end
end
end