Apply pixel Coordinates to Screen Coordinates

两盒软妹~` 提交于 2019-12-10 13:57:20

问题


I'm trying to make an object appear where the person last touched. However when I try to do so it appears in the wrong place. I assume this is because of the fact the coordinates that the input returns is different to the display coordinates, my code is as follows:

public class Core implements ApplicationListener, InputProcessor
          { //Has to be here otherwise the code formatting becomes buggy


private Mesh squareMesh;
private PerspectiveCamera camera;
private Texture texture;
private SpriteBatch spriteBatch;
Sprite sprite;
float moveX = 0;


private final Matrix4 viewMatrix = new Matrix4();
private final Matrix4 transformMatrix = new Matrix4();

@Override
public void create()
{
    Gdx.input.setInputProcessor(this);


    texture = new Texture(Gdx.files.internal("door.png"));

    spriteBatch = new SpriteBatch();
    sprite = new Sprite(texture);
    sprite.setPosition(0, 0);
    viewMatrix.setToOrtho2D(0, 0, 480, 320);

    float x = 0;
    float y = 0;

}

@Override
public void dispose()
{
}

@Override
public void pause()
{
}

@Override
public void render()
{
    viewMatrix.setToOrtho2D(0, 0, 480, 320);
    spriteBatch.setProjectionMatrix(viewMatrix);
    spriteBatch.setTransformMatrix(transformMatrix);
    spriteBatch.begin();
    spriteBatch.disableBlending();
    spriteBatch.setColor(Color.WHITE);

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


    //spriteBatch.draw(texture, 0, 0, 1, 1, 0, 0, texture.getWidth(),
    //      texture.getHeight(), false, false);
    sprite.draw(spriteBatch);
    spriteBatch.end();
    update();
}

@Override
public void resize(int width, int height)
{
    float aspectRatio = (float) width / (float) height;
    camera = new PerspectiveCamera(67, 2f * aspectRatio, 2f);

}

@Override
public void resume()
{
}

public void update()
{

    float delta = Gdx.graphics.getDeltaTime();

    if(Gdx.input.isTouched())
    {
         Vector3 worldCoordinates = new Vector3(sprite.getX(), sprite.getY(), 0);
         camera.unproject(worldCoordinates);
        sprite.setPosition(Gdx.input.getX(), Gdx.input.getY());

        float moveX = 0;
        float moveY = 0;

 }
     }

I cropped this code for sake of simplicty. I also made a video demonstrating the bug: http://www.youtube.com/watch?v=m89LpwMkneI


回答1:


Camera.unproject converts screen coordinates to world coordinates.

Vector3 pos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(pos);
sprite.setPosition(pos.x, pos.y);



回答2:


Firstly, Gdx.input.getX() and Gdx.input.getY() return "screen coordinates". You want to transform these to your "camera coordinates". Screen coordinates typically have (0,0) in the top left corner of the window. I think your camera coordinates have (0,0) at the bottom left corner (either libgdx or opengl are doing that). Your video seems to suggest that this true. So you will need to multiply the Y value by -1. Secondly, I suspect the scale of the screen is different from the scale of the camera. I think you can fix the scale by multiplying by (world/screen).

Let's say your screen has width=800, height=600 and your world has width=480 height=320. Then your new X,Y for your sprite should be:

X = Gdx.input.getX()*(480/800)
Y = Gdx.input.getY()*(320/600)*-1



回答3:


you should check your touch cordinates.

Gdx.app.log("", "hello x"+touchx);
Gdx.app.log("", "hello x"+touchy);

here touchx and touchy are your input x and input y variables then do calculation where touch should work like if u touched x=100,y=100

and touchx is coming 120 and touch y is coming 120

soo in your update method do this

sprite.setPosition(Gdx.input.getX()-20, Gdx.input.getY()-20);

i think this will help




回答4:


I figured out the screen size/ game ratio and multiplied it to the current screen size:

rect.x=((((1024/Gdx.graphics.getWidth()))* Gdx.graphics.getWidth())

for a screen width of 1024 pixels

There must be an easier way however this works for me.



来源:https://stackoverflow.com/questions/8104364/apply-pixel-coordinates-to-screen-coordinates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!