Good behaviour for ease movement?

Get help using Construct 2

Post » Thu Mar 02, 2017 2:37 pm

Currently I'm using lerp a lot for smoothing movement (to simulate easeIn/Out. Using a lot of them I'm wondering if there's any good behaviour that does the same thing? I'm guessing that using behaviours are a bit more performance friendly as well.

Any such behaviours available? I tried LiteTween but not doing exactly what i wanted as you has to specify a duration of the tween. I'm more looking for something to constantly smooth things like viewport scrolling, objects that follow the mouse pointers etc.

Any suggestions?

Thanks
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 992
Reputation: 12,656

Post » Thu Mar 02, 2017 4:20 pm

Cosine interpolation cosp(a, b, x) maybe

Theres also:
qarp(a, b, c, x)
cubic(a, b, c, d, x)
They are aggressively more complicated however.

Rex has several behaviors that are simpler to implement, moveto, spline, etc.
Image ImageImage
B
169
S
50
G
174
Posts: 8,331
Reputation: 110,806

Post » Fri Mar 03, 2017 10:15 am

newt wrote:Cosine interpolation cosp(a, b, x) maybe

Theres also:
qarp(a, b, c, x)
cubic(a, b, c, d, x)
They are aggressively more complicated however.

Rex has several behaviors that are simpler to implement, moveto, spline, etc.


I've tried both qarp and cubic, but not getting the desired effect.

When mouse left is down, i want the object to accelerate smoothly from 0 to a certain speed based on the distance to the mouse, then when mouse is up I want it to decelerate to zero speed.

using lerp(self.x,mouse.x,0.1) has the desired effect for slowing the object down as it is getting closer to Mouse.X. But the start of the movement is sudden (max speed from start), getting slower and slower as it approach Mouse.X. I don't know how I can smooth out the start of the movement.

I'm basically looking for a curve like this. I know how to do it based on time (but that's not what I'm after), but I don't know how to do it based on distance.
Image
The max speed of the object should be somewhere in the middle.

When i click. I want object to accellerate to a "Max speed" determined by the object current distance to mouse. Max speed should automatically get slower as the object approaches destination (Mouse)

I'm just guessing here.
lerp(a, b, x*m)

X has to be the max speed. (m) The modifier has to be a value, that is pretty close to zero, when the object starts to move and closer to 1 when it's approaching max speed.

a = current position, (not starting position as the mouse can move around)
b = target position, (the current position of the mouse)
x = maximum speed. (the maximum speed)
m = modifier (a value that smoothly scales from 0-1 based on distance where further is closer to 0 and closer is closer to 1)

maybe I'm doing it all wrong, and some math genius here can help me out?

lerp(self.X,mouse.X,0.1) has the desired effect except that the start of movement is snappy not accelerating.

I want the object to smoothly catch up to the mouse current position.
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 992
Reputation: 12,656

Post » Fri Mar 03, 2017 10:57 am

Basically a way to apply torque when the distance between mouse and object is far.
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 992
Reputation: 12,656

Post » Fri Mar 03, 2017 11:09 am

One way is to keep track of the velocity. Basically it starts at 0 and accelerates (ease in). Then you can calculate the distance it would take to decelerate to zero and use that the change the acceleration to decceleration (ease out).

Here's a link of the idea:
viewtopic.php?f=147&t=163491&p=989820&hilit=Stopping+distance#p989820
B
92
S
32
G
110
Posts: 5,295
Reputation: 71,501

Post » Fri Mar 03, 2017 11:30 am

R0J0hound wrote:One way is to keep track of the velocity. Basically it starts at 0 and accelerates (ease in). Then you can calculate the distance it would take to decelerate to zero and use that the change the acceleration to decceleration (ease out).

Here's a link of the idea:
viewtopic.php?f=147&t=163491&p=989820&hilit=Stopping+distance#p989820


Thank's rojo. I'll test that out....

Happen to know if there any plugin/behaviour that does the same thing? LiteTween is based on time, not distance, so that wasn't very useful in this case.
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 992
Reputation: 12,656

Post » Fri Mar 03, 2017 1:29 pm

@R0j0hound

I tried your code example It's nearly there but had some quirks. Object really never reaches Mouse, and starts orbiting the mouse pointer, or sometimes it shoots away randomly, after getting close. How would go about to stop the object to overshoot target, and start orbiting? I just need it to home in, then stay there.

https://dl.dropboxusercontent.com/u/20560446/Scirra/smoothchase.capx
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 992
Reputation: 12,656

Post » Fri Mar 03, 2017 1:42 pm

I found this javascript code, for a 2D chase camera... Maybe i can convert it to C2 events somehow.

float lerp = 0.1f;
Vector3 position = this.getCamera().position;
position.x += (Obj.x - position.x) * lerp * deltaTime;
position.y += (Obj.y - position.y) * lerp * deltaTime;
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 992
Reputation: 12,656

Post » Fri Mar 03, 2017 1:51 pm

Lerp the acceleration perhaps.
Mouse is down
->object has line of sight to mouse.x,mouse.y, set position to lerp(self.x,mouse.x,lerp(speed,minspeed,acceleration*dt)*dt) lerp(self.y,mouse.y,lerp(speed,minspeed,acceleration*dt)*dt)
->else set position to lerp(self.x,mouse.x,lerp(speed,maxspeed,acceleration*dt)*dt) lerp(self.y,mouse.y,lerp(speed,maxspeed,acceleration*dt)*dt)

That is with the line of sight behavior on the object, and an else to the los event.
Of course that wouldn't cover when the mouse was up.
Might need a boolean to signal that if you want it to continue to where the mouse was.
Image ImageImage
B
169
S
50
G
174
Posts: 8,331
Reputation: 110,806

Post » Fri Mar 03, 2017 4:56 pm

@newt

Say, we use that to make a sprite hunt the mouse.
Lets start the sprite far away from the mouse, and lets not move the mouse.
The sprite will now 'catch' up with the LineOfSight kicking in at one point.
If we now move the mouse, the LineOfSight is still kicking in, while you really want the speed (starting speed) defined in the else. (if i understand you right). So we start at maxspeed.

Also ...
When the lerp is written as lerp(positionSprite,positionMouse,lerpfactor) ....
positionSprite & positionMouse are dynamic. So the lerpfactor is altering the next position allready when it is static.
Make also the lerpfactor dynamic and i suspect that there will be weird feedback (but i did not try it)
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Next

Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, MeteoorSC, TRMG and 23 guests