2017-04-14 7 views
0

상자 인 몸체에 텍스처를 그려보고 싶습니다. 본문 좌표를 화면 좌표로 변환하려면 어떻게해야합니까? 다른 방법은 camera.unproject (pos)를 사용하는 것입니다.이 방법과 비슷합니까?libgdx pos와 factor를 화면에 표시하는 세계

많은 사람들이 WORLD_TO_SCREEN = 32와 같은 상수를 사용하는 것을 보았습니다. 그러나 현재는 게임에서 사용하지 않았습니다. 이것이 문제이며, 지금 어떻게 구현할 수 있습니까? 이러한 요소를 사용하는 사람들이 세계를 화면 위치로 쉽게 변환 할 수 있기 때문입니다. 나는 현재, 카메라와 ExtendViewport

camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); 
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera); 
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f); 

뷰포트의 폭과 높이가 정말 내가 뭘하는지 모르는

public static final int VIEWPORT_WIDTH = 20; 
public static final int VIEWPORT_HEIGHT = 22; 

이, 나는 문서를 읽고 몇 가지 튜토리얼을 읽을로 설정되어 있습니다 그러나 누군가가이 변수들에 대해 약간의 설명을 줄 수 있고 실제로 나를 도와 줄 요인이 world_to_screen 일 수 있다면.

도 설정할 수 있습니다. APP_WIDTH=1280, APP_HEIGHT = 720 뷰포트 너비와 높이는 box2d의 화면 크기가 너비 20m, 높이 22m임을 의미합니까? (나는 많은에게 질문을 추가하기 때문에 다시 묻고 난 정말이 일을 알고 싶습니다)

[편집]

그래서 나는 지상 본체에 접지 그림을 그려하기 위해 노력하고있어

float x = stage.getGround().getX(); 
float y = stage.getGround().getY(); 
float w = stage.getGround().getWidth(); 
float h = stage.getGround().getHeight(); 

stage.act(delta); 
stage.draw(); 
stage.updateCamera(); 

Texture texture = new Texture(Gdx.files.internal("ground.png")); 
Sprite sprite = new Sprite(texture); 
sprite.setSize(w, h); 
sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2); 

그러나 나는 그것을 볼 수 없습니다 어디

[수정 2] 단계 클래스

public class Mission1Stage extends Stage{ 
    public static final int VIEWPORT_WIDTH = 20; 
    public static final int VIEWPORT_HEIGHT = 22; 

    private World world; 
    private Ground ground; 
    private LeftWall leftWall; 
    private Rocket rocket; 

    private static final float TIME_STEP = 1/300f; 
    private float accumulator = 0f; 

    private OrthographicCamera camera; 
    private Box2DDebugRenderer renderer; 
    private Viewport viewport; 

    private SpriteBatch spriteBatch = new SpriteBatch(); 

    private Vector3 touchPoint; 
    private ShapeRenderer shapeRenderer; 

    private Button boostButton; 
    private Skin boostSkin; 

    private Button boostLeftButton; 
    private Skin boostLeftSkin; 

    private Button boostRightButton; 
    private Skin boostRightSkin; 

    private Button resetButton; 
    private Skin resetSkin; 

    private Game game; 

    private boolean isTouched = false; 

    public Mission1Stage(Game game) { 
     setUpWorld(); 
     renderer = new Box2DDebugRenderer(); 
     shapeRenderer = new ShapeRenderer(); 
     setupCamera(); 
     setUpButtons(); 
     addActor(new Background(ground)); 
    } 

    private void setUpWorld() { 
     world = WorldUtils.createWorld(); 
     setUpGround(); 
     setUpRocket(); 
    } 

    private void setUpGround() { 
     ground = new Ground(WorldUtils.createGround(world)); 
     addActor(ground); 
    } 

    private void setUpLeftWall() { 
     leftWall = new LeftWall(WorldUtils.createLeftWall(world)); 
    } 

    private void setUpRocket() { 
     rocket = new Rocket(WorldUtils.createRocket(world)); 
     addActor(rocket); 
    } 

    private void setupCamera() { 
     camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); 
     viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera); 
     camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f); 
     camera.update(); 
    } 

    private void setUpButtons() { 
     boostSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json")); 
     boostButton = new Button(boostSkin); 
     boostButton.setSize(80,80); 
     boostButton.setPosition(Gdx.graphics.getWidth()-boostButton.getWidth()*2,0); 
     boostButton.setTransform(true); 
     boostButton.scaleBy(0.5f); 
     Gdx.input.setInputProcessor(this); 
     addActor(boostButton); 

     boostLeftSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json")); 
     boostLeftButton = new Button(boostLeftSkin); 
     boostLeftButton.setSize(100, 100); 
     boostLeftButton.setPosition(0, 0); 
     addActor(boostLeftButton); 

     boostRightSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json")); 
     boostRightButton = new Button(boostRightSkin); 
     boostRightButton.setSize(100, 100); 
     boostRightButton.setPosition(boostLeftButton.getWidth(), 0); 
     addActor(boostRightButton); 

     resetSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json")); 
     resetButton = new Button(resetSkin); 
     resetButton.setSize(100, 100); 
     resetButton.setPosition(Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100); 
     addActor(resetButton); 
    } 

    @Override 
    public void act(float delta) { 
     super.act(delta); 
     handleInput(); 

     accumulator += delta; 

     while(accumulator >= delta) { 
      world.step(TIME_STEP, 6, 2); 
      accumulator -= TIME_STEP; 
     } 
    } 

    @Override 
    public void draw() { 
     super.draw(); 
     renderer.render(world, camera.combined); 

     float x = getGround().getBody().getPosition().x; 
     float y = getGround().getBody().getPosition().y; 
     float w = getGround().getWidth() * 2; 
     float h = getGround().getHeight() * 2; 

     spriteBatch.setProjectionMatrix(getCamera().combined); 

     Texture texture = new Texture(Gdx.files.internal("ground.png")); 
     Sprite sprite = new Sprite(texture); 
     sprite.setSize(w, h); 
     sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2); 


     spriteBatch.begin(); 
     sprite.draw(spriteBatch); 
     spriteBatch.end(); 
    } 

    public void handleInput() { 
     if(boostButton.isPressed()) { 
      rocket.boost(); 
     } 
     if(boostLeftButton.isPressed()) { 
      rocket.turnLeft(); 
     } 
     if(boostRightButton.isPressed()) { 
      rocket.turnRight(); 
     } 
     if(resetButton.isPressed()) { 

     } 
    } 

    public boolean resetScreen() { 
     if(resetButton.isPressed()) return true; 
     return false; 
    } 

    public void updateCamera() { 

    } 

    public Ground getGround() { 
     return ground; 
    } 

    public void resize(int width, int height) { 
     viewport.update(width, height); 
     camera.position.x = VIEWPORT_WIDTH/2; 
     camera.position.y = VIEWPORT_HEIGHT /2; 
    } 


    private void translateScreenToWorldCoordinates(int x, int y) { 
     getCamera().unproject(touchPoint.set(x, y, 0));getCamera(); 
    } 
} 

화면 클래스

public class Mission1Screen implements Screen{ 
    private Game game; 
    private Mission1Stage stage; 
    private SpriteBatch spriteBatch = new SpriteBatch(); 

    private Skin boostSkin; 
    private Button boostButton; 

    public Mission1Screen(Game game) { 
     this.game = game; 
     stage = new Mission1Stage(game); 
    } 

    @Override 
    public void show() { 

    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     if(stage.resetScreen()) { 
      game.setScreen(new Mission1Screen(game)); 
     } 

     stage.act(delta); 
     stage.draw(); 
     stage.updateCamera(); 
    } 

    @Override 
    public void resize(int width, int height) { 
     stage.resize(width, height); 
    } 

    @Override 
    public void pause() { 

    } 

    @Override 
    public void resume() { 

    } 

    @Override 
    public void hide() { 

    } 

    @Override 
    public void dispose() { 

    } 
} 

[편집 3]

public class Main extends Game { 

    @Override 
    public void create() { 
     this.setScreen(new Mission1Screen(this)); 
    } 

    @Override 
    public void render() { 
     super.render(); 
    } 

    @Override 
    public void dispose() { 

    } 
} 

답변

2

m에서 Box2D의 최고의 작품 (0-10)하지만 당신은 작은 사용하여이 변환을 피할 수 있기 때문에 우리는 대부분 미터 변환 픽셀을 사용 뷰포트의 월드 와이드와 높이. 저는 주로 뷰포트 너비와 높이로 48과 80을 선호합니다.

