Need some help with jumping and physics!

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
Echamiq
Prole
Posts: 6
Joined: Wed Dec 26, 2012 5:31 pm

Need some help with jumping and physics!

Post 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
Attachments
player.love
(1.5 KiB) Downloaded 140 times
Last edited by Echamiq on Fri Dec 28, 2012 10:34 am, edited 1 time in total.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: General LÖVE newbie needs help with moving his avatar

Post 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.
User avatar
Echamiq
Prole
Posts: 6
Joined: Wed Dec 26, 2012 5:31 pm

Re: General LÖVE newbie needs help with moving his avatar

Post 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
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: General LÖVE newbie needs help with moving his avatar

Post 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.
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: General LÖVE newbie needs help with moving his avatar

Post 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
User avatar
Echamiq
Prole
Posts: 6
Joined: Wed Dec 26, 2012 5:31 pm

Re: General LÖVE newbie needs help with moving his avatar

Post 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 :3

-Echamiq

Edit: ..if I knew how to.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: General LÖVE newbie needs help with moving his avatar

Post by Roland_Yonaba »

Well, the Karma mod isn't actually available.
But, you can give us some cookies instead. Or bacon.
User avatar
Echamiq
Prole
Posts: 6
Joined: Wed Dec 26, 2012 5:31 pm

Re: General LÖVE newbie needs help with moving his avatar

Post 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 :3
Santos: +1 internetbaconcookie :crazy:
Zeliarden: +1 internetbaconcookie :rofl:

-Echamiq
User avatar
Echamiq
Prole
Posts: 6
Joined: Wed Dec 26, 2012 5:31 pm

Re: General LÖVE newbie needs help with moving his avatar

Post 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

Code: Select all

player.body:getLinearVelocity()
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
Attachments
loveq.love
(1.64 KiB) Downloaded 113 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests