Store previous x/y value?

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
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Store previous x/y value?

Post by Ryne »

Hi, I would like to have variables for a "prevx" and "prevy". Which would be the last x/y variables before collision, so that once the player collides with something I could just set player.x/y to prevx/prevy. I just really don't know how to go about programming it. I can think of a very long and inefficient way of doing it that would just +/- x/y depending on the direction of the player, but I'm assuming there is a better/nicer way.

Any Ideas?
@rynesaur
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Store previous x/y value?

Post by nevon »

Well, you could just create two more properties where you store your last x and y values, but that does seem like a rather crude solution. A more elegant approach would be to check for collisions before you move your character.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Store previous x/y value?

Post by tentus »

Crude, but pretty effective. To store a self.prevX all you have to is rewrite it in update(). By contrast, calculating collision before it happens is a ton of code that would be all over the place.

Ryne, to see a working example, look at the source for the "companion" actor in Kurosuke. It is actually 8 frames behind the player, but the same execution applies.
Kurosuke needs beta testers
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Store previous x/y value?

Post by Ryne »

tentus wrote:Crude, but pretty effective. To store a self.prevX all you have to is rewrite it in update(). By contrast, calculating collision before it happens is a ton of code that would be all over the place.

Ryne, to see a working example, look at the source for the "companion" actor in Kurosuke. It is actually 8 frames behind the player, but the same execution applies.
I don't quite understand iteration, and I also don't use middleclass, would you know a similar way excluding middleclass and would you mind explaining it? I would really appreciate it, if you have the time.
@rynesaur
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Store previous x/y value?

Post by nevon »

Ryne wrote:would you mind explaining it? I would really appreciate it, if you have the time.
What you're asking for is basically this:

Code: Select all

function love.load()
    --This is where we make your character. My syntax may be fucked up from having used Python recently.
    player = {
        x = 100,
        y = 100,
        prevx = 100,
        prevy = 100,
    }
end

function love.update(dt)

    if playeriscollidingwithstuff() then
        player.x = player.prevx
        player.y = player.prevy
    end

    player.prevx = player.x
    player.prevy = player.y
end
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Store previous x/y value?

Post by Ryne »

nevon wrote:
Ryne wrote:would you mind explaining it? I would really appreciate it, if you have the time.
What you're asking for is basically this:

Code: Select all

function love.load()
    --This is where we make your character. My syntax may be fucked up from having used Python recently.
    player = {
        x = 100,
        y = 100,
        prevx = 100,
        prevy = 100,
    }
end

function love.update(dt)

    if playeriscollidingwithstuff() then
        player.x = player.prevx
        player.y = player.prevy
    end

    player.prevx = player.x
    player.prevy = player.y
end
I see, I didn't think it would work that way, but I guess it does. Thanks nevon!
@rynesaur
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Store previous x/y value?

Post by kikito »

What about storing them in local variables, before updating?

Code: Select all

player = { x = 100, y = 100 }

function love.update()
  local prevX, prevY = player.x, player.y

  doStuffThatMovesThePlayer()

  if playerCollidesWithAnything() then
    player.x, player.y = prevX, prevY --restore with prevX, prevY
  end
end
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests