Search code examples
objectlibgdx

object array positioning-LibGdx


In my game,if I touch a particular object,coin objects will come out of them at random speeds and occupy random positions.

public void update(delta){
if(isTouched()&& getY()<Constants.WORLD_HEIGHT/2){
         setY(getY()+(randomSpeed * delta));
         setX(getX()-(randomSpeed/4 * delta));
         }
  }

Now I want to make this coins occupy positions in some patterns.Like if 3 coins come out,a triangle pattern or if 4 coins, rectangular pattern like that.

I tried to make it work,but coins are coming out and moved,but overlapping each other.Not able to create any patterns. patterns like: enter image description hereenter image description here

This is what I tried

int a = Math.abs(rndNo.nextInt() % 3)+1;//no of coins
        int no =0;

        float coinxPos = player.getX()-coins[0].getWidth()/2;
        float coinyPos = player.getY();
        int minCoinGap=20;
        switch (a) {
        case 1:
            for (int i = 0; i < coins.length; i++) {
                if (!coins[i].isCoinVisible() && no < a) {
                    coins[i].setCoinVisible(true);

                    coinxPos = coinxPos+rndNo.nextInt()%70;
                    coinyPos = coinyPos+rndNo.nextInt()%70;
                    coins[i].setPosition(coinxPos, coinyPos);
                    no++;
                }
            }
            break;
        case 2:
            for (int i = 0; i < coins.length; i++) {
                if (!coins[i].isCoinVisible() && no < a) {
                    coins[i].setCoinVisible(true);
                    coinxPos = coinxPos+minCoinGap+rndNo.nextInt()%70;
                    coinyPos = coinyPos+rndNo.nextInt()%150;
                    coins[i].setPosition(coinxPos, coinyPos);
                    no++;
                }
            }
           break:
          ......
          ......
          default:
            break;

may be this is a simple logic to implement,but I wasted a lot of time on it and got confused of how to make it work. Any help would be appreciated.


Solution

  • In my game, when I want some object at X,Y to reach some specific coordinates Xe,Ye at every frame I'm adding to it's coordinates difference between current and wanted position, divided by constant and multiplied by time passed from last frame. That way it starts moving quickly and goes slowly and slowly as it's closer, looks kinda cool.

    X += ((Xe - X)* dt)/ CONST;
    Y += ((Ye - Y)* dt)/ CONST;
    

    You'll experimentally get that CONST value, bigger value means slower movement. If you want it to look even cooler you can add velocity variable and instead of changing directly coordinates depending on distance from end position you can adjust that velocity. That way even if object at some point reaches the end position it will still have some velocity and it will keep moving - it will have inertia. A bit more complex to achieve, but movement would be even wilder.

    And if you want that Xe,Ye be some specific position (not random), then just set those constant values. No need to make it more complicated then that. Set like another constat OFFSET:

    static final int OFFSET = 100;
    
    Xe1 = X - OFFSET; // for first coin
    Ye1 = Y - OFFSET;
    
    Xe2 = X + OFFSET; // for second coin
    Ye2 = Y - OFFSET;
    
    ...