Hey im currently programming on a game where you have to avoid asteroids(displayed as circles).
Here is the code how i create asteroids:
renderer = new ShapeRenderer();
RandomXS128 rand = new RandomXS128();
for (int i = 0; i < 20; i++) {
circlePositions.add(
new Vector2(
rand.nextInt(Gdx.graphics.getWidth()),
rand.nextInt(Gdx.graphics.getHeight())
)
);
circlePositions.add(
new Vector2(
rand.nextInt(Gdx.graphics.getWidth()),
rand.nextInt(Gdx.graphics.getHeight())
)
);
}
renderer.begin(ShapeType.Filled);
for (int i = 0; i < circlePositions.size; i++) {
Vector2 pos = circlePositions.get(i);
renderer.setColor(Color.GRAY);
renderer.circle(pos.x, pos.y, 50);
}
renderer.end();
How can i add random movement to the circles?
You can create speedx and speedy or random direction variables in circleposition class and give them random values. But you also should update x and y of circle.
For example when you creating circlePositions class also add direction parameter and create new method called update() in circlePosition class.
circlePositions.add(
new Vector2(
rand.nextInt(Gdx.graphics.getWidth()),
rand.nextInt(Gdx.graphics.getHeight()))
direction );
Set direction to random between 0 to 360 when calling. You can use libgdx's MathUtils.random() method.
circlePositions.add(
new Vector2(
rand.nextInt(Gdx.graphics.getWidth()),
rand.nextInt(Gdx.graphics.getHeight()))
MathUtils.random(360f) );
Change constructor as well.
public circlePosition(Vector2 position, float direction )
{
...
this.direction=direction;// now you have random direction stored in circlePosition class
}
In update() method of circlePosition class
void update()
{
x+=MathUtils.cosDeg(direction)*speed;//You can simply change speed by setting speeds value
y+=MathUtils.sinDeg(direction)*3.6f;//Or you can just use float
}
Dont forget to call circlePositions.update();