Page 1 of 1
Need some help with jumping and physics!
Posted: Wed Dec 26, 2012 5:42 pm
by Echamiq
Hi!
I'm totally new to Love2d, I have programmed Lua a little in minecraft, and I have an issue:
I want to have a picture as an avatar, controlled with the arrow keys. But the script doesn't give any error messages, but I only get a brown version of my originally red avatar which is uncontrollable.
If you need any more info, please ask me!
Thanks a lot in advance,
-Echamiq
Re: General LÖVE newbie needs help with moving his avatar
Posted: Wed Dec 26, 2012 9:56 pm
by Roland_Yonaba
Hi,
I don't know much of love.physics. But hopefull, these pointers might help.
First, you created your world with a gravity. So keep in my that any new body you put into that world will tend to go down, due to gravity.
In player.lua, you defined a restitution coefficient of 0. So it implies that at start, the body goes down, and stands sticked to the ground. You might want to change that coefficient to 1 (for full restitution), or any other value lower than 1, maybe 0.9, depending on what you need.
Actually, you are handling the inputs correctly (adding forces to the body). The problem is in player_draw. Drawing the player pic at player.x, player.y won't display it at the correct position, as love physics doesn't act on these values, but on player.body. So you just have to change this line in player.lua
Code: Select all
love.graphics.draw(player.pic, player.x, player.y)
to
Code: Select all
love.graphics.draw(player.pic, player.body:getX(), player.body:getY())
Hope this helps.
Re: General LÖVE newbie needs help with moving his avatar
Posted: Thu Dec 27, 2012 10:42 am
by Echamiq
Well... Only drew the avatar slightly lower, and it's still brown-colored... The part about physics only acting on bodies makes a lot of sense though.
Thanks so much for helping already, but I'm afraid I'm going to need some more...
-Echamiq
Re: General LÖVE newbie needs help with moving his avatar
Posted: Thu Dec 27, 2012 11:25 am
by Santos
The current color can affect images too. Then the color is being set with
love.graphics.setColor(72, 160, 14) in
ground_draw is still used when the player is drawn. The simplest way to fix this is to change the color to white before drawing the player:
Code: Select all
function player_draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player.pic, player.x, player.y)
end
An alternative way is to change how the current color affects what's drawn with
love.graphics.setColorMode.
Re: General LÖVE newbie needs help with moving his avatar
Posted: Thu Dec 27, 2012 12:08 pm
by Zeliarden
remove dt and the player.y test to get it to move
the force 200*0.01666 = 3.3333 is too small for the body to move
Code: Select all
function player_move(dt)
if love.keyboard.isDown("right") then
player.body:applyForce(player.speed , 0)
elseif love.keyboard.isDown("left") then
player.body:applyForce((player.speed * -1, 0)
elseif love.keyboard.isDown("up") then
player.body:applyForce(0, (player.speed * -1)
end
end
Re: General LÖVE newbie needs help with moving his avatar
Posted: Thu Dec 27, 2012 12:32 pm
by Echamiq
Santos wrote:The current color can affect images too. Then the color is being set with
love.graphics.setColor(72, 160, 14) in
ground_draw is still used when the player is drawn. The simplest way to fix this is to change the color to white before drawing the player:
Code: Select all
function player_draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player.pic, player.x, player.y)
end
An alternative way is to change how the current color affects what's drawn with
love.graphics.setColorMode.
That makes a lot of sense, thanks a bunch!
Zeliarden wrote:remove dt and the player.y test to get it to move
the force 200*0.01666 = 3.3333 is too small for the body to move
Code: Select all
function player_move(dt)
if love.keyboard.isDown("right") then
player.body:applyForce(player.speed , 0)
elseif love.keyboard.isDown("left") then
player.body:applyForce((player.speed * -1, 0)
elseif love.keyboard.isDown("up") then
player.body:applyForce(0, (player.speed * -1)
end
end
I hadn't thought of calculating that, I'll out in both the changes when I have time, I'm currently participating in a game-making marathon in my home city, and I'm using Löve, yay!
Thanks already everyone for helping me, I'll give you some karma
-Echamiq
Edit: ..if I knew how to.
Re: General LÖVE newbie needs help with moving his avatar
Posted: Thu Dec 27, 2012 12:55 pm
by Roland_Yonaba
Well, the Karma mod isn't actually available.
But, you can give us some cookies instead. Or bacon.
Re: General LÖVE newbie needs help with moving his avatar
Posted: Thu Dec 27, 2012 1:20 pm
by Echamiq
Roland_Yonaba wrote:Well, the Karma mod isn't actually available.
But, you can give us some cookies instead. Or bacon.
Okay, that explains it.
Roland_Yonana: +1 internetbaconcookie
Santos: +1 internetbaconcookie
Zeliarden: +1 internetbaconcookie
-Echamiq
Re: General LÖVE newbie needs help with moving his avatar
Posted: Thu Dec 27, 2012 8:11 pm
by Echamiq
Yay, I can use physics a little! My character can now jump, and move. But I don't want him to be able to jump (alter his vertical velocity) when he is in mid-air. The way I tried to do that is by using
I now recieved the input for the y and x velocity, and in a weird way which I can't comprehend both have to be 0 to be able to jump. Is there a way for me to specify that I want the x-velocity to be maintained and the y-velocity to be changed to my jump-velocity?
More advanced questions, yay!
Thanks a bunch for helping me!
-Echamiq