Help with moving objects relative to each other (lerp math)

0 favourites
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I'm trying to figure out how to move an objects y axis in relation with another objects y axis. I've been trying to use lerp to do it but I can't get it to work properly. What I'm trying to do is if object 1 Y is 310 then object 2 Y is 1250. And if object 1 Y is -1800 then object 2 Y is 410. That part is easy enough to get to work but I'm having trouble with the math to get everything in between to move correctly. Any help would be much appreciated.

  • If I understand correctly, you want the following to happen:

    object1.Y starts at 310 and then moves over time to -1800 (a difference of -2110 pixels)

    and at the same time:

    object2.Y starts at 1250 and then moves over time to 410 (a difference of -840 pixels)

    and you want object1 to trigger object2, and for both to reach their end positions at the same time?

  • Almost. Object 1 gets moved by the player and is clamped between 310 and -1800. Object 2 moves by itself relative to object 1 from 1250 to 410. The percentage of the distance traveled should be the same between the two objects.

  • Are these moves supposed to happen smoothly over time? Or instantly, like once the player releases placement of object1, then object2 jumps to the appropriate place as a result?

    I ask about "over time" as you mentioned using lerp in your original post

    Either way, it leads me to think about these as percentages between their clamp values, determine the percentage that player one has selected, and then move object2 the same percentage between 1250 and 410

    If you're looking for the position change to move smoothly over time, then using lerp can help:

    lerp( a, b, x )

    lerp( starting value, target value, percentage of the distance to move each tick )

    so if you have:

    Set object2.Y to lerp( object2.Y, object1.yOffset, 1 )

    yOffset is the percentage between 310 and -1800 that the player has placed object1. object2.Y will then be set to the same percentage from 1250 to 410 immediately, as its travelled 100% of the way from where it is to where it should be this tick.

    If instead you have:

    Set object2.Y to lerp( object2.Y, object1.yOffset, 0.25 )

    Then object2 will only move 25% of the way there this tick, and now that its moved, the pixel distance between the two is shorter, so the next tick that 25% will be smaller. Lerp runs the risk of never reaching its destination and only moving smaller and smaller increments, so its good practice to check for a position that's really close to the target, and if its passed that position, set it to the target. Otherwise, it'll be lerping forever but essentially invisible to the eye.

    Is this close to what you're looking for?

  • I want them to move smoothly at the same time. so if object 1 is 25% of the way to the end then object 2 will be 25% of the way as well. How would I figure out the offset?

  • So if its the specific math you need, then the way to calculate it is:

    Object1.yMin = -1800

    Object1.yRange = 2110 ( -1800 + 2110 = 310 )

    Object2.yMin = 410

    Object2.yRange = 840 ( 410 + 840 = 1250 )

    Object1.Y = -1272.5 ( this is where the player has placed Object1 )

    Object2.Y = ???

    // yOffset uses the values of yMin, Y, and yRange to end up with a value

    // between 0 and 1... this decimal is our percentage 0.25 = 25%, etc

    - Object1.yOffset = abs( abs( Object1.yMin - Object1.Y ) / Object1.yRange )

    // we can then use that percentage to easily place Object2, as it'll take the

    // yOffset percentage, and multiply it by object2.yRange, giving the same

    // percentage between Object2.yMin, and Object2.yRange

    - Object2.Y = Object2.yMin + ( Object2.yRange * Object1.yOffset )

    // By putting the actual values in:

    - Object2.Y = 410 + ( 840 * 0.25 ) // which equals...

    - Object2.Y = 410 + ( 210 ) // which equals...

    - Object2.Y = 620

    I'm sorry if this comes across as patronizing, its not meant to be, I'm just not sure at what level you're at with all this... does this help?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Sorry, it's probably exactly what I need but that's way over my head. I don't understand how to implement it.

  • I can do a mockup for you tomorrow and post it here, sound ok?

  • I would appreciate it but only if it's no trouble.

  • Thank you. This looks promising. I just need to mess with it to make it work in my project.

  • For some reason when my object1.y goes down my object2.y goes up. What would I need to change so they travel in the same direction?

  • If you share your capx with me, I can more quickly identify what's going on for you, if you're comfortable sharing it that is. I'm on Discord if that'd be better for you.

  • Every time I try sharing a file on dropbox it doesn't work.

  • This would set obj2 from obj1:

    Obj2: set y to lerp(1250,410, unlerp(310,-1800,obj1.y))

    And this would set obj1 from obj2:

    Obj1: set y to lerp(310,-1800, unlerp(1250,410,obj2.y))

    You’d only use one of those at a time. Say when you’re moving one you’d update the other.

Jump to:
Active Users
There are 2 visitors browsing this topic (0 users and 2 guests)