Best way to move from A to B

Get help using Construct 2

Post » 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
11
S
3
G
2
Posts: 110
Reputation: 2,410

Post » 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
29
S
12
G
7
Posts: 740
Reputation: 7,839

Post » 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
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » 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 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » 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
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » 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 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Sat Mar 03, 2012 5:49 am

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

Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » 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 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Sat Mar 03, 2012 11:06 pm

Thanks, bros!
B
11
S
3
G
2
Posts: 110
Reputation: 2,410


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests