0

我正在尝试计算java中两个点之间的角度。这是我用来计算角度的代码。

public static double calcAngle(Point.Double p1, Point.Double p2)
{
    double xDiff = p2.x - p1.x;
    double yDiff = p2.y - p1.y;
    return Math.toDegrees(Math.atan2(yDiff, xDiff));
}

这是我的其余代码

double playerX = panel.getCharacter().getX();
double playerY = panel.getCharacter().getY();
int dispenserX = x*Block.WIDTH;
int dispenserY = y*Block.HEIGHT;
Point2D.Double player = new Point2D.Double(playerX, playerY);
Point2D.Double dispenser = new Point2D.Double(dispenserX, dispenserY);
double angle = calcAngle(dispenser, player);
System.out.println(angle);
panel.addEntity(newEntityFireball(x*Block.WIDTH,y*Block.HEIGHT,angle,1));//adds a fireball at a 45 degree angle
System.out.println(angle);
System.out.println(dispenser);
System.out.println(player);

发射器发射的火球不是针对玩家的,为什么?它似乎以随机的角度移动。

编辑这里是火球类

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class EntityFireball extends Entity 
{
    private double angle;
    private double speed;
    private int life;

    public EntityFireball(double x, double y, double angle, double speed) 
    {
        super(x, y, 20, 20);
        this.angle = angle;
        this.speed = speed;
    }

    public void update(boolean inRange)
    {
        life++;

        if(life>2500)
            removeEntityFromGame(this);

        if(inRange)
        {
            float xDirection = (float) (Math.sin((float) Math.toRadians(angle)) * speed);
            float yDirection = (float) (Math.cos((float) Math.toRadians(angle)) * -speed);
            double newX = getX() + xDirection;
            double newY = getY() + yDirection;
            setX(newX);
            setY(newY);
        }
    }

    public BufferedImage getImage() 
    {
        try
        {
            return ImageIO.read(Main.class.getResourceAsStream("/images/Fireball.png"));
        } 
        catch (IOException e)
        {
            return null;
        }
    }
    }
4

3 回答 3

2

改变

float xDirection = (float) (Math.sin((float) Math.toRadians(angle)) * speed);
float yDirection = (float) (Math.cos((float) Math.toRadians(angle)) * -speed);

float xDirection = (float) (Math.cos((float) Math.toRadians(angle)) * speed);
float yDirection = (float) (Math.sin((float) Math.toRadians(angle)) * speed);

此外,您将 2D 航向矢量更改为角度,然后将其更改回 2D 航向矢量。这是相当数量的循环三角函数,可以为您提供与最初开始时相同的答案。您是否有理由不将其保留为向量?

public static Point2D.Double calcAngle(Point.Double p1, Point.Double p2){
    double xDiff = p2.x - p1.x;
    double yDiff = p2.y - p1.y;
    return new Point2D.Double(xDiff,yDiff);
}


public class EntityFireball extends Entity {
    private Point2D.Double course;
    private double speed;
    private int life;

    public EntityFireball(double x, double y, double angle, Point2D.Double course){
        super(x, y, 20, 20);
        this.angle = angle;
        this.course=course;
    }

    public void update(boolean inRange){
        life++;
        if(life>2500)
            removeEntityFromGame(this);

        if(inRange){
            float xDirection = course.x;
            float yDirection = course.y;
            double newX = getX() + xDirection;
            double newY = getY() + yDirection;
            setX(newX);
            setY(newY);
        }
    }
}
于 2013-04-27T15:29:16.897 回答
1

只需取绝对值:

public static double calcAngle(Point.Double p1, Point.Double p2)
{
    double xDiff = Math.abs(p2.x - p1.x);
    double yDiff = Math.abs(p2.y - p1.y);
    return Math.toDegrees(Math.atan2(yDiff, xDiff));
}
于 2013-04-27T12:44:06.067 回答
0

我的猜测是您已将角度转换为度数,但稍后在您进行绘图时将其视为弧度角。这可以解释为什么它在“随机方向”

于 2013-04-27T14:51:48.723 回答