gpt4 book ai didi

java - 计算角度的代码不起作用

转载 作者:行者123 更新时间:2023-12-01 23:51:27 25 4
gpt4 key购买 nike

我正在尝试计算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;
}
}
}

最佳答案

改变

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 航向 vector 更改为角度,然后将其更改回 2D 航向 vector 。这是相当数量的圆三角,可以让你得到与最初开始时相同的答案。您是否有理由不将其保留为 vector ?

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

关于java - 计算角度的代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251997/

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