Libgdx-camera.unproject没有固定我的坐标
下面的代码给出了非常奇怪的y坐标Libgdx-camera.unproject没有固定我的坐标,camera,libgdx,project,Camera,Libgdx,Project,下面的代码给出了非常奇怪的y坐标 10-18 00:13:36.834 30543-30567/com.xxxx.yyyy.android I/x﹕ 137.4782 10-18 00:13:36.834 30543-30567/com.xxxx.yyyy.android I/y﹕ -1984.2426 10-18 00:13:36.835 30543-30567/com.xxxx.yyyy.android I/ux﹕ 91.65213 10-18 00:13:36.835 30543-
10-18 00:13:36.834 30543-30567/com.xxxx.yyyy.android I/x﹕ 137.4782
10-18 00:13:36.834 30543-30567/com.xxxx.yyyy.android I/y﹕ -1984.2426
10-18 00:13:36.835 30543-30567/com.xxxx.yyyy.android I/ux﹕ 91.65213
10-18 00:13:36.835 30543-30567/com.xxxx.yyyy.android I/uy﹕ -1984.2426
我想我把一切都设置错了,而不是在跑步时做错事?
camera.unproject调用应该负责从屏幕坐标到游戏坐标的所有重新映射,不是吗?或者我必须在取消投影之前缩放和反转吗
package com.xxxx.yyyy;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
public class LetterActor extends Actor
{
private Texture texture;
private Vector3 touchPosition = new Vector3();
private Camera camera;
private boolean unproject = true;
public LetterActor(Texture letterTexture, Camera theCamera)
{
texture = letterTexture;
camera = theCamera;
touchPosition.set(240, 800, 0);
camera.unproject(touchPosition);
setPosition(touchPosition.x, touchPosition.y);
setSize(texture.getWidth(), texture.getHeight());
addListener(new InputListener()
{
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
logPositions(x, y, touchPosition.x, touchPosition.y);
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
logPositions(x, y, touchPosition.x, touchPosition.y);
}
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
logPositions(x, y, touchPosition.x, touchPosition.y);
}
});
}
private void screenTo()
{
}
private void logPositions(float x, float y,float ux, float uy)
{
Gdx.app.log("x", Float.toString(x));
Gdx.app.log("y", Float.toString(y));
Gdx.app.log("ux", Float.toString(ux));
Gdx.app.log("uy", Float.toString(y));
}
@Override
public void draw(Batch batch, float alpha)
{
batch.draw(texture, getX(), getY(), getWidth(), getHeight());
}
@Override
public void act(float delta) {}
}
package com.xxxx.yyyy;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class WordPuzzle extends ApplicationAdapter
{
private final static float VIRTUAL_WIDTH = 480;
private final static float VIRTUAL_HEIGHT = 800;
private OrthographicCamera camera;
private FitViewport viewport;
private Stage stage;
@Override
public void create()
{
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
camera.setToOrtho(false, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, camera);
stage = new Stage();
stage.setViewport(viewport);
Gdx.input.setInputProcessor(stage);
Texture[] textures = LetterLoader.loadLetters();
for (int i = 0; i < textures.length; i++)
{
LetterActor letterActor = new LetterActor(textures[i], camera);
letterActor.setTouchable(Touchable.enabled);
stage.addActor(letterActor);
}
}
@Override
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override public void resize(int width, int height)
{
stage.getViewport().update(width, height, true);
}
@Override public void dispose()
{
stage.dispose();
}
}
package com.xxxx.yyyy;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
public class LetterLoader {
public static Texture[] loadLetters()
{
Texture[] letters = new Texture[26];
for (int i = 0; i < 26; i++)
{
char letter = (char) (i + 65);
letters[i] = new Texture(Gdx.files.internal("bigletters/" + letter + ".png"));
}
return letters;
}
}
包com.xxxx.yyyy;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.Camera;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.Batch;
导入com.badlogic.gdx.math.Vector3;
导入com.badlogic.gdx.scenes.scene2d.Actor;
导入com.badlogic.gdx.scenes.scene2d.InputEvent;
导入com.badlogic.gdx.scenes.scene2d.InputListener;
公共类LetterActor扩展了Actor
{
私有纹理;
私有向量3 touchPosition=新向量3();
私人摄像机;
私有布尔unproject=true;
公共信笺演员(纹理、纹理、照相机)
{
纹理=字母纹理;
摄像机=摄像机;
触摸位置设置(240800,0);
相机。取消投影(触摸位置);
设置位置(touchPosition.x,touchPosition.y);
设置大小(texture.getWidth(),texture.getHeight());
addListener(新的InputListener()
{
@凌驾
公共布尔接地(InputEvent事件、浮点x、浮点y、整数指针、整数按钮)
{
触摸位置设置(x,y,0);
如果(未投影)
{
相机。取消投影(触摸位置);
}
设置位置(touchPosition.x,touchPosition.y);
日志位置(x,y,touchPosition.x,touchPosition.y);
返回true;
}
@凌驾
公共无效修补(InputEvent事件、浮点x、浮点y、整数指针、整数按钮)
{
触摸位置设置(x,y,0);
如果(未投影)
{
相机。取消投影(触摸位置);
}
设置位置(touchPosition.x,touchPosition.y);
日志位置(x,y,touchPosition.x,touchPosition.y);
}
@凌驾
public void touchdrable(InputEvent事件、浮点x、浮点y、int指针)
{
触摸位置设置(x,y,0);
如果(未投影)
{
相机。取消投影(触摸位置);
}
设置位置(touchPosition.x,touchPosition.y);
日志位置(x,y,touchPosition.x,touchPosition.y);
}
});
}
私有空屏幕到()
{
}
专用无效日志位置(浮动x、浮动y、浮动ux、浮动uy)
{
Gdx.app.log(“x”,Float.toString(x));
Gdx.app.log(“y”,Float.toString(y));
Gdx.app.log(“ux”,Float.toString(ux));
Gdx.app.log(“uy”,Float.toString(y));
}
@凌驾
公共作废提取(批量、浮动alpha)
{
绘制(纹理,getX(),getY(),getWidth(),getHeight());
}
@凌驾
公共无效法案(浮动三角洲){}
}
包com.xxxx.yyyy;
导入com.badlogic.gdx.ApplicationAdapter;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Orthographic Camera;
导入com.badlogic.gdx.utils.viewport.ExtendViewport;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.scenes.scene2d.Stage;
导入com.badlogic.gdx.scenes.scene2d.Touchable;
导入com.badlogic.gdx.utils.viewport.FitViewport;
公共类WordPuzzle扩展了ApplicationAdapter
{
专用最终静态浮动虚拟_宽度=480;
专用最终静态浮动虚拟_高度=800;
私人正交摄影机;
私人视窗;
私人舞台;
@凌驾
公共void create()
{
摄影机=新正交摄影机(虚拟_宽度、虚拟_高度);
照相机。设置为(假、虚拟宽度、虚拟高度);
视区=新视区(虚拟视区宽度、虚拟视区高度、相机);
阶段=新阶段();
stage.setViewport(viewport);
Gdx.input.setInputProcessor(阶段);
纹理[]纹理=LetterLoader.loadLetters();
对于(int i=0;i
首先,从输入侦听器获得的触摸位置(x,y)已经是正确的坐标
关于输出,您实际上打印了两次y,但第二次称之为uy:
Gdx.app.log(“uy”,Float.toString(y));
如果
touchPosition.set(240800,0)代码>在屏幕坐标中,则需要取消投影,但是
camera.unproject(触摸位置);
假设您的相机充满整个屏幕,因此它在内部调用:
unproject(screenCoords,0,0,Gdx.graphics.getWidth(),Gdx.grap