Reputation: 1318
I am trying to calculate the angle in degrees between tow points in java. This is the code i am using to calculate the angle.
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));
}
Here is the rest of my code
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);
The fireball fired from the dispenser is not aimed at the player why? And it seems to move at a random angle.
Edit here is the fireball class
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;
}
}
}
Upvotes: 0
Views: 316
Reputation: 2524
change
float xDirection = (float) (Math.sin((float) Math.toRadians(angle)) * speed);
float yDirection = (float) (Math.cos((float) Math.toRadians(angle)) * -speed);
to
float xDirection = (float) (Math.cos((float) Math.toRadians(angle)) * speed);
float yDirection = (float) (Math.sin((float) Math.toRadians(angle)) * speed);
Also, you are changing the 2D course vector into an angle, and then changing it back to a 2D course vector. It's a decent amount of circular trig that lands you the same answer that you initially started with. Is there a reason you don't just leave it a 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);
}
}
}
Upvotes: 2
Reputation: 16392
My guess is that you've converted the angle to degrees but are treating it later as a radian angle when you do your drawing. That would explain why it's in "random directions"
Upvotes: 0
Reputation: 13177
Just take the absolute values:
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));
}
Upvotes: 1