需要绘制抛球的抛射运动

时间:2017-11-18 01:33:41

标签: java swing curve projectile

所以我编写了一个代码,允许用户以初始角度和速度投掷一个虚构物体,看看它们是否能够接近击球(根据用户输入定位)。

然而,我在绘制用户输入的虚拟对象的角度和速度的曲线时遇到了麻烦。

我使用数学公式来计算所述物体的总时间和范围。那就是:

=02sin⁡(2/180)  =20sin⁡(/ 180)

我已经尝试过将范围和总时间放入弧线并以此方式绘制。但这似乎不起作用。

以下是代码:

import java.awt.AWTEvent;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.util.Scanner;
import java.awt.geom.QuadCurve2D;
public class BallGame extends JFrame {

    public static void main (String[]args)
    {
        Scanner cool= new Scanner(System.in);
        double angle, speed, range, totalTime;

        {
            JFrame frame = new JFrame (" Throwing Ball");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(700,700);
        }

        {
            System.out.println("Please enter the location of the ball (0 < X < 1)");
            StdDraw.setPenRadius(0.06);
            StdDraw.setPenColor(StdDraw.BLUE);
            double x, y;
            y = 0;
            x = cool.nextDouble();
            StdDraw.point(x, y);
        }

        System.out.println("Please enter an angle of your choosing:");
        angle = cool.nextDouble();

        System.out.println("Please enter the speed at wish you which to throw the ball");
        speed = cool.nextDouble();

        double g;
        g = 9.8;
        range = Math.pow(speed, 2) * Math.sin(2 * angle * (Math.PI / 180) / g);
        totalTime = (2 * speed * Math.sin(angle * Math.PI / 180)) / g;

1 个答案:

答案 0 :(得分:1)

要绘制曲线,您需要计算水平(x)和垂直(y)位置,作为时间,起点,起始速度和角度的函数。 换句话说,计算水平距离和垂直距离。 这些方程是众所周知的widely available

使用这些方程式重复计算x,y然后重新绘制 请参阅以下演示。不是评论:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class BalisticCurve extends JFrame {

    private static final double G = 9.8; //positive because Y axis is positive going down
    private int animationSpeed = 5; //millis. The smaller the faster
    private static int size = 900, ballDiameter = 10;
    private double startX, startY, ballX, ballY;
    private double xSpeed, ySpeed, lastPointX, lastPointY;
    private double time, deltaTime = 0.01 ; //in seconds
    private List<Point2D> curvePoints= new ArrayList<>();
    private Timer timer;

    BalisticCurve(){

        super("Balistic Curve");
        DrawBoard board  = new DrawBoard();
        add(board, BorderLayout.CENTER);

        ballX= lastPointX = startX = 50;
        ballY = lastPointY = startY = size - 100;
        getUserInput();

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        timer = new Timer(animationSpeed, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {

                board.moveBall();
                board.repaint();
                if(! inBounds()) {
                    timer.stop();
                }
            }
        });
        timer.start();
    }

    private void getUserInput() {

        double angle = 45;//todo replace with user input + verification
        double speed = 100;
        xSpeed = speed * Math.cos(angle * (Math.PI / 180));
        ySpeed = speed * Math.sin(angle * (Math.PI / 180));
    }

    private boolean inBounds() {

        //ignore if ball exceeds height
        if((ballX < 0) || (ballX > (getWidth()))
                || ( ballY  > (getHeight() - ballDiameter) ) ) {
            return false;
        }

        return true;
    }

    class DrawBoard extends JPanel {

        public DrawBoard() {
            setPreferredSize(new Dimension(size, size));
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(Color.RED);
            g2d.fillOval((int)ballX,(int)ballY,ballDiameter,ballDiameter);

            if((Math.abs(lastPointX - ballX)>=1) && (Math.abs(lastPointY - ballY)>=1) ) {
                curvePoints.add(new Point2D.Double(ballX, ballY));
                lastPointX = ballX; lastPointY = ballY;
            }

            drawCurve(g2d);
        }

        private void drawCurve(Graphics2D g2d) {

            g2d.setColor(Color.BLUE);
            for(int i=0; i < (curvePoints.size()-1); i++) {

                Point2D from = curvePoints.get(i);
                Point2D to = curvePoints.get(i+1);
                g2d.drawLine((int)from.getX(),(int)from.getY(), (int)to.getX(), (int)to.getY());
            }
        }

        private void moveBall() {

            ballX = startX + (xSpeed * time);
            ballY = startY - ((ySpeed *time)-(0.5 *G * Math.pow(time, 2))) ;
            time += deltaTime;
        }
    }

    public static void main(String[] args) {

        new BalisticCurve();
    }
}

enter image description here

不要犹豫什么不清楚。