0
\$\begingroup\$

I'm making a small game, and I'm trying to add this mechanic where if you click on an object and move it, then you release the object it will snap back to it's original position. Currently, each object has a Vector2f, and I'd prefer that when you release it, it eases back to it's position, is this possible? I've tried to use atan2, but it doesn't work, since it only calculates an angle then keeps moving towards the angle, and doesn't stop.

Here's the code I have for it so far. When the object is released, the initialPosition Vector2f is set to the position that the object is released.

if (moveBack) {
    float angle = (float) Math.atan2(position.y - initialPosition.y, position.x - initialPosition.x);
    position.x += Math.sin(Math.toRadians(angle));
    position.y += Math.cos(Math.toRadians(angle));
}
\$\endgroup\$
0
\$\begingroup\$

There is no need to calculate the direction, instead you can use interpolate between the two positions. Interpolation is when you calculate a position between start and end using an interpolant which is 0 at the start and 1 at the end.

The simplest of these is linear interpolation, often shortened to lerp. Many game api's has this function somewhere but you didn't mention which one you use, it is very easy to write yourself though:

public float lerp(float start, float end, float interpolant) {
  return (1-interpolant) * start + interpolant * end;
}

As you can see, when the interpolant is 0 start is multiplied with 1 and end with 0. And the reverse for when interpolant is 1. And for a Vector2f:

public float lerp(Vector2f start, Vector2f end, float interpolant) {
  return new Vector2f(
    (1-interpolant) * start.x + interpolant * end.x,
    (1-interpolant) * start.y + interpolant * end.y)
}

Now you just need to store the point the item was let go from and the time remaining for the interpolation, something like this:

if (moveBack) {
  moveBackTime += timeSinceLastFrame;
  if (moveBackTime <= 1f)
    position = lerp(droppedPosition, initialPosition, moveBackTime);
  else {
    moveBack = false;
    position = initialPosition;
  }
}
\$\endgroup\$
3
  • \$\begingroup\$ What would the droppedPosition be in this case? I've tried implementing this, and it snaps back, however when I try and drag the object again, it just stays in the same position. EDIT: duhh, obviously droppedPosition is the position from when the object is dropped. \$\endgroup\$ – chapman Feb 17 '15 at 21:35
  • \$\begingroup\$ So with that in mind, what does the initialPosition represent, since the initialPosition in my case is the droppedPosition. \$\endgroup\$ – chapman Feb 17 '15 at 21:35
  • \$\begingroup\$ @freefouran I assumed you used initialPosition as the position the object was in when the drag started and that is exactly how I used it. The lerp function interpolates from droppedPosition to initialPosition over time \$\endgroup\$ – Daniel Carlsson Feb 18 '15 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.