# Moving Object from A to B

### » Sat Nov 28, 2009 5:03 am

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...
B
5
S
2
G
4
Posts: 632
Reputation: 2,829

### » Sat Nov 28, 2009 2:50 pm

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.
Moderator
B
96
S
34
G
33
Posts: 3,007
Reputation: 27,921

### » Sat Nov 28, 2009 3:13 pm

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.
B
5
S
2
G
4
Posts: 632
Reputation: 2,829

### » Sat Nov 28, 2009 3:22 pm

So your saying you don't want it to slow down when it gets close to the other point?
B
178
S
50
G
206
Posts: 8,686
Reputation: 127,715

### » Sat Nov 28, 2009 3:24 pm

nope it should just go right to the point at a set speed.
B
5
S
2
G
4
Posts: 632
Reputation: 2,829

### » Sat Nov 28, 2009 3:49 pm

Yeah I don't think you want lerp for that.
B
178
S
50
G
206
Posts: 8,686
Reputation: 127,715

### » Sat Nov 28, 2009 3:51 pm

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('OrigX')=Square.X
> Square('OrigY')=Square.Y
> Square('PointX')=MouseX
> Square('PointY')=MouseY
> 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.
B
62
S
21
G
13
Posts: 1,910
Reputation: 13,685

### » Sat Nov 28, 2009 9:23 pm

Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

### » Sat Nov 28, 2009 9:57 pm

B
5
S
2
G
4
Posts: 632
Reputation: 2,829

### » Sun Nov 29, 2009 12:37 am

Thought about a lerp solution to this. Its pretty much the same as Mipey's.
[code:26kevabm]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'))

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

[url:26kevabm]http://dl.dropbox.com/u/666516/lerpnoslow.cap[/url:26kevabm]

Edit: For a smother transition make var smaller, and adjust speed accordingly.
B
178
S
50
G
206
Posts: 8,686
Reputation: 127,715

Next