Paddle Stretch in Pong Clone

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BFGames
:warning: Old Version Published before Godot 3 was released.

I’m creating a pong clone in Godot 2.1.4 and i’m trying to ‘juice it’ with some simple animation. One part of that is trying to create a paddle-stretch style animation, so that the scale is altered the faster it moves.

I’ve tried implementing this with tweens using the current and next positions of the paddle (controlled by mouse), but it seems to do nothing, and i’m not sure whether this is because i’m barking up the wrong tree, or because it’s just updating too fast.

Does anyone have any advice to achieve this? Should I try something like a secondary sprite instead, which lags slightly behind using a timer on a noticeable delay instead? It won’t have the same fluidity but it should at least better signify speed.

:bust_in_silhouette: Reply From: mollusca

I’m not sure a Tween works that well for something like this (or maybe I’m just using them wrong). Anyway, for a basic version you can calculate the scale based on the velocity:

extends Sprite

var prev_x = 0.0

func _ready():
    set_process(true)
    prev_x = get_global_pos().x

func _process(delta):
    var mouse_pos = get_global_mouse_pos()
    set_global_pos(Vector2(mouse_pos.x, 300))
    var vel = abs(mouse_pos.x - prev_x) / delta
    prev_x = mouse_pos.x
# use min and max to limit the scale to reasonable values
    var target_scale = Vector2(min(2, 1 + vel / 4000.0), max(0.2, 1 - vel / 8000.0))
    set_scale(target_scale)

Smoothing the scaling a bit might look nicer:

set_scale(get_scale() + (target_scale - get_scale()) * 0.3)

I didn’t think it was that natural a fit either, but i wasn’t sure whether that was just my inexperience. Glad that you’ve corroborated at least.

That’s great, thank you. I had an inkling that i might need a speed or velocity variable to do this properly, but couldn’t work out how to implement it while using the mouse position to control movement.

I’ll try out your suggestion when i’m back home. Thanks for the help!

BFGames | 2017-11-23 17:09

Need to spend a bit of time tweaking it to my liking but it works perfectly! Thanks so much!

BFGames | 2017-11-23 20:51