Box2D how to implement a camera?
        Posted  
        
            by 
                Romeo
            
        on Game Development
        
        See other posts from Game Development
        
            or by Romeo
        
        
        
        Published on 2012-04-12T11:16:06Z
        Indexed on 
            2012/04/12
            11:44 UTC
        
        
        Read the original article
        Hit count: 396
        
By now i have this Camera class.
package GameObjects;
import main.Main;
import org.jbox2d.common.Vec2;
public class Camera {
public int x;
public int y;
public int sx;
public int sy;
public static final float PIXEL_TO_METER = 50f;
private float yFlip = -1.0f;
public Camera()
{
    x = 0;
    y = 0;
    sx = x + Main.APPWIDTH;
    sy = y + Main.APPHEIGHT;
}
public Camera(int x, int y)
{
    this.x = x;
    this.y = y;
    sx = x + Main.APPWIDTH;
    sy = y + Main.APPHEIGHT;
}
public void update()
{
    sx = x + Main.APPWIDTH;
    sy = y + Main.APPHEIGHT;
}
public void moveCam(int mx, int my)
{
    if(mx >= 0 && mx <= 80)
    {
        this.x -= 2;
    } else if(mx <= Main.APPWIDTH && mx >= Main.APPWIDTH - 80)
    {
        this.x += 2;
    }
    if(my >= 0 && my <= 80)
    {
        this.y += 2;
    } 
    else if(my <= Main.APPHEIGHT && my >= Main.APPHEIGHT - 80)
    {
        this.y -= 2;
    }
    this.update();
}
public float meterToPixel(float meter)
{
    return meter * PIXEL_TO_METER;
}
public float pixelToMeter(float pixel)
{
    return pixel / PIXEL_TO_METER;
}
public Vec2 screenToWorld(Vec2 screenV) {
    return new Vec2(screenV.x + this.x, yFlip
            * screenV.y + this.y);
}
public Vec2 worldToScreen(Vec2 worldV) {
    return new Vec2(worldV.x - this.x, yFlip
            * worldV.y - this.y);
}
}
I need to know how to modify the screenToWorld and worldToScreen functions to include the PIXEL_TO_METER scaling.
© Game Development or respective owner