mardi 19 juin 2018

Body does not spawn randomly despite code previously working

In my game objects are meant to spawn randomly along the y-axis of the camera's view, and come across the screen in an infinite runner style. It took me a while to learn to do this and upon running it worked. Later I made a new method (Respawn) and ever since the body always spawns in the exact same place even though I haven't changed ANY code with the random, any idea why? I've obviously changed something without realising but I have no idea what I did, any help would be greatly appreciated.

(Pigeoncam is my OrthographicCamera) (Only relevant code is below)

Code: `

Vector3 cameraPosition = pigeoncam.position;

float effectiveViewportHeight = pigeoncam.viewportHeight * pigeoncam.zoom;

float minWorldY = cameraPosition.y - effectiveViewportHeight/2f;
float maxWorldY = cameraPosition.x + effectiveViewportHeight/2f;

float randomYPos = MathUtils.random(minWorldY, maxWorldY);


public ObstacleOne(GameScreen screen){
    this.world = screen.getWorld();
    defineObstacleOne();
}

public void update() {

  b2body.setLinearVelocity(-4f, 0);
  Respawn();

}

public void Respawn(){
    if(b2body.getPosition().x <= 10 / RadiationPigeon.PPM){
        b2body.setTransform(6.5f, randomYPos, 0);
    }
}
public void defineObstacleOne(){
    BodyDef bdef = new BodyDef();
    Vector2 set = bdef.position.set(6.5f, randomYPos);

At first, I thought I messed something up when I added the Respawn() method but even when I get rid of it the body won't stop spawning at the same place every time. This is where it respawns (It's the blue rectangle)




Aucun commentaire:

Enregistrer un commentaire