2016-10-04 4 views
0

Eclipse에서 Pong을 자바로 만들고 있습니다. 나는 공을 구덩이에 닿은 부분에 비례하여 튕겨 내려고 노력하고 있습니다. 그들이 맨 위나 맨 아래로 부딪쳤다면, 나는 맨 위 75도에서 아래로, 또는 맨 아래에서 75도 아래로 튀어 나오고 싶습니다. 그들이 센터 근처에서 치면, 나는 그들이 수평에 더 가까이에서 튀어 나오게하고 싶습니다.Pong Ball Leaving Padel

double speed = getSpeed() + ballIncrease; 
yPos = Math.abs(yPos); 
double angle = multiplier * yPos; 
double ratio = Math.tan(angle); 
xSpeed = ratio * (speed/(ratio + 1)); 
ySpeed = Math.sqrt((speed * speed) - (xSpeed * xSpeed)); 

나는 그것이 정확해야한다고 생각하지만, 나는 그들에 기대처럼 볼이 동작하지 않습니다

나는이 수학에 공 클래스의 각도를 계산하고 있습니다. 아무도 내가이 문제를 해결할 수 있는지 궁금합니다.

package Pong; 

/*Place cursor here to start 
* 
*/ 

//Import Libraries 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.Random; 

@SuppressWarnings("serial") 
public class Pong extends JPanel implements KeyListener{ 

//Create random instances 
Random r2 =new Random(); 

public final static int padelStart = 150; 
public final static int padelSpace = 10; 

final static int BOX_WIDTH = 1200; 
public final static int BOX_HEIGHT = 800; 

double ballX = BOX_WIDTH/2; 
double ballY = BOX_HEIGHT/2; 

public static Padel padel1 = new Padel(padelSpace,padelStart,true); 
public static Padel padel2 = new Padel(BOX_WIDTH-padelSpace-padel1.getWidth(),padelStart,false); 

Ball ball1 = new Ball(ballX,ballY); 
Ball ball2 = new Ball(300,300); 

public static int padel1y1; 
public static int padel1y2; 
public static int padel2y1; 
public static int padel2y2; 

//Constants 
public final static int UPDATE_RATE = 200; 

//Main game class 
public Pong() { 

    //Set window size 
    setPreferredSize(new Dimension(BOX_WIDTH,BOX_HEIGHT)); 

    //Start game thread 
    Thread gameThread = new Thread() { 

     public void run(){ 

      while(true){ 

       //Draw objects 

       padel1y1 = padel1.getY(); 
       padel1y2 = padel1.getY() + Padel.padelLength; 
       padel2y1 = padel2.getY(); 
       padel2y2 = padel2.getY() + Padel.padelLength; 

       padel1.update(); 
       padel2.update(); 

       ball1.update(); 
       ball2.update(); 
       repaint(); 

       //Wait 
       try {Thread.sleep(1000/UPDATE_RATE);} 
       catch (InterruptedException ex) {} 

      } 

     } 

    }; 

gameThread.start(); 

} 

@Override 
public void paintComponent(Graphics g) { 

    super.paintComponent(g); 

    g.setColor(Color.white); 
    g.fillRect(0,0,BOX_WIDTH,BOX_HEIGHT); 

    padel1.draw(g); 
    padel2.draw(g); 
    ball1.draw(g); 
    ball2.draw(g); 

} 

public void keyPressed(KeyEvent e){ 

    if(e.getKeyCode() == KeyEvent.VK_UP){padel2.setSpeed(-Padel.padelSpeed);} 
    if(e.getKeyCode() == KeyEvent.VK_DOWN){padel2.setSpeed(Padel.padelSpeed);} 

    if(e.getKeyCode() == KeyEvent.VK_W){padel1.setSpeed(-Padel.padelSpeed);} 
    if(e.getKeyCode() == KeyEvent.VK_S){padel1.setSpeed(Padel.padelSpeed);} 

} 

public void keyReleased(KeyEvent e) { 

    if(e.getKeyCode() == KeyEvent.VK_UP){padel2.setSpeed(0);} 
    if(e.getKeyCode() == KeyEvent.VK_DOWN){padel2.setSpeed(0);} 

    if(e.getKeyCode() == KeyEvent.VK_W){padel1.setSpeed(0);} 
    if(e.getKeyCode() == KeyEvent.VK_S){padel1.setSpeed(0);} 


} 

public void keyTyped(KeyEvent e) {} 

public static void main(String[] args) { 

    javax.swing.SwingUtilities.invokeLater(new Runnable() { 

     public void run() { 

      //Create Frame 
      JFrame frame = new JFrame("PONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONG"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      Pong pong = new Pong(); 
      frame.setContentPane(pong); 
      frame.setSize(BOX_WIDTH,BOX_HEIGHT); 
      frame.pack(); 
      frame.addKeyListener(pong); 
      frame.setVisible(true); 

     } 

    }); 

} 

} 

패들 등급 :

package Pong; 

import java.awt.*; 
import java.util.Random; 

public class Padel { 

public int y; 
public int x; 

public boolean ws; 

public int speed = 0; 

public final static int padelSpeed = 3; 
public final static int padelWidth = 30; 
public final static int padelLength = 200; 

Random rng = new Random(); 

int r = rng.nextInt(256); 
int g = rng.nextInt(256); 
int b = rng.nextInt(256); 

Color color = new Color(r,g,b); 

public Padel(int xPos, int yPos, boolean wands){ 

    y = yPos; 
    x = xPos; 

    ws = wands; 

} 

public void update(){ 

    if(speed > 0 && y + padelLength < Pong.BOX_HEIGHT){ 

     y = y + speed; 

    } 

    if(speed < 0 && y > 0){ 

     y = y + speed; 

    } 

} 

public void draw(Graphics g) { 

    g.setColor(color); 
    g.fillRoundRect(x, y, padelWidth, padelLength, 25, 25); 

} 

public int getX(){ 

    return x; 

} 

public int getY(){ 

    return y; 

} 

public int getWidth(){ 

    return padelWidth; 

} 

public int getLength(){ 

    return padelLength; 

} 

public int getSpeed(){ 

    return speed; 

} 

public void setSpeed(int speed1){ 

    speed = speed1; 


} 

public int getPadelSpeed(){ 

    return padelSpeed; 

} 

} 

볼 등급 :

package Pong; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.util.Random; 

public class Ball { 

Random rng = new Random(); 

int r = rng.nextInt(256); 
int g = rng.nextInt(256); 
int b = rng.nextInt(256); 

Color color = new Color(r,g,b); 

public final static double ballSpeedMin = -2.0; 
public final static double ballSpeedMax = 2.0; 
public final static double middleSpeedCutoff = 1.0; 

public final static double ballIncrease = .2; 

public final int ballRadiusMin = 10; 
public final int ballRadiusMax = 40; 

double x; 
double y; 

double xSpeed; 
double ySpeed; 

int ballRadius; 

public Ball(double xPos,double yPos){ 

    x = xPos; 
    y = yPos; 

    xSpeed = getRandomSpeed(); 
    ySpeed = getRandomSpeed(); 

    ballRadius = getRandomRadius(); 

} 

public double getRandomSpeed(){ 

    Random r =new Random(); 

    return ballSpeedMin + (ballSpeedMax - ballSpeedMin) * r.nextDouble(); 

} 

public int getRandomRadius(){ 

    Random r = new Random(); 

    return r.nextInt((ballRadiusMax - ballRadiusMin) + 1) + ballRadiusMin; 

} 

public void update(){ 

    x = x + xSpeed; 
    y = y + ySpeed; 

    ySpeed = verticalBounce(); 

    double multiplier = .75; 

    if(getLowX() < Pong.padelSpace + Padel.padelWidth){ 

     if(y>=Pong.padel1y1 && y<=Pong.padel1y2){ 

      double yPos = y - Pong.padel1y1 - (Padel.padelLength/2); 

      if(yPos<0){ 
       double speed = getSpeed() + ballIncrease; 
       yPos = Math.abs(yPos); 
       double angle = multiplier * yPos; 
       double ratio = Math.tan(angle); 
       xSpeed = ratio * (speed/(ratio + 1)); 
       ySpeed = -Math.sqrt((speed * speed) - (xSpeed * xSpeed)); 
      } 

      else if(yPos>0){ 
       double speed = getSpeed() + ballIncrease; 
       yPos = Math.abs(yPos); 
       double angle = multiplier * yPos; 
       double ratio = Math.tan(angle); 
       xSpeed = ratio * (speed/(ratio + 1)); 
       ySpeed = Math.sqrt((speed * speed) - (xSpeed * xSpeed)); 
      } 

      else{ 
       double speed = getSpeed(); 

       xSpeed = speed; 
       ySpeed = 0; 

      } 

     } 

     else{ 

      System.exit(0); 

     } 

    } 

    if(getHighX() > Pong.BOX_WIDTH - Pong.padelSpace - Padel.padelWidth){ 

     if(y>Pong.padel2y1 && y<Pong.padel2y2){ 

      double yPos = y - Pong.padel2y1 - (Padel.padelLength/2); 

      if(yPos<0){ 
       double speed = getSpeed() + ballIncrease; 
       yPos = Math.abs(yPos); 
       double angle = multiplier * yPos; 
       double ratio = Math.tan(angle); 
       xSpeed = - ratio * (speed/(ratio + 1)); 
       ySpeed = -Math.sqrt((speed * speed) - (xSpeed * xSpeed)); 
      } 

      else if(yPos>0){ 
       double speed = getSpeed() + ballIncrease; 
       yPos = Math.abs(yPos); 
       double angle = multiplier * yPos; 
       double ratio = Math.tan(angle); 
       xSpeed = - ratio * (speed/(ratio + 1)); 
       ySpeed = Math.sqrt((speed * speed) - (xSpeed * xSpeed)); 
      } 

      else{ 
       double speed = getSpeed(); 

       xSpeed = -speed; 
       ySpeed = 0; 

      } 

     } 

     else{ 

      System.exit(0); 

     } 

    } 

} 

public void draw(Graphics g) { 

    g.setColor(color); 

    int xPos = (int) Math.round(x) - ballRadius; 
    int yPos = (int) Math.round(y) - ballRadius; 

    g.fillOval(xPos,yPos,ballRadius*2,ballRadius*2); 

} 

public double verticalBounce(){ 

    if(y - ballRadius<0 || y + ballRadius > Pong.BOX_HEIGHT){ 

     return -ySpeed; 

    } 

    else{ 

     return ySpeed; 

    } 
} 

public double getY(){ 

    return y; 

} 

public double getX(){ 

    return x; 

} 

public double getYSpeed(){ 

    return ySpeed; 

} 

public double getXSpeed(){ 

    return xSpeed; 

} 

public void setYSpeed(double y){ 

    ySpeed = y; 

} 

public void setXSpeed(double x){ 

    xSpeed = x; 

} 

public double getLowX(){ 

    return x - ballRadius; 

} 

public double getLowY(){ 

    return y - ballRadius; 

} 

public double getHighX(){ 

    return x + ballRadius; 

} 

public double getHighY(){ 

    return y + ballRadius; 

} 

public double getSpeed(){ 

    return Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed)); 

} 

} 
+0

