2017-11-18 14 views
1

私はユーザーが初期の角度と速度で想像上のオブジェクトをスローしてボールに近づくかどうかを確認できるコードを作成していますユーザー入力時)。投げられたボールの投射動作を描画する必要がある

しかし、私は、仮想オブジェクトの角度と速度を入力したユーザーの曲線を描くことができません。

私は、上記のオブジェクトの合計時間と範囲を計算するために数式を使用しました。それは次のとおりです。

=02sin⁡(180分の2) =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; 
+0

なぜ{{}}の中にいくつかのものを置いていますか?通常、このようなブロックは使用しませんが、ここでは必要ありません。 – Zabuza

+0

@ Zabuza、私は自分のコードの特定の部分を括弧で囲んで入れています+これは、以前のエラーを出さずにプログラム作業に必要な固定ボールを作成するためにユーザー入力を求めるコードの特定の部分を作成しましたX変数にはポイントのために初期化されておらず、X値に必要なユーザー入力がまったく機能しないと教えてくれる前に、私はブラケットに入っていませんでした。それでなぜかっこを置いたのですか? – Yume

+0

を参照してください。https://stackoverflow.com/help/someone-answers – c0der

答えて

0

あなたは、時間の関数として、水平(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

は十分に明確ではないものを尋ねることを躊躇しないでください。

関連する問題