0
votes

I am creating the card game and i wanted to order the cards as a bezier curve i've ordered the cards with this formula Quadratic Bézier curve formula

but now I have to get closer to each other as the number of cards decreases. Is someone can help me?

    public void SetCardPositions()
{
    Array.Clear(Cards,0,Cards.Length);
    for (int i = 0; i < transform.childCount; i++) {
        Cards[i] = transform.GetChild(i).gameObject;
    }

    int CardCount = Cards.Count(x => x != null); ;
    Transform[] CardInPlayerHand = new Transform[] {};

    for (int i = 0;i < CardCount; i++)
    {
        System.Array.Resize(ref CardInPlayerHand, CardInPlayerHand.Length+1);
        CardInPlayerHand[i] = Cards[i].transform;
    }
    //SetCardPositions
    for (int i = 0; i < CardInPlayerHand.Length; i++)
    {
        float t = 0;
        if (i != 0)
            t = ((1f / (CardInPlayerHand.Length - 1f)) * (float)i) + ;

        if (CardInPlayerHand.Length == 1)
            t = 0.5f;


        Vector3 CardOrigin = (1 - t) * ((1 - t) * DeckPointPositions[0].position + t * DeckPointPositions[1].position) + t * ((1 - t) * DeckPointPositions[1].position + t * DeckPointPositions[2].position);

        CardInPlayerHand[i].DOMove(CardOrigin,1);
    }
}
2
Add meaningful piece of your code or at least a picture to describe your problem betterMBo
i edited and shared the codes tooBuğrahan Özkan
I suggest catmull–rom splines. Really simple and easy to use, along with this plugin: pixelplacement.com/itween/index.phprustyBucketBay
I didn't knew it.Its really good but using the whole library for one thing in the game will be useless.Buğrahan Özkan

2 Answers

1
votes

So you distribute cards over whole Bezier curve range t=0..1 (except for Length=1 case).

To draw less cards, you can center at t=0.5 and limit t range. If Length is limited (for example, 5 for poker), things are simple - just make 1/4 steps.

But if you might show large card pile, it is worth to dynamically change step. When number of card is large, range should be close to 1, when small - range should be less.

For demonstration I choose exponent(1-exp(-x)) that tends to 1 when x grows. Example is in Python but I hope that approach should be clear. Example shows card positions for different number of cards used (n). Parameter k might be adapted for better view.

import math
k = 0.3
for n in range(1, 11):
    print(n)
    rng = 1 - math.exp(k*(1-n))
    first = (1 - rng)/2
    if n > 1:
        step = rng / (n-1)
    else:
        step = 0
    for i in range(n):
        t = first + i * step
        print(f"{t:.3f}  ", end="")
    print()

1
0.500  
2
0.370  0.630  
3
0.274  0.500  0.726  
4
0.203  0.401  0.599  0.797  
5
0.151  0.325  0.500  0.675  0.849  
6
0.112  0.267  0.422  0.578  0.733  0.888  
7
0.083  0.222  0.361  0.500  0.639  0.778  0.917  
8
0.061  0.187  0.312  0.437  0.563  0.688  0.813  0.939  
9
0.045  0.159  0.273  0.386  0.500  0.614  0.727  0.841  0.955  
10
0.034  0.137  0.241  0.345  0.448  0.552  0.655  0.759  0.863  0.966 
...
52
0.0000  0.0196  0.0392  0.0588  0.0784  0.0980  0.1176  0.1373  0.1569  0.1765  
0.1961  0.2157  0.2353  0.2549  0.2745  0.2941  0.3137  0.3333  0.3529  0.3725
0.3922  0.4118  0.4314  0.4510  0.4706  0.4902  0.5098  0.5294  0.5490  0.5686
0.5882  0.6078  0.6275  0.6471  0.6667  0.6863  0.7059  0.7255  0.7451  0.7647 
0.7843  0.8039  0.8235  0.8431  0.8627  0.8824  0.9020  0.9216  0.9412  0.9608 
0.9804  1.0000 
0
votes

Now its works like a charm! Thanks a lot.

//SetCardPositions
    float t = 0;
    float range = 1 - Mathf.Exp(k*(1- cardcount));
    float first = (1 - range) / 2;

    float step;
    if (cardcount > 1)
        step = range / (cardcount - 1);
    else
        step = 0;

    for (int n = 0; n < cardcount; n++) {
        t = first + n * step;
        Vector3 CardOrigin = (1 - t) * ((1 - t) * DeckPointPositions[0].position + t * DeckPointPositions[1].position) + t * ((1 - t) * DeckPointPositions[1].position + t * DeckPointPositions[2].position);

        CardInPlayerHand[n].transform.position = CardOrigin;
    }