# 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:

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.

You can use sin and cos to get the points each coin should be from the object. This example will create 3 coins around the exploding object.

    explodedItems = new Array<Vector2>(); // array to hold coin positions
Vector2 explodingObject = new Vector2(10,10); // object location that has coins explode from it
int amountOfItemsToExplode = 3; // amount of items to explode out
int distance = 5;
float angleIncrement = 2 * PI / amountOfItemsToExplode;
// use sin+cos to get points coins explode from
for(float i = 0; i < amountOfItemsToExplode; i++){
float x = (float) Math.sin(i * angleIncrement) + explodingObject.x;
float y = (float) Math.cos(i * angleIncrement) + explodingObject.y;
}


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.

For this, I'll use a circle:

1. Use a random number as the radius

2. Let's say you have x coins, the position of each one can be calculated like this:

new Vector2(cos(i * (360 / NumberOfCoins) + Offset) * Radius, sin(i * (360 / NumberOfCoins) + Offset) * Radius);

whereas Offset means how much to rotate the whole thing (can be used for more randomization and is in degrees) and i counts from 0 to NumberOfCoins - 1

Final Note: I'm not familiar with libgdx so the code I've written may need to be changed but the logic behind it is the same

• Wow, the question was asked in 2017?? didn't notice that😅 Commented Mar 23, 2022 at 22:16