mardi 16 novembre 2021

Math.random() returns the same number across instances [Java]

I'm trying to create a particle that wanders around by randomly rotating its velocity vector. I'm using LibGdx and Java 11.

This is my Cell class (I didn't want to name it particle):

package me.kolterdyx.testgame.custom;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;


public class Cell {

    private Vector2 vel;
    private float deviationFactor = 15f;

    public Cell(){
        this.vel = Vector2.X;
    }


    public void update(){
        float deviation = (float)(Math.random()*deviationFactor-deviationFactor/2f);
        vel.rotateDeg(deviation);
    }


}

This is my Main class:

package me.kolterdyx.testgame;

import com.badlogic.gdx.ApplicationAdapter;
import me.kolterdyx.testgame.custom.Cell;

public class Main extends ApplicationAdapter {
    Cell[] cells;

    @Override
    public void create () {
        cells = new Cell[2];
        for (int i = 0; i < cells.length; i++) {
            // Here I create a new instance for each index of the array
            cells[i] = new Cell();
        }
    }

    @Override
    public void render () {
        for(int i=0;i<cells.length;i++){
            //Here I update and display each cell
            Cell c = cells[i];
            c.update();
        }
    }

}

As you can see, I create two independent instances of the Cell class. However, when I run the project, all I can see is both cells following exactly the same path. They look like they are only one cell because they overlap perfectly. I've tried creating independent Random instances using the ID of each Cell as the seed, but that has only achieved a difference of about 1.2 between the random numbers, but they are still very close and the cells never separate. What could be causing this problem?

EDIT: More info

I have printed the deviation from two cells and their resulting velocity angle. I have set Cell 0 to always have a deviation of 0:

Cell 0: Deviation=-0.0 | New velocity angle=0.0
Cell 1: Deviation=-3.7252638 | New velocity angle=356.27475
---
Cell 0: Deviation=-0.0 | New velocity angle=356.27475
Cell 1: Deviation=-1.5686669 | New velocity angle=354.70605
---
Cell 0: Deviation=0.0 | New velocity angle=354.70605
Cell 1: Deviation=3.9682944 | New velocity angle=358.67438
---
Cell 0: Deviation=-0.0 | New velocity angle=358.67438
Cell 1: Deviation=3.6621578 | New velocity angle=2.3365214
---
Cell 0: Deviation=-0.0 | New velocity angle=2.3365214
Cell 1: Deviation=-10.350971 | New velocity angle=351.98553
---
Cell 0: Deviation=0.0 | New velocity angle=351.98557
Cell 1: Deviation=-8.096683 | New velocity angle=343.88885
---
Cell 0: Deviation=-0.0 | New velocity angle=343.88885
Cell 1: Deviation=11.823002 | New velocity angle=355.71188
---
Cell 0: Deviation=-0.0 | New velocity angle=355.71188
Cell 1: Deviation=5.809546 | New velocity angle=1.5214165

For some reason, the deviation value of Cell 1 is added to the velocity angle of both Cells, even though the deviation value is created locally inside the Cell instance in one of its methods.




Aucun commentaire:

Enregistrer un commentaire