How do I stop lerp?

Get help using Construct 2

Post » Tue May 10, 2016 8:43 am

I'm using lerp quite heavily and while using the debug i can see that some objects actually never stop. (They just move too small steps to be noticeable) My guess is that this could cause a minor performance hit, so how do I stop the lerp when it's close to the destination, like half a pixel or so?

And is there a good way to limit decimals in calculations?
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,060
Reputation: 14,054

Post » Tue May 10, 2016 9:17 am

What is the original formula you are using ? What are you lerping, and how ?

Depending on the situation, you can try round(lerp()).
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Tue May 10, 2016 9:53 am

Just basic movement of an sprite, towards another sprite.

lerp(self.x, object2.x, 0.1)

will round actually stop it? i was thinking more of having a condition to make the event false if it was close enough, if the distance between object1 and object2 is less than a certain amount ... The point is that I'm trying to optimize for a lot of objects, so i don't want heavy calculations.

maybe compare condition like this would be suffice, but I'm not sure if distance calculations for a lot of objects is very performance friendly?
distance(object1.x, object2.x) > 0.5
lerp(self.x, object2.x, 0.1)

Or maybe it would be better to add a variable to object 1 which holds the last x postion, then checking if last position x (minus) current position x is less 0.5? or something like that, but I guess i always have to return a positive value then.
abs(object1.lastpos-object2.x) > 0.5
lerp(self.x, object2.x, 0.1)
set object1.lastpos to self.x

or something like that? lol

Just specilating Maybe that would work... better try.
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,060
Reputation: 14,054

Post » Tue May 10, 2016 10:12 am

or something like that?

Image
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Tue May 10, 2016 10:29 am

korbaach wrote:or something like that?

Image


yeah that could work. :D

I tried that but it seemed that Sprite.X never reached exactly 300. it was 299.249859284752975029478520938752095786209587625

but i didn't think of rounding in the condition :p round(sprite.x) = 300

way easier.
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,060
Reputation: 14,054

Post » Tue May 10, 2016 12:45 pm

Thats is because you should lerp from start postion to end position. Now you lerp from a in-between-position to the end position. Sprite.X or Self.X is constanty updating. You create a Zeno paradox.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Tue May 10, 2016 5:44 pm

Two ways I use regularly.

Store start and end position in variable and lerp from 0 to 1 with a dt counter/timer for a true lerp (linear).

Or wrap the fixed step lerp based on current position to destination with ceil() or floor() depending if incrementing or decrementing, for an eased motion. Usually have to make a sub event condition for both because round() only works when lerping with a factor greater than 0.5 per tick.


Another way I don't use normally is to have the lerp event only run when not within 1-2 pixels of the destination as a condition. I don't use this because I generally like more precise control of the final resting position.
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083


Return to How do I....?

Who is online

Users browsing this forum: max2612 and 23 guests