I’m making a card game. I made my card script I can move and place it. but now ım trying to fan my cards like real life but I have problems.
first I can’t move my cards properly. with code they are much less moving than the mouse it shouldnt be happen. and first 3 cards are at weird directions. and finally ı want to make adding cards smoother. here is the code → extends Node2D
Settings
var card_spacing = 100
var max_tilt = 10
var max_height = 100
var card_scene = preload(“res://scenes/card.tscn”)
Arrange cards in a fan shape
func _process(delta):
var cards = get_children()
var num_cards = cards.size()
if num_cards == 0:
return
var total_width = (num_cards - 1) * card_spacing
var start_x = -total_width / 2
for i in range(num_cards):
var card = cards[i]
var normalized_x = float(i) / max(1, num_cards - 1)
card.position = Vector2(start_x + i * card_spacing, max_height * (4 * (normalized_x - 0.5) ** 2 - 1))
card.rotation_degrees = lerp(-max_tilt, max_tilt, normalized_x)
Is it necessary to do this mathematically in code?
I have done this using a brute-force method.
I create a landing zone or pan where the cards sit.
I arranged the cards the way I like them and hard code as constants the x,y and rotation.
Do this for every hand possibilty, 1 card, 2 cards, 3 cards etc.
With a small number of cards per hand this does not take long nor does it eat up significant memory.
You can move the pan anywhere and since the x,y is relative to the pan it moves with it.
Each seat is a duplicate of the pan.
Coincidentally, I just watched a tutorial touching upon this exact topic! It’s “Transforms in Godot” by Godotneers on YouTube. It’s a video that broadly covers transforms in both 2D and 3D, but very briefly in the 2D section (starting at timestamp 32:30), he goes over how to easily fan out cards for a card game.
There is also another way of doing this, which is sampling Curve2D export properties and rotating and moving it and applying in code as needed (example tutorial). I am doing this for my own game, and it gives me full control on how to things are layered out and such, although you might want something more rigid depending on your use-case.