Java 不同设备的Libgdx缩放坐标
我做了一个简单的游戏,你点击一个物体,它就会消失。它在尺寸为240 x 480的桌面上运行良好,但在我的手机上,尺寸更宽,如1920 x 1080,因此触摸坐标不同,因此在桌面上,它可能会记录触摸100 x 50,但如果我在手机上点击相同的位置,它将是400 x 200,因此我只想缩放它们或使用相同的坐标Java 不同设备的Libgdx缩放坐标,java,libgdx,Java,Libgdx,我做了一个简单的游戏,你点击一个物体,它就会消失。它在尺寸为240 x 480的桌面上运行良好,但在我的手机上,尺寸更宽,如1920 x 1080,因此触摸坐标不同,因此在桌面上,它可能会记录触摸100 x 50,但如果我在手机上点击相同的位置,它将是400 x 200,因此我只想缩放它们或使用相同的坐标 private double WidthScale = (272 / Gdx.graphics.getWidth()); private double HeightScale = (408 /
private double WidthScale = (272 / Gdx.graphics.getWidth());
private double HeightScale = (408 / Gdx.graphics.getHeight());
private Array<Rectangle> rockets;
private long lastDropTime = 0;
private float tap_X = 0;
private float tap_Y = 0;
public GameRenderer() {
cam = new OrthographicCamera();
cam.setToOrtho(false, 408, 272);
batch = new SpriteBatch();
rockets = new Array<Rectangle>();
spawnRocket();
}
public void render(){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
for(Rectangle rocket : rockets) {
batch.draw(AssetLoader.rocket ,rocket.x, rocket.y,
rocket.width, rocket.height);
}
batch.end();
if (Gdx.input.justTouched()){
tap_X = (int) (Gdx.input.getX() * HeightScale);
tap_Y = (int) ((Gdx.graphics.getHeight()-Gdx.input.getY()) * WidthScale);
Gdx.app.log("MyTag", String.valueOf(tap_X));
Gdx.app.log("MyTag", String.valueOf(tap_Y));
Gdx.app.log("MyTag", String.valueOf(HeightScale));
Gdx.app.log("MyTag", String.valueOf(WidthScale));
}
Iterator<Rectangle> iter = rockets.iterator();
while(iter.hasNext()) {
Rectangle rocket = iter.next();
rocket.y -= 70 * Gdx.graphics.getDeltaTime();
if(rocket.y + 32 < 0) iter.remove();
if (rocket.x < tap_X && tap_X < rocket.x + rocket.width) {
Gdx.app.log("MyTag", "getRekt");
if (tap_Y > rocket.y && tap_Y < rocket.y + rocket.height) {
Gdx.app.log("MyTag", "poo");
iter.remove();
}
}
}
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRocket();
}
private void spawnRocket() {
Gdx.app.log("MyTag", "Rocket Spawned");
Rectangle rocket = new Rectangle();
rocket.x = MathUtils.random(0 , 272 - 16);
rocket.y = 408 + rocket.height;
rocket.height = 32;
rocket.width = 16;
rockets.add(rocket);
lastDropTime = TimeUtils.nanoTime();
}
private double WidthScale=(272/Gdx.graphics.getWidth());
私有双高度刻度=(408/Gdx.graphics.getHeight());
专用阵列火箭;
私有长lastDropTime=0;
专用浮点数X=0;
专用浮点数Y=0;
公共游戏机(){
cam=新的正交摄影机();
凸轮设置为(假,408272);
批次=新的SpriteBatch();
火箭=新阵列();
产卵火箭();
}
公共无效呈现(){
glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
用于(矩形火箭:火箭){
批量绘制(AssetLoader.rocket,rocket.x,rocket.y,
火箭。宽度,火箭。高度);
}
batch.end();
if(Gdx.input.justTouched()){
轻触X=(int)(Gdx.input.getX()*高度刻度);
点击Y=(int)((Gdx.graphics.getHeight()-Gdx.input.getY())*WidthScale);
Gdx.app.log(“MyTag”,String.valueOf(tap_X));
Gdx.app.log(“MyTag”,String.valueOf(tap_Y));
Gdx.app.log(“MyTag”,String.valueOf(HeightScale));
Gdx.app.log(“MyTag”,String.valueOf(WidthScale));
}
迭代器iter=rockets.Iterator();
while(iter.hasNext()){
矩形火箭=iter.next();
rocket.y-=70*Gdx.graphics.getDeltaTime();
if(rocket.y+32<0)iter.remove();
if(rocket.xrocket.Y&&tap_Y100000000)spawnRocket();
}
私有火箭(){
Gdx.app.log(“MyTag”、“火箭繁殖”);
矩形火箭=新矩形();
rocket.x=MathUtils.random(0272-16);
火箭y=408+火箭高度;
火箭高度=32;
rocket.width=16;
火箭。添加(火箭);
lastDropTime=TimeUtils.nanoTime();
}
为了使用不同的屏幕尺寸,您必须处理视口,有一个演示如何使用它的窗口。下面是一个快速示例(但是,我建议您阅读wiki并自己尝试其他视口):
希望您觉得这很有用。您必须处理不同的视口和投影,添加代码以了解您尝试了什么以及您期望的是什么…@AlexandroSifuentesDíaz
// Declare a viewport object
private Viewport v;
public GameRenderer() {
// initialize after your camera initialization
v = new FitViewport(408, 272, cam); // <- I have use a FitViewport
// but you can use others
// I guess you could remove the setToOrtho() but not too sure...
// ... rest of code
}
Vector3 v = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
v = cam.unproject(v);
// now v has your coordinate world system and you can properly make use of it
// you can do now something like:
tap_x = (int) v.x;
tap_y = (int) v.y;