Efficiently smoothing a 2D card-tilting effect

Godot Version

v4.5.stable.official [876b29033]

Question

I have a card (Node2D) and when I drag it fast in the y-direction, I want to decrease its y-scale to give the impression of the card tilting in the 3rd dimension.

However, if I base the y-scaling only off of the current y-velocity, the animation is way too sharp & sudden and doesn’t look good.

In plain and simple terms, I’m trying to achieve something similar to Hearthstone’s card tilting animation when dragging around a card from the hand.

Looking to optimize for performance.

I am contemplating a few approaches:

  • Store velocity hist in GDScript Array

    • Store velocities in a GDScript Array and then .pop_front() any velocity older than X seconds.

    Disadvantage

    • .pop-front() shifts each index on every execution, so it is O(n) time.
  • Velocity hist tracking via C# queue

    • Same concept as above, but leverage C# queues. I think O(1) time?

    Disadvantage

    • Is it possible to mix C# and GDScript? Is it expensive?
  • Execute y-scaling at a fixed scaling units / sec speed and only trigger new y-scaling if not busy

    • Calculate desired duration based on scaling speed and pass it to a Tween, then only execute a new Tween if current Tween not busy. O(1) time. O(1) time.

    Disadvantage

    • Is it possible to track whether property Y of object X is currently being Tweened?

Am I overthinking this? Is there something built-in to solve such a problem that I’m not aware of?

Definitely overthinking :slight_smile:

Just use simple damping. Calculate the wanted scale each frame as you’re doing now but instead of assigning it directly nudge the actual scale towards the wanted scale to a some predefined percentage of their difference. The smaller the percentage the less abrupt the transition will be. Adjust that percentage number to what feels good for your game.

Ahh right! Duh

Thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.