需要绘制投掷球的抛射体运动
问题描述:
所以,我写了一个代码,允许用户以初始角度和速度抛出虚拟对象,以查看它们是否可以接近击球(即基于位置在用户输入上)。需要绘制投掷球的抛射体运动
但是,我很难绘制用户输入的角度和虚拟物体速度的曲线。
我已经使用了一个数学公式来计算所述对象的总时间和范围。那就是:
=02sin(一百八十〇分之二) =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;
答
用于绘图需要计算水平(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();
}
}
不要犹豫,问什么是不太清楚。
为什么你在'{...}'中放置一些东西?通常你不使用这种块,它们在这里也不需要。 – Zabuza
@Zabuza,我将代码的某些部分放在括号中以使它更整洁,它使得我的代码的特定部分要求用户输入,以便创建程序工作所需的静止球而不会收到任何以前的错误因为X变量会告诉我它并未针对该点进行初始化,并且X值所需的用户输入根本不起作用,所以我之前没有使用它。因此为什么我放置括号。 – Yume
请参阅:https://*.com/help/someone-answers – c0der