Page 1 of 1
Slope issues
Posted: Sat Feb 14, 2015 11:54 pm
by AlexsSteel
Hi,
I have been trying to make a platformer game using LÖVE Physics, almost everything works fine, but i have three questions about slopes:
1º How can I prevent the player from falling off over the slopes?
2º What can I do to detect if the player is over a slope?
3º How can I prevent the player jumping at the top of a slope?
* My english it's really bad, so i attached an image showing the related issue.
Re: Slope issues
Posted: Sun Feb 15, 2015 6:07 am
by norubal
How about checking this thread? markgo made good example before, I hope it can help you.
viewtopic.php?f=5&t=12214
Re: Slope issues
Posted: Sun Feb 15, 2015 5:57 pm
by AlexsSteel
I solved the first and the third question, but i don't know how to solve the second problem.
This code works with the square tiles, but doesn't if the player is on a slope.
Code: Select all
function createPlayer(x, y)
if #players < 5 then
local nPlayer = #players+1
players[nPlayer] = {}
local body = love.physics.newBody( level, x, y, 'dynamic' )
local pShape = love.physics.newPolygonShape( 0.25, 0.4, 0.75, 0.4, 1, 0.65, 1, 1.75, 0.75, 2, 0.25, 2, 0, 1.75, 0, 0.65 )
--body:setGravityScale(0)
body:setFixedRotation(true)
local fixture = love.physics.newFixture( body, pShape, 1 )
fixture:setFriction(0)
fixture:setRestitution(0)
local sShape = love.physics.newRectangleShape( 0.26, 1.9, 0.48, 0.5, 0 )
local sensor = love.physics.newFixture( body, sShape, 1 )
sensor:setSensor( true )
sensor:setFriction(0)
sensor:setRestitution(0)
body:setMassData( 0.5, 1, 80, 0 )
players[nPlayer] = { body, pShape, fixture, sensor, false, false, 0, false, 0 }
end
end
What is the best way to know if a body is colliding with a slope?
function beginContact(a, b)
if a == players[1][4] or b == players[1][4] then
players[1][9] = players[1][9] + 1
players[1][8] = true
end
end
function endContact(a, b)
if a == players[1][4] or b == players[1][4] then
players[1][9] = players[1][9] - 1
if players[1][9] <= 0 then
players[1][8] = false
end
end
end
Re: Slope issues
Posted: Sun Feb 15, 2015 7:35 pm
by AlexsSteel
Don't worry, i solved it.
Re: Slope issues
Posted: Sun Feb 15, 2015 9:19 pm
by rmcode
I found this article on slopes quite helpful:
http://concernedjoe.com/devblog/hardcoding-slopes/
Concerned Joe <3
Re: Slope issues
Posted: Mon Feb 16, 2015 9:05 am
by Jasoco
AlexsSteel wrote:Don't worry, i solved it.
That's not really solving your slope issue. It's creating a workaround to avoid solving the problem.
Re: Slope issues
Posted: Mon Feb 16, 2015 10:49 am
by s-ol
Jasoco wrote:AlexsSteel wrote:Don't worry, i solved it.
That's not really solving your slope issue. It's creating a workaround to avoid solving the problem.
I'd say thats a pretty legit way of doing slopes with box2D