화면 좌표에서 주어진 점을 월드 공간으로 변환하는 카메라 방법 unproject(vector3)을 사용할 수 있습니다. 나는이 방법을 touchdown에서 사용하고 있는데, 화면 좌표를 매개 변수로 얻었 기 때문에 카메라 월드 공간으로 변환해야하므로 월드의 특정 위치에서 개체를 생성 할 수 있습니다.

public class MyGdxTest extends Game implements InputProcessor { 

    private SpriteBatch batch; 
    private ExtendViewport extendViewport; 
    private OrthographicCamera cam; 

    private float w=20; 
    private float h=22; 

    private World world; 
    private Box2DDebugRenderer debugRenderer; 

    private Array<Body> array; 
    private Vector3 vector3; 

    @Override 
    public void create() { 

     cam=new OrthographicCamera(); 
     extendViewport=new ExtendViewport(w,h,cam); 

     batch =new SpriteBatch(); 
     Gdx.input.setInputProcessor(this); 

     world=new World(new Vector2(0,-9.8f),true); 
     array=new Array<Body>(); 
     debugRenderer=new Box2DDebugRenderer(); 
     vector3=new Vector3(); 

     BodyDef bodyDef=new BodyDef(); 
     bodyDef.type= BodyDef.BodyType.StaticBody; 
     bodyDef.position.set(0,0); 
     Body body=world.createBody(bodyDef); 

     ChainShape chainShape=new ChainShape(); 
     chainShape.createChain(new float[]{1,1,55,1}); 

     FixtureDef fixtureDef=new FixtureDef(); 
     fixtureDef.shape=chainShape; 
     fixtureDef.restitution=.5f; 
     body.createFixture(fixtureDef); 
     chainShape.dispose(); 
    } 

    @Override 
    public void render() { 
     super.render(); 

     Gdx.gl.glClearColor(0,1,1,1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     world.step(1/60f,6,2); 
     batch.setProjectionMatrix(cam.combined); 
     batch.begin(); 

     world.getBodies(array); 
     for (Body body:array){ 
      if(body.getUserData()!=null) { 
       Sprite sprite = (Sprite) body.getUserData(); 
       sprite.setPosition(body.getPosition().x-sprite.getWidth()/2, body.getPosition().y-sprite.getHeight()/2); 
       sprite.setRotation(body.getAngle()*MathUtils.radDeg); 
       sprite.draw(batch); 
      } 
     } 
     batch.end(); 
     debugRenderer.render(world,cam.combined); 
    } 

    @Override 
    public void resize(int width, int height) { 
     super.resize(width,height); 
     extendViewport.update(width,height); 
     cam.position.x = w /2; 
     cam.position.y = h/2; 
     cam.update(); 
    } 

    private void createPhysicsObject(float x,float y){ 

     float sizeX=2,sizeY=2; 
     BodyDef bodyDef=new BodyDef(); 
     bodyDef.position.set(x,y); 
     bodyDef.type= BodyDef.BodyType.DynamicBody; 

     Body body=world.createBody(bodyDef); 

     PolygonShape polygonShape=new PolygonShape(); 
     polygonShape.setAsBox(sizeX,sizeY); 
     FixtureDef fixtureDef=new FixtureDef(); 
     fixtureDef.shape=polygonShape; 
     fixtureDef.restitution=.2f; 
     fixtureDef.density=2; 

     body.createFixture(fixtureDef); 
     body.setFixedRotation(false); 
     polygonShape.dispose(); 


     Sprite sprite=new Sprite(new Texture("badlogic.jpg")); 
     sprite.setSize(2*sizeX,2*sizeY); 
     sprite.setPosition(x-sprite.getWidth()/2,y-sprite.getHeight()/2); 
     sprite.setOrigin(sizeX,sizeY); 

     body.setUserData(sprite); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     debugRenderer.dispose(); 
     world.dispose(); 
    } 

    @Override 
    public boolean keyDown(int keycode) { 
     return false; 
    } 

    @Override 
    public boolean keyUp(int keycode) { 
     return false; 
    } 

    @Override 
    public boolean keyTyped(char character) { 
     return false; 
    } 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 

     vector3.set(screenX,screenY,0); 
     Vector3 position=cam.unproject(vector3); 
     createPhysicsObject(vector3.x,vector3.y); 

     return false; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
     return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) { 
     return false; 
    } 

    @Override 
    public boolean mouseMoved(int screenX, int screenY) { 
     return false; 
    } 

    @Override 
    public boolean scrolled(int amount) { 
     return false; 
    } 
} 
관련 문제