Best way to move from A to B

0 favourites
  • 9 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • What's the best method to move an object on the X axis from A to B?

    lerp?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • yes lerp is your friend here!

    lerp(a, b, c)

    so lerp)sprite.x, 150, 0.8*dt) // the 150 is the x position I want it to tween to.

  • Global number speed=10   //px per second
    Mouse: On left Click
      -> Sprite: set xA to Sprite.X
      -> Sprite: set yA to Sprite.Y
      -> Sprite: set xB to Mouse.X
      -> Sprite: set yB to Mouse.Y
      -> Sprite: set t to 0
      -> Sprite: set moving to true
    Sprite: is moving
      -> Sprite: set t to clamp(self.t+dt*speed*1/distance(self.xA,self.yA,self.xB,self.yB),0,1)
      -> Sprite: set X to lerp(self.xA,self.xB,self.t)
      -> Sprite: set Y to lerp(self.yA,self.yB,self.t)
    Sprite: t = 1
      -> Sprite: set is moving to false

    if you want a constant movement

  • If by best you mean least lines of events/least calculations:

    Sprite A X < B.X:

    -> A.X = A.X + 1

    Sprite A X > B.X:

    -> A.X = A.X - 1

    Note that you'd need to change it to this for move speeds > 1:

    Sprite A X < (B.X - SPEED):

    -> A.X = A.X + SPEED

    Sprite A X > (B.X + SPEED):

    -> A.X = A.X - SPEED

  • Jayjay

    It works only for horizontal movements and also you should multiply by dt to make it framerate independant.

    using the same method for any movement would be

    Sprite: Set X to self.X+cos(angle(self.X,self.Y,B.X,B.Y)*SPEED

    Sprite: Set X to self.X+sin(angle(self.X,self.Y,B.X,B.Y)*SPEED

    and if you want to be framerate independant it will look like

    sprite: Set X to self.X+cos(angle(self.X,self.Y,B.X,B.Y)*SPEED*dt

    Sprite: Set X to self.X+sin(angle(self.X,self.Y,B.X,B.Y)*SPEED*dt

    In fact, it's exactly what "moving at angle" does

    But it might lead to wiggling when the sprite reach the B point. Because you might not end up with B.X,B.Y precisely.

    That's why using lerp is probably better (: you get the framerate independancy and precision.

  • Yann

    I know my method isn't the better way to do it for being robust, but as Khaz specifically stated X axis, I figured it might be a simple fix if he's trying to minimize his events/calculations per second.

    You're right about the dt thing though, I should have used it in my events. Not sure if it'd fit in with my method of avoiding the wiggling (in the second set of events it will always stop within SPEED of the object)

    I'm not certain how much more processing power lerp takes, especially as games are usually lagged by their graphical side rather than logical though, so your method is probably the more correct one to use.

  • variable= move

    sprite set x to self.x<target.x ?self.x+1*dt :self.x-1

    <img src="smileys/smiley17.gif" border="0" align="middle" />

  • variable= move

    sprite set x to self.x<target.x ?self.x+1*dt :self.x-1

    <img src="smileys/smiley17.gif" border="0" align="middle" />

    Awesome.

  • Thanks, bros!

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