如何在重力弹跳球应用程序中施加重力? [英] How do I apply gravity to my bouncing ball application?

查看:125
本文介绍了如何在重力弹跳球应用程序中施加重力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个相当简单的Java应用程序,它允许您拖动鼠标,并根据鼠标拖动的长度,它将向该方向射击一个球,并随其弹起墙壁.

I've written a fairly simple java application that allows you to drag your mouse and based on the length of the mouse drag you did, it will shoot a ball in that direction, bouncing off walls as it goes.

以下是快速截图:
替代文本http://img222.imageshack.us/img222/3179/ballbouncemf9.png

Here is a quick screenshot:
alt text http://img222.imageshack.us/img222/3179/ballbouncemf9.png

屏幕上的每个圆圈都是一个Ball对象.球的运动分解为x和y向量;

Each one of the circles on the screen is a Ball object. The balls movement is broken down into an x and y vector;

public class Ball {
    public int xPos;
    public int yPos;
    public int xVector;
    public int yVector;

    public Ball(int xPos, int yPos, int xVector, int yVector) {
        this.xPos = xPos;
        this.yPos = yPos;
        this.xVector = xVector;
        this.yVector = yVector;
    }

    public void step()
    {
        posX += xVector;
        posY += yVector;

        checkCollisions();
    }

    public void checkCollisions()
    {
        // Check if we have collided with a wall
        // If we have, take the negative of the appropriate vector
        // Depending on which wall you hit
    }

    public void draw()
    {
        // draw our circle at it's position
    }
}

这很好用.所有的球都从一壁到另一壁反弹.

This works great. All the balls bounce around and around from wall to wall.

但是,我决定要包含重力的影响.我知道物体以9.8m/s的速度向地球加速,但是我不直接知道这应该如何转换为代码.我意识到yVector会受到影响,但是我对此进行的实验并没有达到我想要的效果.

However, I have decided that I want to be able to include the effects of gravity. I know that objects accelerate toward the earth at 9.8m/s but I don't directly know how this should translate into code. I realize that the yVector will be affected but my experimentation with this didn't have the desired effect I wanted.

理想情况下, 我希望能够在此程序中添加一些重力效果,并允许球反弹几次,然后再稳定到地面上.

Ideally, I would like to be able to add some gravity effect to this program and also allow the balls to bounce a few times before settling to the "ground."

如何创建弹跳弹性重力效应?在每一步中我该如何操纵球的速度矢量?当它到达地面"时必须做什么,以便我可以让它再次弹起,但比上一次短一些?

How can I create this bouncing-elastic, gravity effect? How must I manipulate the speed vectors of the ball on each step? What must be done when it hits the "ground" so that I can allow it to bounce up again, but somewhat shorter then the previous time?

在向正确的方向指点我时,我们将提供任何帮助.

Any help is appreciated in pointing me in the right direction.

谢谢大家的评论!它已经很好用了!

Thanks you for the comments everyone! It already is working great!

在step()中,我像人们建议的那样向我的yVector添加了一个重力常数,这就是我的checkCollision():

In my step() I am adding a gravity constant to my yVector like people suggested and this is my checkCollision():

public void checkCollision()
{
    if (posX - radius < 0)              // Left Wall?
    {
        posX = radius;              // Place ball against edge
        xVector = -(xVector * friction);
    }
    else if (posX + radius > rightBound) // Right Wall?
    {
        posX = rightBound - radius;     // Place ball against edge
        xVector = -(xVector * friction);
    }

    // Same for posY and yVector here.
}

但是,这些球将继续在地板上滑动/滚动.我认为这是因为每次反弹时我只是取其向量的一个百分比(90%),而它永远不会真正为零.我应该添加一个检查,如果xVector变为某个绝对值,我应该将其更改为零吗?

However, the balls will continue to slide around/roll on the floor. I assume this is because I am simply taking a percentage (90%) of their vectors each bounce and it is never truly zero. Should I add in a check that if the xVector becomes a certain absolute value I should just change it to zero?

推荐答案

您要做的就是不断从yVector中减去一个小的常数(代表您的9.8 m/s的常数).当球下降时(yVector已经为负),这会使球走得更快.上升时(yVector为正)会减慢其速度.

What you have to do is constantly subtract a small constant (something that represents your 9.8 m/s) from your yVector. When the ball is going down (yVector is already negative), this would make it go faster. When it's going up (yVector is positive) it would slow it down.

这不会解决摩擦,因此事物应该永远反弹很多.

This would not account for friction, so the things should bounce pretty much for ever.

edit1: 为了解决摩擦,只要它反转了(并且您反转了符号),就将绝对数降低一点.就像它到达yVector = -500一样,当您反转符号时,使其变为+480而不是+500.您可能应该对xVector做同样的事情,以防止它从一侧跳到另一侧.

edit1: To account for friction, whenever it reverses (and you reverse the sign), lower the absolute number a little. Like if it hits at yVector=-500, when you reverse the sign, make it +480 instead of +500. You should probably do the same thing to xVector to stop it from bouncing side-to-side.

edit2: 另外,如果您希望它对空气摩擦"起反应,则每次调整时都应将这两个向量减小很小的量.

edit2: Also, if you want it to react to "air friction", reduce both vectors by a very small amount every adjustment.

edit3: 关于永远在底部滚动的问题-根据您的数字有多高,这可能是两件事之一.要么您的数字很大,似乎需要永远地完成它,要么您是四舍五入,并且您的向量始终为5左右. (5的90%是4.5,因此可能会向上舍入为5).

edit3: About the thing rolling around on the bottom forever--Depending on how high your numbers are, it could be one of two things. Either your numbers are large and it just seems to take forever to finish, or you are rounding and your Vectors are always 5 or something. (90% of 5 is 4.5, so it may round up to 5).

我将打印出一条调试语句,然后查看Vector编号是什么样的.如果它们到达5左右的某个位置并停留在该位置,则可以使用将分数截断为4而不是四舍五入到5的函数.如果它继续下降并最终停止,则可能必须提高摩擦系数

I'd print out a debug statement and see what the Vector numbers are like. If they go to somewhere around 5 and just stay there, then you can use a function that truncates your fraction to 4 instead of rounding back to 5. If it keeps on going down and eventually stops, then you might have to raise your friction coefficient.

如果找不到简单的四舍五入"函数,则可以使用(0.9 *向量)-1,从现有方程式中减去1应该做同样的事情.

If you can't find an easy "rounding" function, you could use (0.9 * Vector) - 1, subtracting 1 from your existing equation should do the same thing.

这篇关于如何在重力弹跳球应用程序中施加重力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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