我想不出移动球的方法 [英] I cannot figure out a way to move the balls

查看:55
本文介绍了我想不出移动球的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 3 靠垫台球游戏项目.到目前为止,我已经在桌子上添加了两个球.我试图移动其中一个球,但我很难做到这一点.我应该使用计时器吗?如果是这样,那么你能告诉我一种在我的代码上使用计时器的有效方法,以便我可以移动我的球吗?

I am currently working on a 3 cushion billiards game project. I have added two balls on the table so far. I am trying to move one of the balls but I am having a hard time doing that. Should I use a timer? If so then could you tell me an effective way to use the timer on my code so I can move my balls?

非常感谢您的帮助.提前致谢.法尔汉·哈桑

Your help would be much appreciated. Thanks in advance. Farhan Hasan

我试图为班级球创建一个移动函数.但是我不确定应该在函数中放入什么,我添加了 xSpeed 和 ySpeed.xLocation 和 yLocation 根据 xSpeed 和 ySpeed 变化.

I have tried to create a move function for the class balls. But I am not sure what I should put inside the function, I have added the xSpeed and ySpeed. The xLocation and the yLocation changes depending on the xSpeed and ySpeed.

public class Balls
{

    private Color ballFillColor;
    private Color ballBorderColor;

    private int ballX = 0;
    private int ballY = 0;
    private int xSpeed = 5;
    private int ySpeed = 0;
    private int ballWidth = 0;
    private int ballHeight = 0;
    Timer t; 

    public boolean fillBall = false;
    private static Balls ballArray[]; //Required for drawMultipleBalls





    Balls(){ //Constructor
        ballBorderColor = Color.black;
    }

    Balls(int ballX, int ballY, int ballWidth, int ballHeight, Color ballBorderColor, JFrame window){ //Constructor
        // X , Y , Width, Height, Border Colour, container
        this.setBallBorderColor(ballBorderColor);
        this.setBallWidth(ballWidth);
        this.setBallHeight(ballHeight);
        this.setBallX(ballX);
        this.setBallY(ballY);
        this.drawBall(window);
    }


    //Here is the move function. I am not really sure what to do here. 
    public void move()
    {
        if(this.ballX < 1000 - this.ballWidth)
        {
            this.ballX += this.xSpeed; 
        }
        try
        {
            Thread.sleep(1);
        }
        catch(Exception e)
        {

        }
    }

//GET AND SET FUNCTIONS HERE 

//HERE ARE THE FUNCTIONS WHICH ARE RESPONSIBLE FOR DRAWING MY BALLS IN JFRAME

 public void drawBall(JFrame frame) 
    {
        frame.getContentPane().add(new MyComponent());
    }

    public void drawMultipleBalls(JFrame frame, Balls[] balls)
    {
        ballArray = balls;
        frame.getContentPane().add(new MyComponent2());
    }




    private class MyComponent extends JComponent{
        public void paintComponent(Graphics g){

            if (fillBall) //Fill first, and then draw outline.
            {
                g.setColor(ballFillColor);
                g.fillOval(getBallX(),getBallY(), getBallHeight(),getBallWidth());
            }

            g.setColor(getBallBorderColor());
            g.drawOval(getBallX(),getBallY(), getBallHeight(),getBallWidth());

        }
    }

    private class MyComponent2 extends JComponent{
        public void paintComponent(Graphics g){

            for (int i = 0; i < ballArray.length; i++)
            {
                if (ballArray[i].fillBall) //Fill first, and then draw outline.
                {
                    g.setColor(ballArray[i].ballFillColor);
                    g.fillOval(ballArray[i].getBallX(),ballArray[i].getBallY(), ballArray[i].getBallHeight(),ballArray[i].getBallWidth());
                }

                g.setColor(ballArray[i].getBallBorderColor());
                g.drawOval(ballArray[i].getBallX(),ballArray[i].getBallY(), ballArray[i].getBallHeight(),ballArray[i].getBallWidth());
            }
        }
    }

希望我可以为游戏准备两个可移动的球,它们应该会在击中屏幕边缘时反弹回来,并且随着时间的推移它们应该能够放慢速度.为此,我正在考虑使用阻尼器(我将 xSpeed 和 ySpeed 乘以一个小于 1 的数字,最终它会减慢球的速度)

Hopefully, I can have two movable balls for the game, the should bounce back as the hit the edge of the screen and they should be able to slow down over time. For that, I am thinking to use a damper (I will multiply the xSpeed and ySpeed with a number less than 1, eventually it will slow down the ball)

推荐答案

总的来说,您需要弄清楚以下几点:

It seems in general there are a few things you need to figure out:

