我正在尝试计算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;
}
}
}
答案 0 :(得分: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);
}
}
}
答案 1 :(得分: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));
}
答案 2 :(得分:0)
我的猜测是你已经将角度转换为度数,但是当你进行绘图时,它会稍后将其视为弧度角度。这可以解释为什么它处于“随机方向”