Height and width in world units to set vectors

Hi, I’m trying to make a Brick Bricker game,
need to know how to get camera width and height in world units to send the ball always to accurate edges in all screen sizes dynamically, i tried and also read docs didn’t get any clue, i used some values by my self to just go on but now need to make it accurate,
also i need another help at first i used to make singleton and use it in other class for sending rootNode and … . but i found out about it is very wrong soon, and used app-states i used a class name BallState and extended with BaseAppState Class as below, so is it good design paradigm with engine or am i completely wrong, i got it about states from documentations and a maze-game in github.
everything is so much primitive at a moment, but i want to go a lot further with game so any idea may help will appreciate it so much. view is locked at defaults and no fly cam,

/**
 * The type BallState which will define Ball mesh,geo,update logic,and attach it to node.
 */
public class BallState extends BaseAppState {
    private float xSpeed = 0.1f;
    private float ySpeed = 0.1f;
    private SimpleApplication app;
    private Sphere ballMesh;
    private Material ballMat;
    private Geometry ballGeo;


    @Override
    protected void initialize(Application application) {
        app = (SimpleApplication) application;
        ballMesh = new Sphere(32,32, 0.1f);
        ballGeo = new Geometry("ball", ballMesh);
        ballMesh.setTextureMode(Sphere.TextureMode.Projected);
        //TangentBinormalGenerator.generate(ballMesh);
        ballMat = new Material(app.getAssetManager(),
                "Common/MatDefs/Misc/Unshaded.j3md");
        ballMat.setColor("Color", ColorRGBA.randomColor());
        ballGeo.setMaterial(ballMat);
        ballGeo.setLocalTranslation(0,-3,0);
        ballGeo.rotate(1.6f, 0, 0);
        app.getRootNode().attachChild(ballGeo);
    }
    
    @Override
    public void update(float tpf) {
        float x = ballGeo.getLocalTranslation().getX();
        float y = ballGeo.getLocalTranslation().getY();
        //System.out.println(x);
        //System.out.println(y);
        x += xSpeed;
        y += ySpeed;
        
        float wt = 5.3333335f;
        
        if (x < -wt || x > wt) {
            xSpeed = -xSpeed;
        }
        if (y < -4 || y > 4) {
            ySpeed = -ySpeed;
        }
        ballGeo.setLocalTranslation(x,y,0);
    }

    @Override
    protected void cleanup(Application application) {

    }

    @Override
    protected void onEnable() {

    }

    @Override
    protected void onDisable() {

    }
}

I used AbstractControl and moved code from AppState update method, but i won’t change this for anyone who can helps me on how to get camera width and height in world units, as example: when i get cam.height it is 600, but i can’t set it float:wt to limit my ball and setLocalTranslation to 600, so what i do?

Are you making a 2D game or a 3D game?

For a 3D game “screen size in world space” is unanswerable as it will depend on the distance from camera.

But for 2D game it is easy… use the guiNode instead of root node and set all of your sizes in pixels.

1 Like

Use vector math to give the ball direction and speed. I made a brick breaker game with jME last year. It is available on itch.io. I suggest you to read up on vector math if you’re not very familiar with it.

Edit:

That’s correct. So if the camera is fixed in one position and objects are always at the same distance from it (as I think it should be in this kind of game), screen size is not something you have to worry about.

1 Like

thank you for your answers i gonna fix it, also it’s 3D game

1 Like