Java Projectile Angles

时间:2013-09-22 00:38:33

标签: java trigonometry angle

所以,我正在制作一个平台游戏,您应该能够拖动鼠标,从鼠标释放方向发射一个射弹,速度与两点之间的距离。

    public void mouseDragged(MouseEvent e) {
    player.setAiming(true);
    Point2D.Float aimpoint = new Point2D.Float(e.getXOnScreen(), e.getYOnScreen());
    player.setAimpoint(aimpoint);
    player.setAimDistance(GetDistanceToPoint(player.getPos(), aimpoint));
}
public static float GetAngleOfLine(Point2D.Float p1, Point2D.Float p2) { 
    double xDiff = p2.x - p1.x; 
    double yDiff = p2.y - p1.y; 
    System.out.println("P1X: " + p1.x + " P1Y: " + p1.y + " P2X: " + p2.x + " P2Y: " + p2.y + " Angle: " + Math.toDegrees(Math.atan2(yDiff, xDiff)));
    return (float) Math.toDegrees(Math.atan2(yDiff, xDiff));
}
public static float GetDistanceToPoint(Point2D.Float p1, Point2D.Float p2){
    float distance = (float) Math.sqrt(Math.pow(p2.y - p1.y, 2) + Math.pow((p2.x - p1.x), 2));
    return distance;
}

然后,当鼠标被释放时,

player.setAimingangle(GetAngleOfLine(player.getPos(), player.getAimpoint()));

然后创造一个射弹,其中角度是玩家。射击和力量是玩家。射击距离:

        float yvel = (float) (strength * Math.sin(angle));
    float xvel = (float) (strength * Math.cos(angle));

我得到了令人难以置信的奇怪的射弹,它们飞来飞去。

1 个答案:

答案 0 :(得分:2)

Math.sinMath.cos采用弧度参数,但你正在为它们提供学位。