0

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

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

然后创建一个射弹,其中角度为 player.aimingangle,强度为 player.aimingdistance:

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

我得到了令人难以置信的奇怪弹丸,它们到处飞。

4

1 回答 1

2

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

于 2013-09-22T00:41:17.153 回答