I have been trying to replicate the effect of a card tilting in the direction you drag it, similar to games like marvel snap and MTG arena. At first I thought it would be as simple as getting a vector for the mouse's acceleration and then converting that to the rotation on the card.
However that last part was much easier said than done as this is the first time I have done anything in 3D. After much trial and error (and failing to understand quaternions), the best solution I could come up with is defining a rotation for left, right, up, down, and center and then interpolating between them based on the acceleration.
While my solution gets the job done (sort of), I would love to know how a proper card game gets this effect.
This is what my solution looks like
var up: Quaternion = Quaternion(0.0871557, 0.0, 0.0, 0.9961947)
var down: Quaternion = Quaternion(-0.0871557, 0.0, 0.0, 0.9961947)
var left: Quaternion = Quaternion(0.0, 0.0, -0.0871557, 0.9961947)
var right: Quaternion = Quaternion(0.0, 0.0, 0.0871557, 0.9961947)
var center: Quaternion = Quaternion(0.0, 0.0, 0.0, 1.0)
var acceleration: Vector2 = Vector2.ZERO
func _physics_process(delta: float) -> void:
acceleration = get_mouse_acceleration()
var card_basis: Quaternion = Quaternion(Card3D.transform.basis)
if (acceleration == Vector2.ZERO):
card_basis = card_basis.slerp(center, delta)
elif (abs(acceleration.x) > abs(acceleration.y)):
if (acceleration.x < 0.0):
card_basis = card_basis.slerp(right, delta)
else:
card_basis = card_basis.slerp(left, delta)
else:
if (acceleration.y < 0.0):
card_basis = card_basis.slerp(down, delta)
else:
card_basis = card_basis.slerp(up, delta)
Card3D.transform.basis = Basis(card_basis)