# Best way to move from A to B

Get help using Construct 2

### » Sat Mar 03, 2012 12:31 am

What's the best method to move an object on the X axis from A to B?

lerp?

Khaz2012-03-03 00:40:30
B
13
S
4
G
3
Posts: 120
Reputation: 3,080

### » Sat Mar 03, 2012 1:01 am

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.
B
30
S
13
G
7
Posts: 740
Reputation: 8,099

### » Sat Mar 03, 2012 2:38 am

[code]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[/code]

if you want a constant movement
Yann2012-03-03 02:48:57
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

### » Sat Mar 03, 2012 4:07 am

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 - SPEEDJayjay2012-03-03 04:09:54
Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
139
S
47
G
19
Posts: 2,268
Reputation: 22,065

### » Sat Mar 03, 2012 4:33 am

@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.
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

### » Sat Mar 03, 2012 5:08 am

@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.
Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
139
S
47
G
19
Posts: 2,268
Reputation: 22,065

### » Sat Mar 03, 2012 5:49 am

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

B
175
S
50
G
199
Posts: 8,626
Reputation: 124,028

### » Sat Mar 03, 2012 7:04 pm

[QUOTE=newt] variable= move
sprite set x to self.x<target.x ?self.x+1*dt :self.x-1

[/QUOTE]

Awesome.
Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
139
S
47
G
19
Posts: 2,268
Reputation: 22,065

### » Sat Mar 03, 2012 11:06 pm

Thanks, bros!
B
13
S
4
G
3
Posts: 120
Reputation: 3,080