Width and height from pixels to meters

Hi all, I am trying to make a class which will help me convert the world co-ordinates to pixels and vice versa, I have been having alot of problems with the width and height.

It doesnt seem to be scaling correctly to the world. Here is the `WorlScaler` class which scales world/pixel co-ordinates according to screen width/height, specifically though the `toWorldHeight` and `toWorldWidth` is what I think is giving problems:

Code: Select all

class WorldScaler {

    private final int containerWidth, containerHeight;
    private final float meterToPixel;

    public WorldScaler(int containerWidth, int containerHeight, float meterToPixel) {
        this.meterToPixel = meterToPixel;
        this.containerWidth = containerWidth;
        this.containerHeight = containerHeight;
    }

    public int getContainerHeight() {
        return containerHeight;
    }

    public int getContainerWidth() {
        return containerWidth;
    }

    //http://www.box2d.org/forum/viewtopic.php?f=3&t=8198
    public int toPixelX(float worldX) {
        float offsetX = (containerWidth / meterToPixel);
        int x = (int) ((worldX + offsetX) * meterToPixel);
        return x;
    }

    public int toPixelY(float worldY) {
        float offsetY = (containerHeight / meterToPixel);
        int y = (int) ((-worldY + offsetY) * meterToPixel);
        return y;
    }

    public float toWorldX(int pixelX) {
        float y = (pixelX - containerWidth) / meterToPixel;
        return y;
    }

    public float toWorldY(int pixelY) {
        float y = (-pixelY + containerHeight) / meterToPixel;
        return y;
    }

    //In Swing usually we  work with an objects inital co-oridnates and not center co-ords this methid is for
    //accucracy purposes and toPixelX(..)*2 and toPixelY(..)*2 would show small visual anamolies as the rounfing was too far off
    public int toSwingPixelX(float worldX) {
        float offsetX = (containerWidth / meterToPixel);
        int x = (int) (((worldX + offsetX) * meterToPixel) * 2);
        return x;
    }

    public int toSwingPixelY(float worldY) {
        float offsetY = (containerHeight / meterToPixel);
        int y = (int) (((-worldY + offsetY) * meterToPixel) * 2);
        return y;
    }

    public float toWorldWidth(int pixelWidth) {
        float w=pixelWidth/meterToPixel;
        return w;
    }

    public float toWorldHeight(int pixelHeight) {
        float h =(pixelHeight/meterToPixel);
        return h;
    }
}


Here is the 2 classes used `Ball` and `Wall` (and `Entity` the `interface` which they implement):

Code: Select all

class Wall implements Entity {

    private float centeredWorldX, centeredWorldY, centeredWorldWidth, centeredWorldHeight;
    private int pixelX, pixelY, pixelWidth, pixelHeight;
    private final Color color;
    private final WorldScaler worldScaler;
    private Body body;

    public Wall(int pixelX, int pixelY, int pixelWidth, int pixelHeight, Color color, WorldScaler worldScaler) {
        this.pixelX = pixelX;
        this.pixelY = pixelY;
        this.pixelWidth = pixelWidth;
        this.pixelHeight = pixelHeight;
        this.color = color;
        this.worldScaler = worldScaler;
        int pixelCenteredX = this.pixelX / 2;//box2d works with the center co-ords so we get the center of circle X and Y
        int pixelCenteredY = this.pixelY / 2;
        int centeredPixelWidth = this.pixelWidth / 2;
        int centeredPixelHeight = this.pixelHeight / 2;
        this.centeredWorldX = this.worldScaler.toWorldX(pixelCenteredX);
        this.centeredWorldY = this.worldScaler.toWorldY(pixelCenteredY);
        this.centeredWorldWidth = this.worldScaler.toWorldWidth(centeredPixelWidth);
        this.centeredWorldHeight = this.worldScaler.toWorldHeight(centeredPixelHeight);
    }

    @Override
    public FixtureDef createFixtureDef(Shape s) {
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = s;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.3f;
        return fixtureDef;
    }

    @Override
    public PolygonShape createShape() {
        PolygonShape polygonShape = new PolygonShape();
        polygonShape.setAsBox(centeredWorldWidth, centeredWorldHeight);
        return polygonShape;
    }

    @Override
    public BodyDef createBodyDef() {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.STATIC;
        bodyDef.position = new Vec2(centeredWorldX, centeredWorldY);
        return bodyDef;
    }