Hello Liam은 코드를 너무 많이 보지 않고 볼을 쉽게 제어 할 수 있도록 여러 가지 방법을 권장합니다. 예를 들어,이 답변에서와 같이 모든 업데이트에서 볼의 x 및 y 속도를 결정하는 각도를 사용해보십시오. http://stackoverflow.com/questions/11720293/need-better-way-of-making-the-ball- angle-in-pong-game? rq = 1 그런 다음 충돌시 볼의 각을 사용하여 이탈 각을 결정할 수 있습니다. –

+0

그들이 무엇을 기대합니까? 그들은 어떻게 행동하고 있습니까? 이 작업을 올바르게 수행하려면 물리 엔진을 찾는 것이 좋습니다. – duffymo

답변

1
double multiplier = .75; 
    //... 

      double yPos = y - Pong.padel1y1 - (Padel.padelLength/2); 
      //... 
      double angle = multiplier * yPos; 

당신은 당신이 삼각 함수에 전달하는 각도는 라디안으로 표시되는 것을 알고 있습니까?
귀하의 패들 길이를 가정하면 yPos[-10, 10] 사이에서 변합니다. 귀하의 각도는 [-7.5, 7.5] 사이입니다. 이것을 라디안으로 변환하면 angle이 원 주위를 2 회 이상 회전하는 것을 볼 수 있습니다. 정말이게 니가 원하는거야?

+0

고마워요! 내 게임은 완벽하게 작동하고 있으며, 라디안으로 작동하는지 모릅니다. –