Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于计算角度不工作的代码_Java_Math - Fatal编程技术网

Java 用于计算角度不工作的代码

Java 用于计算角度不工作的代码,java,math,Java,Math,我试图计算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 =

我试图计算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);
从分配器发射的火球不是针对玩家的,为什么?它似乎以一个随机的角度移动

编辑这里是fireball类

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;
        }
    }
    }

取绝对值:

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));
}

取绝对值:

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));
}

我的猜测是,您已经将角度转换为度,但稍后在绘制时将其视为弧度角。这就解释了为什么它是在“随机方向”

我的猜测是,你已经将角度转换为度,但稍后在画图时将其视为弧度角。这就解释了为什么它在“随机方向”变化

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

此外,还可以将二维航向向量更改为角度,然后将其更改回二维航向向量。这是一个相当不错的循环三角,让你得到与你开始时相同的答案。你为什么不把它当作向量呢

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);
        }
    }
}
改变

此外,还可以将二维航向向量更改为角度,然后将其更改回二维航向向量。这是一个相当不错的循环三角,让你得到与你开始时相同的答案。你为什么不把它当作向量呢

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);
        }
    }
}

你在期待什么?使用+x轴作为-x方向的角度的基础,稍微向下,这对我来说似乎是正确的?对我来说是正确的。你期望得到什么结果?当seocnd点高于第一个点时,为什么会返回-度“两点之间的度”是多少?两点总是在同一条线上。第三点是否暗示为原点
(0,0)
?弧度与度的问题可能是您所期望的?使用+x轴作为-x方向的角度的基础,稍微向下,这对我来说似乎是正确的?对我来说是正确的。你期望得到什么结果?当seocnd点高于第一个点时,为什么会返回-度“两点之间的度”是多少?两点总是在同一条线上。第三点是否暗示为原点
(0,0)
?弧度与度的问题probably@JoshSobel到底是什么不起作用?这对我来说很好。火球不见了球员。看我的更新question@JoshSobel到底是什么不起作用?这对我来说很好。火球缺少玩家看我更新的问题,但当我用弧度时,我叫托拉迪亚斯看我更新的帖子,但当我用弧度时,我叫托拉迪亚斯看我更新的帖子,我想把它保留为度,因为我在游戏的其他部分使用固定角度,我想把它保留为度因为我在比赛的其他部分使用固定角度