Moving....stuff!

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
WahWah
Prole
Posts: 6
Joined: Fri Apr 19, 2013 9:03 pm

Moving....stuff!

Post 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.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Moving....stuff!

Post by veethree »

Here's a recent thread about jumping. For future reference please search the forums and/or wiki before posting :)
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Moving....stuff!

Post 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
User avatar
WahWah
Prole
Posts: 6
Joined: Fri Apr 19, 2013 9:03 pm

Re: Moving....stuff!

Post 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 :)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Moving....stuff!

Post 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).
User avatar
WahWah
Prole
Posts: 6
Joined: Fri Apr 19, 2013 9:03 pm

Re: Moving....stuff!

Post 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
Post Reply

Who is online

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