    @Override
    public void draw(Graphics2D g2d) {
        g2d.setColor(color);
        g2d.fillRect(pixelX, pixelY, pixelWidth, pixelHeight);
        g2d.setColor(Color.red);
        g2d.drawLine(pixelWidth / 2, worldScaler.getContainerHeight(), worldScaler.getContainerWidth() / 2, worldScaler.getContainerHeight() - pixelHeight);
        g2d.setColor(Color.BLUE);
        g2d.drawLine(0, worldScaler.getContainerHeight() - (pixelHeight / 2), worldScaler.getContainerWidth(), worldScaler.getContainerHeight() - (pixelHeight / 2));
    }

    @Override
    public void setBody(Body body) {
        this.body = body;
    }

    @Override
    public void updateBodyPosition() {
    }
}

class Ball implements Entity {

    private float worldX, worldY, worldRadius;
    private int pixelX, pixelY, pixelRadius;
    private final Color color;
    private final WorldScaler worldScaler;
    private Body body;

    public Ball(int pixelX, int pixelY, int pixelRadius, Color color, WorldScaler worldScaler) {
        this.pixelX = pixelX;
        this.pixelY = pixelY;
        this.pixelRadius = pixelRadius;
        this.color = color;
        this.worldScaler = worldScaler;
        int pixelsCenterX = pixelX / 2;//box2d works with the center co-ords so we get the center of circle X and Y and radius
        int pixelsCenterY = pixelY / 2;
        this.worldX = this.worldScaler.toWorldX(pixelsCenterX);
        this.worldY = this.worldScaler.toWorldY(pixelsCenterY);
        this.worldRadius = this.worldScaler.toWorldWidth(pixelRadius);
    }

    @Override
    public FixtureDef createFixtureDef(Shape s) {
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = s;
        fixtureDef.density = 0.1f;
        fixtureDef.friction = 0.3f;
        fixtureDef.restitution = 0.5f;
        return fixtureDef;
    }

    @Override
    public CircleShape createShape() {
        CircleShape circularShape = new CircleShape();
        circularShape.m_radius = worldRadius;
        return circularShape;
    }

    @Override
    public BodyDef createBodyDef() {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DYNAMIC;
        bodyDef.position.set(worldX, worldY);//uses center co-ords of pixels -> meters /2
        return bodyDef;
    }

    @Override
    public void draw(Graphics2D g2d) {
        g2d.setColor(color);
        g2d.fillOval(pixelX, pixelY, pixelRadius, pixelRadius);
        g2d.setColor(Color.red);
        g2d.drawLine(pixelX + pixelRadius / 2, pixelY + pixelRadius / 2, pixelX + (pixelRadius), pixelY + (pixelRadius));
    }

    @Override
    public void setBody(Body body) {
        this.body = body;
    }

    @Override
    public void updateBodyPosition() {
        pixelX = worldScaler.toSwingPixelX(body.getPosition().x);
        pixelY = worldScaler.toSwingPixelY(body.getPosition().y);
    }
}

interface Entity {

    public abstract BodyDef createBodyDef();

    public abstract FixtureDef createFixtureDef(Shape shape);

    public abstract Shape createShape();

    public abstract void setBody(Body body);

    public abstract void updateBodyPosition();

    public abstract void draw(Graphics2D g2d);
}


I create instances of each like so:

Code: Select all

                int WIDTH=800,HEIGHT=600;

                //create a floor for ball to bounce on at bottom of screen
                int wallHeight = 10;
                int wallX = 0;
                int wallY = HEIGHT - wallHeight;
                int wallWidth = WIDTH;
                //create a new instance of wall at bottom of screen as a floor
                Wall floor = new Wall(wallX, wallY, wallWidth, wallHeight, Color.BLACK, worldScaler);

                //create entities co-ordinates using Swing values as entity would convert toappropate world values i.e pixels -> meters
                int ballRadius = 50;
                int ballX = (WIDTH / 2) - ballRadius, ballY = (HEIGHT / 2) - ballRadius;
                Color ballColor = Color.GREEN;
                //create a new green ball in the center of the screen using java co-ords the class will convert to world co-ords through wordscaler instance passed
                Ball ball = new Ball(ballX, ballY, ballRadius, ballColor, worldScaler);


The below attachment shows the body at rest on an invisible platform (hence my notion my width and height scaling is going funny)

Original thread: https://box2d.org/forum/viewtopic.php?t=9066