简体   繁体   English

如何使播放器始终指向鼠标

[英]How to make the player always point to the mouse

Just for fun I thought I would create the game Asteroids. 只是为了好玩,我想我会创造游戏Asteroids。 I'm a little bit stuck on the the fact I can't get the player to always look where the mouse is. 我有些无法坚持的事实是,我无法让播放器始终查看鼠标的位置。 Here is the player class below. 这是下面的玩家类别。

public class Player extends GameObject {

    private int size;

    public Player(float x, float y, int width, int height) {
        super(x, y, width, height);
        size = width / 2 + height / 2;
    }

    public void update() {

    }

    public void render(Graphics g) {
        Point mouse = MouseInfo.getPointerInfo().getLocation();
        int centerX = (int) (mouse.getX() - getX());
        int centerY = (int) (mouse.getY() - getY());
        double angle = Math.toDegrees(Math.atan2(centerY, centerX));

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setColor(Color.white);
        g2d.rotate(angle, (int) getX() + getWidth() / 2, (int) getY() + getHeight() / 2);
        g2d.drawLine((int) getX() + getWidth() /  2, (int) getY(), (int) getX(), (int) getY() + getHeight());
        g2d.drawLine((int) getX() + getWidth() /  2, (int) getY(), (int) getX() + getWidth(), (int) getY() + getHeight());
        g2d.drawLine((int) getX(), (int) getY() + getHeight(), (int) getX() + getWidth() / 2, (int) getY() + getHeight() - size / 6);
        g2d.drawLine((int) getX() + getWidth(), (int) getY() + getHeight(), (int) getX() + getWidth() / 2, (int) getY() + getHeight() - size / 6); 
    }

    public Rectangle getBounds() {
        return new Rectangle((int) getX(), (int) getY(), getWidth(), getHeight());
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getSize() {
        return size;
    }

}

I guess it sort of works in the fact that if I move my mouse the player does rotate but it rotate pretty fast. 我猜想这是可以的,因为如果我移动鼠标,播放器会旋转,但是旋转得非常快。 So I ask am I doing anything wrong? 所以我问我做错了什么吗? Before you ask, I have looked it up on Google and found some question like this one but none have helped me. 在您问之前,我已经在Google上进行了查询,发现了类似这样的问题,但没有一个对我有帮助。

The argument to Graphics2D.rotate is in radians, not degrees. Graphics2D.rotate的参数以弧度而不是度为单位。 Try removing Math.toDegrees from the computation of angle. 尝试从angle.计算中删除Math.toDegrees angle. Reference here . 参考这里

Updated : Per this comment , getLocation gives the mouse position in screen coordinates, not in the user coordinates of your Graphics2D . 更新 :根据此注释getLocation以屏幕坐标而不是Graphics2D的用户坐标给出鼠标位置。 You need to convert the coordinates, but how to do that will be specific to your game library. 您需要转换坐标,但是如何实现将取决于您的游戏库。

Updated 2 : Assuming GameObject extends java.awt.Component somehow, try getLocationOnScreen (reference here , from the comment noted above ): 更新2 :假设GameObject以某种方式扩展了java.awt.Component ,请尝试getLocationOnScreen (从上面提到注释中引用此处 ):

Point me_absolute = getLocationOnScreen()
int centerX = (int) (mouse.getX() - me_absolute.getX());
int centerY = (int) (mouse.getY() - me_absolute.getY());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM