[SOLVED] Get a body to spawn randomly in the camera's view?

Anything libgdx related goes here!

[SOLVED] Get a body to spawn randomly in the camera's view?

Postby Potassium » Thu Jun 14, 2018 11:18 am

I need help getting a box2d body's Y location to be set at random within the camera's view. The game I'm programming is a mix between an infinite runner and flappy bird where the camera follows the player when they jump. I want to make obstacles spawn randomly on the y-axis of the camera or viewport so that where they come from is random but in the view of the player no matter what altitude the player is at. Using "Gdx.graphics.getHeight" doesn't result in an error and works but is useless, using "gameScreen.viewport.getWorldHeight() or "gameScreen.viewport.getScreenHeight" results in a null object error, and the camera doesn't seem to have any methods that fit my agenda. Any help would be greatly appreciated.

Code: Select all
public class ObstacleOne extends Sprite {

    private World world;
    public Body b2body;
    private FixtureDef fdef;
    Random randomYposition = new Random();
    private GameScreen gameScreen;


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

    }

    public void defineObstacleOne(){
        BodyDef bdef = new BodyDef();
        bdef.position.set(500 / RadiationPigeon.PPM, randomYposition.nextFloat() + gameScreen.vieport.getWorldHeight());
        bdef.type = BodyDef.BodyType.KinematicBody;
        b2body = world.createBody(bdef);

        fdef = new FixtureDef();
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(60 / RadiationPigeon.PPM, 25 / RadiationPigeon.PPM);

        fdef.shape = shape;
        b2body.createFixture(fdef);
    }
}
Last edited by Potassium on Sun Jun 17, 2018 1:29 am, edited 3 times in total.
Potassium
 
Posts: 4
Joined: Thu Jun 14, 2018 9:16 am

Re: Set a Box2dbody to spawn randomly in the camera's view?

Postby tomski » Thu Jun 14, 2018 11:29 am

Code: Select all
Vector3 cameraPosition = camera.position;
float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
float effectiveViewportHeight = camera.viewportHeight * camera.zoom;

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

float randomXPos = MathUtils.random(minWorldX, maxWorldX);
float randomYPos = MathUtils.random(minWorldY, maxWorldY);
tomski
 
Posts: 820
Joined: Wed Jun 26, 2013 6:06 pm

Re: Set a Box2dbody to spawn randomly in the camera's view?

Postby Potassium » Thu Jun 14, 2018 12:02 pm

tomski wrote:
Code: Select all
Vector3 cameraPosition = camera.position;
float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
float effectiveViewportHeight = camera.viewportHeight * camera.zoom;

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

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


Thank you so much, tomski!
Potassium
 
Posts: 4
Joined: Thu Jun 14, 2018 9:16 am

Re: Set a Box2dbody to spawn randomly in the camera's view?

Postby Potassium » Sat Jun 16, 2018 8:06 am

tomski wrote:
Code: Select all
Vector3 cameraPosition = camera.position;
float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
float effectiveViewportHeight = camera.viewportHeight * camera.zoom;

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

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


I couldn't try out this code yesterday as I was too busy and I tried it today and it returns with yet another "null object reference" error in an attempt to read from "com.badlogic.gdx.graphics.OrthographicCamera com.doppelganger.radiation_pidgeon.Screens.GameScreen.pigeoncam"

Any Idea why? Any solutions?
Potassium
 
Posts: 4
Joined: Thu Jun 14, 2018 9:16 am

Re: Get a body to spawn randomly in the camera's view?

Postby evilentity » Sat Jun 16, 2018 10:43 am

Perhaps you should initialize it to something then
Looking for a freelancer? PM me!
evilentity
 
Posts: 4508
Joined: Wed Aug 24, 2011 11:37 am


Return to Libgdx

Who is online

Users browsing this forum: No registered users and 1 guest