Page 1 of 1

Moving....stuff!

Posted: Sat May 25, 2013 9:22 pm
by WahWah
Hello! :D

I am very new to lua and i just want to know how i can get an object to move a designated length
instead of teleporting instantly :)
For example when jumping.
I just want to tap w for example and i want the player to move up a specific length smoothly.

Re: Moving....stuff!

Posted: Sun May 26, 2013 4:34 am
by veethree
Here's a recent thread about jumping. For future reference please search the forums and/or wiki before posting :)

Re: Moving....stuff!

Posted: Sun May 26, 2013 10:54 am
by Sheepolution
So you can do this:

Code: Select all

x-position = 400
That's teleporting.
But what you want to do is this:

Code: Select all

x-position = x-position + speed
And I can recommend you to use dt (deltatime).
So in the end you get something like this:

Code: Select all

function love.main()
image = newImage("image")
x = 10
y = 200
speed = 100
end

function love.update(dt)
x = x + speed * dt
end

function love.draw()
love.graphics.draw(image,x,y)
end

Re: Moving....stuff!

Posted: Sun May 26, 2013 4:56 pm
by WahWah
Sheepolution wrote:So you can do this:

Code: Select all

x-position = 400
That's teleporting.
But what you want to do is this:

Code: Select all

x-position = x-position + speed
And I can recommend you to use dt (deltatime).
So in the end you get something like this:

Code: Select all

function love.main()
image = newImage("image")
x = 10
y = 200
speed = 100
end

function love.update(dt)
x = x + speed * dt
end

function love.draw()
love.graphics.draw(image,x,y)
end

Yeah. I already know that :)
What i mean is that i want to move a desired amount of pixels and then stop. :)
Thanks anyway :)

Re: Moving....stuff!

Posted: Sun May 26, 2013 5:56 pm
by micha
WahWah wrote:Yeah. I already know that :)
What i mean is that i want to move a desired amount of pixels and then stop. :)
Thanks anyway :)
Is it for jumping or for smooth movement?
If you want to implement jumping, then you do it just like Sheepolution suggested and in each timestep do

Code: Select all

velocity.y = velocity.y + gravity * dt
That way the object will have a highest point and fall back down.

If you just want smooth movement you need two pairs of coordinates: target-coordinates and actual-coordinates. The actual-coordinates are the ones, where you draw the object and the target-coordinates are the ones, where the object should move. Each timestep move the actual-coordinates towards the target-coordinates (it's called tweening).

Re: Moving....stuff!

Posted: Sun May 26, 2013 7:54 pm
by WahWah
micha wrote:
WahWah wrote:Yeah. I already know that :)
What i mean is that i want to move a desired amount of pixels and then stop. :)
Thanks anyway :)
Is it for jumping or for smooth movement?
If you want to implement jumping, then you do it just like Sheepolution suggested and in each timestep do

Code: Select all

velocity.y = velocity.y + gravity * dt
That way the object will have a highest point and fall back down.

If you just want smooth movement you need two pairs of coordinates: target-coordinates and actual-coordinates. The actual-coordinates are the ones, where you draw the object and the target-coordinates are the ones, where the object should move. Each timestep move the actual-coordinates towards the target-coordinates (it's called tweening).
Oh... I get it now :)
Thanks :D