在libgdx中的java中,我试图使我的相机遵循由Shaperender创建的对象。我不想使用精灵,但只能找到精灵的教程。使用SpriteBatch等
我只希望它遵循我在Shaperender中创建的RECT,在其他类别中,我可以在其他类别中获得,但没有数量的相机更新和更改位置的摄像机。是否可以使用无精灵的拼字摄像头?
public class Main extends ApplicationAdapter {
Ground g;
ShapeRenderer sr;
Player square;
OrthographicCamera camera;
@Override
public void create() {
sr = new ShapeRenderer();
g = new Ground(0, 20,Gdx.graphics.getWidth(), 5, new Color(Color.WHITE));
square = new Player(20, 25, 50,50, new Color(Color.PURPLE), 3);
camera = new OrthographicCamera();
camera.update();
}
@Override
public void render() {
ScreenUtils.clear(0.15f, 0.15f, 0.2f, 1f);
camera.lookAt(square.getX(), square.getY(), 0);
handleInput();
camera.update();
float dt = Gdx.graphics.getDeltaTime();
square.moveplayer(g, dt);
sr.begin(ShapeRenderer.ShapeType.Filled);
g.draw(sr);
sr.end();
sr.begin(ShapeRenderer.ShapeType.Filled);
square.draw(sr);
sr.end();
}
private void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.DPAD_LEFT)) {
camera.translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DPAD_RIGHT)) {
camera.translate(3, 0, 0);
}
}
您可以看到我尝试了很多东西,例如根据用户输入移动屏幕或查看对象,但也许我缺少一些愚蠢的东西,因为没有任何变化/发生。
我假设这是针对2D,您不需要使用
Camera
,因为它总是只观察一个区域。您只需将
.position
的
Camera
设置为您想要观察的任何坐标。
camera.position.set(5, 6, 0); // setting position to 5, 6
还要注意,您尚未为
Camera
定义一个视口,也就是说,您尚未定义它看到的世界多少。
在下面的示例中,有很多方法可以进行设置,以便尊重窗口的长宽比,并且可见一定数量的水平世界单位。
fulther,您需要将ShapeRenderer
的投影矩阵设置为相机的组合矩阵。
shapeRenderer.setProjectionMatrix(camera.combined);
package com.bornander.gdxsandbox;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ScreenUtils;
public class SandboxGame extends ApplicationAdapter {
private OrthographicCamera camera;
private ShapeRenderer shapeRenderer;
private Vector2 moverPosition = new Vector2(0, 0);
private Vector2 moverTargetPosition = new Vector2(0, 0);
private Vector2 delta = new Vector2(MathUtils.random(-50, 50), MathUtils.random(-50, 50));
private boolean cameraFollowMover = false;
@Override
public void create () {
// Set up a camera and define how much it is supposed to "see",
// in this case I create a camera that views 100 units wide, and what ever units tall
// that the current aspect ratio of the window requires
float aspectRatio = (float)Gdx.graphics.getWidth() / Gdx.graphics.getHeight();
float w = 100.0f;
camera = new OrthographicCamera(w, w / aspectRatio);
camera.position.set(0, 0, 0); // Camera is looking at (0, 0)
shapeRenderer = new ShapeRenderer();
}
@Override
public void render () {
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE))
cameraFollowMover = !cameraFollowMover;
// At random times, set a new target for the mover
if (MathUtils.random() > 0.96f)
moverTargetPosition.set(MathUtils.random(-50, 50), MathUtils.random(-50, 50));
delta.set(moverTargetPosition).sub(moverPosition).nor().scl(Gdx.graphics.getDeltaTime() * 8.0f);
moverPosition.add(delta);
if (cameraFollowMover)
camera.position.set(moverPosition.x, moverPosition.y, 0);
camera.update();
ScreenUtils.clear(Color.BLACK);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
// Draw a grid
shapeRenderer.setColor(Color.LIGHT_GRAY);
for(int x = 0; x < 100; x += 10) {
shapeRenderer.setColor(x == 0 ? Color.GREEN : Color.LIGHT_GRAY);
shapeRenderer.line(x, -100, x, 100);
shapeRenderer.line(-x, -100, -x, 100);
}
for(int y = 0; y < 100; y += 10) {
shapeRenderer.setColor(y == 0 ? Color.RED : Color.LIGHT_GRAY);
shapeRenderer.line(-100, y, 100, y);
shapeRenderer.line(-100, -y, 100, -y);
}
shapeRenderer.setColor(Color.MAGENTA);
shapeRenderer.circle(moverPosition.x, moverPosition.y, 8, 24);
shapeRenderer.end();
}
}