  1. 球与另一个球相撞
  2. 球撞到墙上了
  3. 否则只需根据球的速度找出球的新位置

下面是一些示例代码,可以将其中的一些存根.您可以先将当前球的位置与所有其他球(当然不包括当前球)进行比较.如果有任何相等的位置,则处理与球的碰撞.如果球在窗口边界处,即它撞到了墙壁,则处理与墙壁的碰撞.否则,只需根据其当前速度计算其新位置.

Below is some sample code that stubs some of this out. You can first compare the current ball's position to all others (not including the current ball of course). If there are any equal positions, process a collision with a ball. If the ball is at the window border i.e it hit a wall, process a collision with a wall. Otherwise just calculate its new position based on its current velocity.

过程碰撞部分只是将物理力学应用于您需要的任何复杂程度.一种普遍建议的更改是更新球的速度,然后将其应用于之后的位置.您可以根据需要应用速度变化的特定计算,并且您可以想象它可能会非常复杂,这就是为什么我建议使用单独的方法和可能的速度子类,而不是管理球本身速度矢量的每个部分.因此,我将墙壁用作对象.物体碰撞的成分、重量、速度等会影响产生的碰撞,但您希望处理的复杂程度取决于您.

The process collision part is just to apply physics mechanics to whatever degree of complexity you require. One general suggested change would be to update the velocity of the balls then apply it to the position after. The specific calculations for velocity changes you could apply as needed and as you can imagine it can get pretty involved which is why I suggest using a separate method and possibly a sub class for velocity instead of managing each part of the velocity vector in the ball itself. I used the wall as an object because of this. The composition, weights, velocities etc of the object's colliding can affect the resulting collision, but how complex you want that processing to be is up to you.

抱歉,我不是物理专家,但我希望这能让您在代码方面朝着正确的方向发展!这也可能有助于您可能想要使用的特定计算:https://www.khanacademy.org/science/physics/one-dimensional-motion/displacement-velocity-time/v/calculating-average-velocity-or-speed

Sorry I'm no physics expert but I hope this sends you in the right direction in terms of code! Also this might help with the specific calculations you might want to use: https://www.khanacademy.org/science/physics/one-dimensional-motion/displacement-velocity-time/v/calculating-average-velocity-or-speed

public void move()
    {
        // check if balls are on same position not including this ball
        for(Ball b: ballArray){
            if (this.position == b.position && this != b){
                processCollision(this, b, null);
            } else{
                // if the ball hasn't collided with anything process its movement based on speed
                // this assumes a 1000 x 1000 window for keeping objects inside it
                if(this.ballX < 1000 - this.ballWidth && this.ballY < 1000 - this.ballHeight){
                    this.ballX += this.xSpeed;
                    this.ballY += this.ySpeed;
                }else {
                    processCollision(this, null, new Wall());
                }
            }
        }
        try
        {
            Thread.sleep(1);
        }
        catch(Exception e)
        {

        }
    }

    public void processCollision(Ball b1, Ball b2, Wall w){
        // if ball hasn't collided with a wall, process a ball - ball collision
        if(w == null){
            // apply physics mechanics according the complexity desired for ball collisions
            b1.xSpeed -= b2.xSpeed;
            b1.ySpeed -= b2.ySpeed;

            // ball 2 would end up slowing down
            b2.xSpeed -= b1.xSpeed;
            b2.ySpeed -= b1.ySpeed;
        }

        // if ball hasn't collided with a ball, process a ball - wall collision
        if(b2 == null){
            // apply physics mechanics for hitting a wall
            // e.g as below: just send ball in opposite direction
            b1.xSpeed = b1.xSpeed * -1;
            b1.ySpeed = b1.ySpeed * -1;
        }

        // either way, process ball's new position based on its new speed
        b1.ballX += b1.xSpeed;
        b1.ballY += b1.ySpeed;

        b2.ballX += b2.xSpeed;
        b2.ballY += b2.ySpeed;
    }

这篇关于我想不出移动球的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