gpt4 book ai didi

java - 如何在 Libgdx Java 中实现缩放和平移?

转载 作者:行者123 更新时间:2023-11-30 01:40:01 25 4
gpt4 key购买 nike

我想在我的游戏中添加缩放和平移机制,但我在网上查找的所有内容都完全失败了。

如果你能给我一个实现这些功能的好例子,那就太好了。

这是我试图让它工作的类(class)。

package com.adam.finis.screens;

import com.adam.finis.Main;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

public class Play extends ApplicationAdapter implements Screen, GestureListener, ApplicationListener{
//Core Variables
private Main game;
public static InputMultiplexer inputMultiPlex;
//Texture Variables
Texture Title;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
//Sprite Variables
public static boolean spawnSprite = false;
//Font
private BitmapFont font;
//Window Variables
private OrthographicCamera gameCam;
private Viewport gamePort;
private PlayHud hudPlay;
private int mapX = 1952;
private int mapY = 1952;
private int mapHalfX = mapX / 2;
private int mapHalfY = mapY / 2;
public static boolean GAME_PAUSED = false;

//Random Variables
private Vector2 dragOld, dragNew;
public static Vector2 worldSize;
//DEBUG
private String message;
private Texture debugTexture;
private Sprite debugSprite;


public Play(Main game){
this.game = game;
gameCam = new OrthographicCamera();
gameCam.setToOrtho(false, Main.V_WIDTH, Main.V_HEIGHT);
gamePort = new FitViewport(Main.V_WIDTH, Main.V_HEIGHT, gameCam);
hudPlay = new PlayHud(game.sb);

mapLoader = new TmxMapLoader();
map = mapLoader.load("images/level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map);

gameCam.position.set(mapHalfX, mapHalfY, 0);

GestureDetector gd = new GestureDetector(this);
inputMultiPlex = new InputMultiplexer();
inputMultiPlex.addProcessor(hudPlay.stage);
inputMultiPlex.addProcessor(hudPlay.debugStage);
inputMultiPlex.addProcessor(gd);
Gdx.input.setInputProcessor(gd);

debugTexture = new Texture(Gdx.files.internal("images/house.png"));
debugSprite = new Sprite(debugTexture);

worldSize = new Vector2(mapX, mapY);

font = new BitmapFont(Gdx.files.internal("fonts/lemonMilk.fnt"),false);
font.setColor(Color.RED);
}

@Override
public void show() {

}

public void handleInput(float dt){
//Keyboard Settings
if (Gdx.input.isKeyPressed(Input.Keys.W)) {
gameCam.position.y += 350 * dt;
}

if (Gdx.input.isKeyPressed(Input.Keys.A)) {
gameCam.position.x -= 350 * dt;
}
if (Gdx.input.isKeyPressed(Input.Keys.S)) {
gameCam.position.y -= 350 * dt;
}

if (Gdx.input.isKeyPressed(Input.Keys.D)) {
gameCam.position.x += 350 * dt;
}
//ZOOM
if (Gdx.input.isKeyPressed(Input.Keys.O)) {
gameCam.zoom += 1.5f * dt;
}

if (Gdx.input.isKeyPressed(Input.Keys.P)) {
gameCam.zoom -= 1.5f * dt;
}
//CAMERA BOUNDS
gameCam.zoom = MathUtils.clamp(gameCam.zoom, 0.1f, mapX / gameCam.viewportWidth);
//|
float camX = gameCam.position.x;
float camY = gameCam.position.y;
//|
Vector2 camMin = new Vector2(gameCam.viewportWidth, gameCam.viewportHeight);
Vector2 camMax = new Vector2(1952, 1952);
//|
camMin.scl(gameCam.zoom/2);
camMax.sub(camMin);
//|
camX = Math.min(camMax.x, Math.max(camX, camMin.x));
camY = Math.min(camMax.y, Math.max(camY, camMin.y));
gameCam.position.set(camX, camY, gameCam.position.z);
//------------------------------------------------------------------------------------

//Touch Settings
if (Gdx.input.justTouched()){
dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
dragOld = dragNew;
}

if (Gdx.input.isTouched()){
dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
if (!dragNew.equals(dragOld)){
gameCam.translate(dragOld.x - dragNew.x, dragNew.y - dragOld.y);
dragOld = dragNew;
}
}
}

public void update(float dt){
handleInput(dt);
gameCam.update();
renderer.setView(gameCam);
}

@Override
public void render(float delta) {
if(GAME_PAUSED == false){
update(delta);
//CLEAR SCREEN - BLACK
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

//INIT ALL INPUT
Gdx.input.setInputProcessor(inputMultiPlex);

//RENDER MAP
renderer.setView(gameCam);
renderer.render();
//DRAW
if(spawnSprite == true){
game.sb.begin();
game.sb.draw(debugSprite, 1500, 500);
game.sb.end();
}
//DRAW HUD
hudPlay.stage.getViewport().apply();
hudPlay.stage.act();
hudPlay.stage.draw();
//debug DRAW HUD
hudPlay.debugStage.getViewport().apply();
hudPlay.debugStage.act();
hudPlay.debugStage.draw();
//PROJECTION
game.sb.setProjectionMatrix(gameCam.combined);
game.hudSb.setProjectionMatrix(hudPlay.debugStage.getCamera().combined);
game.hudSb.setProjectionMatrix(hudPlay.stage.getCamera().combined);


if(Main.zoomOut == true){
gameCam.zoom += 1.5f * delta;
}

if(Main.zoomIn == true){
gameCam.zoom -= 1.5f * delta;
}
}

if(GAME_PAUSED == true){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.input.setInputProcessor(inputMultiPlex);
game.sb.setProjectionMatrix(hudPlay.debugStage.getCamera().combined);
hudPlay.debugStage.getViewport().apply();
hudPlay.debugStage.act();
hudPlay.debugStage.draw();
}
}

@Override
public void resize(int width, int height) {
gamePort.update(width, height);
gameCam.viewportWidth = width/5f; //We will see width/32f units!
gameCam.viewportHeight = gameCam.viewportWidth * height/width;
hudPlay.stage.getViewport().update(width, height, true);
hudPlay.debugStage.getViewport().update(width, height, true);
}

@Override
public void pause() {}

@Override
public void resume() {}

@Override
public void hide() {}

@Override
public void dispose() {
game.sb.dispose();
renderer.dispose();
hudPlay.dispose();
font.dispose();
}

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
return false;
}

@Override
public boolean tap(float x, float y, int count, int button) {
message = "TAP";
Gdx.app.log("INFO", message);
return false;
}

@Override
public boolean longPress(float x, float y) {
message = "LONG PRESS";
Gdx.app.log("INFO", message);
return false;
}

@Override
public boolean fling(float velocityX, float velocityY, int button) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
message = "PAN";
Gdx.app.log("INFO", message);
return false;
}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean zoom(float initialDistance, float distance) {
message = "Zoom performed";
Gdx.app.log("INFO", message);
return false;
}

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2) {
message = "Pinch performed";
Gdx.app.log("INFO", message);

return true;
}
}

最佳答案

你自己的答案是对的,但这里有一个改进版本,在前一次缩放的基础上进行第二次缩放。

此外,相机平移速度与当前相机变焦成正比。

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
message = "PAN";
Gdx.app.log("INFO", message);

gameCam.translate(-deltaX * currentZoom,deltaY * currentZoom);
gameCam.update();
return false;
}
@Override
public boolean zoom(float initialDistance, float distance) {
message = "Zoom performed";
Gdx.app.log("INFO", message);

gameCam.zoom = (initialDistance / distance) * currentZoom;
gameCam.update();

return true;
}
@Override
public boolean panStop(float x, float y, int pointer, int button) {
Gdx.app.log("INFO", "panStop");
currentZoom = gameCam.zoom;
return false;
}

关于java - 如何在 Libgdx Java 中实现缩放和平移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com