gpt4 book ai didi

java - 相对于 x 轴运动镜像

转载 作者:行者123 更新时间:2023-12-02 03:28:52 28 4
gpt4 key购买 nike

我正在尝试实现移动到单击鼠标的点。

但是我遇到了 X 轴镜像行为的问题。当我点击顶部 -> 它移动到底部,当我点击底部 -> 它移动到顶部。

这里是原始位置的例子 enter image description here

我点击了屏幕上有红十字的位置。 enter image description here

但它向下移动(如箭头所示)。

有什么问题吗?我认为这似乎与运动 vector 有关。

public class Player {

private static final float PLAYER_CIRCLE_RADIUS = 24f;
private static final float MOVEMENT_SPEED = 200f;
private final Circle playerCircle;

private Vector2 direction = new Vector2();
private Vector2 position;
private Vector2 velocity = new Vector2();
private Vector2 movement = new Vector2();
private Vector2 mouseClick = new Vector2();

public Player(float x, float y) {
position = new Vector2(x, y);
playerCircle = new Circle(x, y, PLAYER_CIRCLE_RADIUS);
}

public void draw(ShapeRenderer shapeRenderer) {
shapeRenderer.circle(position.x, position.y, playerCircle.radius);
}

public void update(float delta) {
movement.set(velocity).scl(delta);
if (position.dst2(mouseClick) > movement.len2()) { position.add(movement); }
else { position.set(mouseClick); }
}

public void setDirection(float x, float y) {
mouseClick.set(x, y);
direction.set(mouseClick).sub(position).nor();
velocity.set(direction).scl(MOVEMENT_SPEED);
}

public Vector2 getDirection() {
return direction;
}

public Circle getPlayerCircle() {
return playerCircle;
}

public Vector2 getMouseClick() {
return mouseClick;
}
}


public class GameScreen extends ScreenAdapter {

private static final float WORLD_WIDTH = 640;
private static final float WORLD_HEIGHT = 480;

private ShapeRenderer shapeRenderer;
private Viewport viewport;
private Camera camera;

private Player player;
private Destination dest;

@Override
public void render(float delta) {
clearScreen();
update(delta);
shapeRenderer.setProjectionMatrix(camera.projection);
shapeRenderer.setTransformMatrix(camera.view);
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);

dest.draw(shapeRenderer);
player.draw(shapeRenderer);

shapeRenderer.end();
}

@Override
public void resize(int width, int height) {
viewport.update(width, height);
}

@Override
public void show() {
camera = new OrthographicCamera();
camera.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2, 0);
viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);
shapeRenderer = new ShapeRenderer();

player = new Player(WORLD_WIDTH / 2, WORLD_HEIGHT / 2);
dest = new Destination();

Gdx.input.setInputProcessor(new InputAdapter() {

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
dest.setPosition(screenX, screenY);
camera.unproject(new Vector3(screenX, screenY, 0));
player.setDirection(screenX, screenY);
return true;
}

});
}

private void clearScreen() {
Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}

private void update(float delta) {
player.update(delta);
}

}

最佳答案

您所描述的实际上是在 Y 轴上镜像的。出现此行为的原因很可能是您的绘图矩阵设置为原点位于左下角,Y 轴朝上,但 gui 的原点位于左上角,Y 轴朝上- 轴朝下。因此,当您获得鼠标位置时,您应该执行以下操作:

actualPosY = screenHeight - mousePosY

这可以有效地将您的鼠标位置转换为绘图空间。

关于java - 相对于 x 轴运动镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392477/

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