Get help using Construct 2

### » Mon Dec 15, 2014 1:11 am

Okay everyone. The answer to this might be easy or not...

Given an object with the following variables:

Velocity X
Max Speed
Rate of acceleration and deceleration
Distance to target

How do I know when to decelerate to arrive at the target and have 0 velocity when I do. Basically I have an object that needs to accelerate towards a target and then begin decelerating at the right time.

I Was looking around and found some physics equations that give me things like distance = Acceleration * time ^2 but that doesn't help much... position = position + velocity is how velocity integration works in most games and that gives a different number than physics equations (euler explicit integration???) I don't know what I am talking about but all I know is I spent 3 hour trying to figure out how to calculate when to slow down with no luck and google didn't help
B
33
S
11
G
2
Posts: 563
Reputation: 5,141

### » Mon Dec 15, 2014 2:04 am

it's dependent on your rate of deceleration, current speed, and distance to target.

you haven't given enough information as to how exactly you want this to work, is the deceleration something that can change with time? that makes things much more complicated, and would necessitate some kind of PID loop to make an estimation of how to alter the velocity. If your deceleration is constant its simple algebra.

for the pid loop you'd have to alter velocity based on the current speed and distance to target and some value to control sensitivity. You'd need to carefully tune things not to overshoot. If you know deceleration will be constant for the entirety of the stopping motion you just need to solve using an appropriate kinematic equation, depending on how far you want to move before stopping / what constant rate you wish to decelerate. in each of these cases you'd need to do different things. If you had a specified stopping rate you'd need to find at what distance to begin decelerating. if you wanted to stop at a particular point from any distance to target you'd need to solve at that instant what constant rate to decelerate at until velocity is zero and the target has been reached.
B
75
S
13
G
8
Posts: 1,973
Reputation: 9,841

### » Tue Dec 16, 2014 12:17 am

Search for "kinematics projectile motion" in Google.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822

### » Tue Dec 16, 2014 2:05 am

not sure if this will help or not:

https://www.scirra.com/tutorials/1051/b ... cing-games
B
8
S
1
Posts: 72
Reputation: 721

### » Tue Dec 16, 2014 7:36 am

You'll mainly be interested with the stopping distance which can be derived from this kinematic equation:
vf^2 = v0^2 - 2*a*d
where
vf is the final velocity
v0 is the initial velocity
a is the acceleration
d is the distance

We want to know how far it takes to stop with a given speed and deceleration. So:
speed^2 = 0^2 - 2*deceleration*distance

solving for distance we get:
distance = -(speed^2)/(2*deceleration)

Note: if the value you use for deceleration isn't negative you can omit the minus in the formula.

So then our pseudo code to move the object would be:
Code: Select all
`if( speed < max_speed) then accelerateif( distance_to_target <= -(speed^2)/(2*deceleration) then decelerate`
B
91
S
31
G
103
Posts: 5,235
Reputation: 67,756