Java JFrame弹跳球物理模拟 - 创建创建时球运动冲突

时间:2015-08-16 17:28:10

标签: java swing jframe paint physics

所以,现在,我能够创建多个球并通过Vector对象和for循环正确更新它们以独立更新每个对象。问题在于,对于第一个球之后的每个球,所产生的球似乎会影响其他球的动量和位置,导致所有球突然改变其飞行路径。

基于mouselistener创建框架和管理球创建的代码:

public class BallCreate extends JPanel {    
    Timer timer;
    int diam;
    Color color;
    double vX, vY, posX, posY;
    final double G = 30;

    public BallCreate(double posX, double posY, int diam, double vX, double vY){
        this.posX = posX;
        this.posY = posY;
        this.vX = vX;
        this.vY = vY;
        this.diam = diam;
        color = getRandomColor();
        timer = new Timer(100, new MovementUpdate());
        timer.start();
    }

    public void drawing(){
        repaint();
    }

    public void motionUpdate(){
        for(BallCreate ball : FrameCreation.ballObjects){
            double t = 0.1;
            double dX = ball.vX*t;
            double dY = (ball.vY + G*t)*t;
            double v2Y = G*t + ball.vY;
            System.out.println("Ball v2y: " + ball.vY);
            ball.posX = ball.posX + dX;
            ball.posY = ball.posY + dY;
            vY = v2Y;
            drawing();
            wallCollisionCheck();
        }
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(BallCreate ball : FrameCreation.ballObjects){
            g.setColor(ball.color);
            //System.out.println("pos X: " + posX);
            //System.out.println("pos Y: " + posY);
            g.fillOval((int)ball.posX, (int)ball.posY, 
                       ball.diam ,ball.diam);
        }
        }

    public void wallCollisionCheck(){
        for(BallCreate ball : FrameCreation.ballObjects){
            double botBound = 500-(ball.posY + ball.diam);
            if((botBound <=0 && vY>=0)|| (ball.posY <=0 && ball.vY <=0 )){
                //System.out.println("Collision Noted");
                //System.out.println("Prev vY: " + ball.vY);
                ball.vY = ball.vY * -0.65;
                //System.out.println("Post vY: " + ball.vY);
                ball.posY = 460;
            }
            if((ball.posX <= 0 && ball.vX <= 0) || ((Math.abs(500 - ball.posX) <= 40)&& ball.vX >=0)){
                ball.vX = ball.vX * -0.65;

            }
            drawing();
        }
    }

    public Color getRandomColor(){
        int R = (int)(255*Math.random());
        int G = (int)(255*Math.random());
        int B = (int)(255*Math.random());
        Color c = new Color(R, G , B);
        return c;

    }
    public class MovementUpdate implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            for(BallCreate ball : FrameCreation.ballObjects){
                ball.motionUpdate();
            }
        }
    }   
}

绘制和更新球的代码:

 @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Dialog);

        Window window = dialog.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.getAttributes().windowAnimations = R.style.Share_Multiplayer_Animation;
        return dialog;
    }

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.share_multiplayer_scorecard, container, false);
        ButterKnife.bind(this, view);

        ViewGroup shareContainer = ButterKnife.findById(view, R.id.container);
        List<MultiplayerRound> multiplayerRoundList = mRound.getMultiplayerRoundList();
        for (int i = 0; i < multiplayerRoundList.size(); i++) {
            MultiplayerRound mpRound = multiplayerRoundList.get(i);
            View rowView = inflater.inflate(R.layout.multiplayer_share_row, shareContainer, false);

            TextView nameTv = ButterKnife.findById(rowView, R.id.et_name);
            nameTv.setText(mpRound.getName());

            CheckBox checkBox = ButterKnife.findById(rowView, R.id.cb_send_scorecard);

            final EditText emailET = ButterKnife.findById(rowView, R.id.et_email);
            emailET.addTextChangedListener(new CheckboxControllerTextWatcher(checkBox));
            emailET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    emailET.setSelection(emailET.getText().length());
                }
            });

            String email = getEmail(mpRound);
            emailET.setText(email);

            updateCheckboxState(email, checkBox);

            shareContainer.addView(rowView);
            mPlayerViews[i] = rowView;
            rowView.setTag(mpRound);
        }
        return view;
    }

我意识到这个问题有些模糊,但是如果不能显示正在发生的事情的图像就很难描述。任何帮助表示赞赏,如有必要,可以进行代码说明。感谢。

0 个答案:

没有答案