How to get inertia without physics?

Not favoritedFavorited Favorited 0 favourites
  • 6 posts
From the Asset Store
In this template the music plays without looping in your game
  • Imagine this: an automatic sniper scope [in form of a cross on the screen] is following the moving object [platform]. The scope is getting closer to it and in given time it will reach the object and shot can be fired. This can be achieved by simple decreasing the distance in relation to moving object. But how to apply inertia to it, when the object decides to suddenly change direction? The preferable result would be that scope will still continue to follow the previous direction of objects movement for a short time and then again follow the object while reducing the distance.

    In other words - the object and the scope are connected by invisible rubber band which shortens in given time...

    Is it possible to achieve this without using the physics?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I believe you can do this with MoveTo behavior - set something like speed=200, acceleration=100, deceleration=50. And on every tick move the scope to the object position.

  • Thanks for the answer, but MoveTo has one flaw. When the object is moving, its coordinates are changing every tick and MoveTo will never reach the object if it runs constantly.

  • You mentioned a rubber band so you could do that with a damped spring. Basically add two variables for xy velocity: vx,vy. Then do the following. You can tune it by changing the 0.005 and 0.1 values. You can use any value from 0 to 1. The first value is the strength of the spring and the second is the amount of damping to apply.

    Compare: dt > 0

    -- add -0.005*(scope.x-target.x)/dt-0.1*vx to vx

    -- add -0.005*(scope.y-target.y)/dt-0.1*vy to vy

    -- scope: set position to (scope.x+vx*dt, scope.y+vy*dt)

  • R0J0hound Still if the target moves slowly, the scope will be lagging behind and never reach it. I tried to make a sort of predictable aim - position the scope slightly in front of the target - but couldn't make it right.

  • So a damped spring works well for getting the position to match but since the target is moving we probably want to match the velocity too.

    If you assume the target is moving at a constant velocity or with a constant acceleration it’s fairly easy to predict where it will be in the future. Ideally you’d take the predicted path and the path of the crosshairs and solve it so that they cross paths at the same time. But I can think of a few reasons why that would be hard to calculate.

    Alternately maybe we could modify the formula a bit. It’s basically -spring*dx/dt-damping*velocity. So the damping is applied to the total velocity. Instead we could damp by the relative velocity. That would let it settle on the path the target is moving instead of lagging behind. So something like this? I haven’t tested it.

    Scope: Add -0.005*(scope.x-target.x)/dt-0.1*(scope.vx-target.vx) to vx

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)