因此,我正在开发一个平台游戏,您应该能够在这两个位置之间拖动鼠标,以释放鼠标的方向从角色中发射弹丸。

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


我得到了令人难以置信的怪异弹丸,这些弹丸四处飞扬。

最佳答案

Math.sinMath.cos接受弧度参数,但是您要输入度数。

关于java - Java射弹角度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18939167/

10-11 11:57