Moving Object from A to B

This forum is currently in read-only mode.
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • ok so this isnt really a construct question but a logic question and I can figure out how to do it in code. I cant use construct for this project but I figured some one would help me.

    So in the game I'm making right now I have a square that bounces around and when I want that square to head towards a certain point.

    so I have square.x, square.y and point.x point.y

    now I need to move the square towards point in a smooth transition at a set speed. call that "speed"

    Im pretty sure I would use lerp to do this but Im not sure how to implement it.....

    even if you tell me how do do it in event logic I can make it work in AS3 I dont have any forums to go to for Action Scirpt at the moment...

  • There are two ways. I think the right command is lerp, but the easier way in construct would be this:

    • Move at angle: angle(square.x, square.y, targetx, targety), pixels: speed*timedelta

    Hmm, using lerp for that does seem to be a bit trickier than I thought.

  • wow I must have been really sleepy last night to put this in feature requests sorry about that.....

    I can do a move at angle in action script. but there is no nice commade for it you have to do all the math for moving at an angle.

  • So your saying you don't want it to slow down when it gets close to the other point?

  • nope it should just go right to the point at a set speed.

  • Yeah I don't think you want lerp for that.

  • First off, you will need a timer. Add a private variable, name it 'timer'. You will also have to store original coordinates.

    + On Left Click

    + Square('timer')= 0 // this is to make sure the object isn't already moving





    Square('timer') = timedelta // this would initiate the move

    + Square('timer') larger than 0 and smaller than 1 // while moving

    Square: set position to X = lerp('OrigX','PointX','timer'), Y = lerp('OrigY','PointY','timer') // linear movement between the two points

    Square('timer') = 'timer' + timedelta // increasing the timer will make it move along the line, up to value of 1; this influences the speed, so you probably want to use a fancy equation to move at set speed. The value should be between zero and one.

    + Square('timer') equal to or greater than 1 // the square is at or exceeding the goal

    Square: set position to 'PointX', 'PointY' // this is to make sure it is at exact position, in case of overshooting

    Square('timer') = 0 // this basically stops and waits until you click somewhere again

    This may appear more complicated than the prior example, however you are at a liberty to define the movement pattern. For example, you could use various interpolation methods. This is the most robust method that makes sure the square reaches the EXACT location.

    As for the set speed, you'll have to increment the timer in a way it matches the desired speed. I think multiplying timedelta with desired speed should do the trick, but don't quote me on that.

  • Moved thread to Help/Tech

  • Thanks deadeye

  • Thought about a lerp solution to this. Its pretty much the same as Mipey's.

    square:  Value 'var' Less than 1
    System: Every square.Value('speed')*timedelta milliseconds

    squareSet position to lerp(square.X, point.X, square.Value('var')), lerp(square.Y, point.Y, square.Value('var'))

    squareAdd 0.01 to 'var'[/code:26kevabm]

    Done with two private variables, speed, and the percentage called var.

    Edit: For a smother transition make var smaller, and adjust speed accordingly.

  • why don't you just move at angle with cos and sin, I don't see the need for lerp if all you want to do is move towards a point at a set speed.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok, I'm confused now. Is the red line what you want to achieve?

    If so, qarp might be your best choice (a thread shows how to use it best, if I remember right)

  • Added two more variables original x, and original y. The first example actually would slow down since it was using current x,y.

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