如何使弹跳球移动更快?动态速度? [英] How do I make bouncing ball move quicker? dynamic velocity?

查看:385
本文介绍了如何使弹跳球移动更快?动态速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个程序移动现在各地使用JavaFX屏幕中的反弹球,现在我已经试过Duration.millis()在我的时间轴动画,并在重新格式化某些价值观我把它放在较低的速度越快,球进,但有人告诉我,是不是要代替我应该问的动态速度添加到我的计划这里是我的code为我的球运动的最佳方式:

So I had a program to move a bouncing ball across the screen using JavaFX now, Now I've tried reformatting certain values under Duration.millis() in my Timeline Animation and the lower I put it the faster the ball goes but, someone has told me that is not the best way to go instead I should ask about dynamic velocity to add to my program here's my code for movement of my ball:

    public class BallPane extends Pane {

public final double radius = 5;
public double x = radius, y = radius;
public double dx = 1, dy = 1;
public Circle circle = new Circle(x, y, radius);
public Timeline animation;

public BallPane(){    
circle.setFill(Color.BLACK); // Set ball color
getChildren().add(circle); // Place ball into Pane

// Create animation for moving the Ball
animation = new Timeline(
    new KeyFrame(Duration.millis(10), e -> moveBall() ));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
} 

public void moveBall() {
// Check Boundaries
if (x < radius || x > getWidth() - radius) {
    dx *= -1; //change Ball direction
}
if (y < radius || y > getHeight() - radius) {
    dy *= -1; //change Ball direction
}
x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
} }

在反过来,这将是一个乒乓球比赛,所以我将有5个级别和每一个级别,我想球移动得更快,我可以通过降低Duration.millis()做到这一点,但我告诉这不是代替最大的方式来增加速度请问有什么可以去这样做,而不在我的时间线动画参数降低我的Duration.millis?难道还有其他参数我要补充或速度的另一种方法?

In turn, it's going to be a pong game so I'm going to have 5 levels and in each level, I want the ball to move faster I can do this by lowering the Duration.millis() but I am told that is not the greatest way instead to add velocity how may I go about doing this without lowering my Duration.millis in my Time line animation parameters? Is there another parameter I should add or another method of velocity?

推荐答案

我想建议另一种方法:使用的 AnimationTimer ,的矢量计算部队

I'd like to suggest another approach: Use an AnimationTimer, Vector calculation and Forces.

滚珠/精灵有属性:

PVector location;
PVector velocity;
PVector acceleration;

的移动由施加力加速度,加速速度和速度到位置完成:

The movement is done by applying forces to acceleration, acceleration to velocity and velocity to location:

public void applyForce(PVector force) {

    // Making a copy of the PVector before using it!
    PVector f = PVector.div(force, mass);
    acceleration.add(f);
}

public void move() {

    // set velocity depending on acceleration
    velocity.add(acceleration);

    // limit velocity to max speed
    velocity.limit(maxSpeed);

    // change location depending on velocity
    location.add(velocity);

    // clear acceleration
    acceleration.mult(0);
}

这是在游戏循环每精灵完成的:

And this is done in a game loop for every sprite:

gameLoop = new AnimationTimer() {

    @Override
    public void handle(long now) {

        // physics: apply forces
        allSprites.forEach(s -> s.applyForce(Settings.FORCE_GRAVITY));
        allSprites.forEach(s -> s.applyForce(Settings.FORCE_WIND));

        // move
        allSprites.forEach(Sprite::move);

        // check boundaries
        allSprites.forEach(Sprite::checkBounds);

        // update in fx scene
        allSprites.forEach(Sprite::display);

    }
};

您可以找到关于此要点一个完整的例子。球反弹在地板上,这取决于重力。从风吹左到右,所以球搬到那里。但是你可以很容易地改变,在设置的属性。

You can find a complete example on this gist. The balls bounce on the floor, depending on the gravity. Wind blows from left to right, so the balls move there. But you can easily change that in the settings attributes.

别担心,这不是太大code,就在通用矢量计算类是漫长的。但是,你只需要知道它的一些方法。

该示例使用力量风和重力。无论你想要实现的,只是采用了它的力​​量。当然对于你的问题,你可以简单地提高速度,而不施加力。这一切都取决于你想要摆弄的东西。

The example uses the forces wind and gravity. Whatever you want to achieve, just apply the force of it. Of course for your question you could simply increase the velocity without applying a force. It all depends on what you want to toy around with.

截图:

在这里输入的形象描述

你如何可以改变,因为摩擦球的弹跳例子是的章2.7 。这里是丹尼尔Shiffman用在他的书处理code,但你看到它很容易转化为JavaFX的:

Example about how you could change the bouncing of the balls because of friction is in chapter 2.7. Here's the processing code that Daniel Shiffman uses in his book, but you see it's very easy to translate to JavaFX:

for (int i = 0; i < movers.length; i++) {

    float c = 0.01;
    PVector friction = movers[i].velocity.get();
    friction.mult(-1);
    friction.normalize();
    friction.mult(c);

    movers[i].applyForce(friction);
    movers[i].applyForce(wind);
    movers[i].applyForce(gravity);

    movers[i].update();
    movers[i].display();
    movers[i].checkEdges();
  }

我离开了JavaFX的实施给你。

I leave the JavaFX implementation to you.

您还可能有兴趣在一个有关如何章节2.10 (一切都吸引视频) 好像。如果你想实现这样的事情这真的不多code。这 code也可。它使用Point2D类,如果你更舒服。但是,你不应该使用的Point2D因为有它的局限性,你必须创建新的Point2D对象可以通过自定义矢量类的实现来避免所有的时间。

You may also be interested in a video about how chapter 2.10 (everything attracts everything) looks like. It's really not much to code if you want to achieve something like this. That code is also available. It uses the Point2D class, if you are more comfortable with that. However, you shouldn't use Point2D since there are limitations with it and you have to create new Point2D objects all the time which can be avoided by a custom Vector class implementation.

这篇关于如何使弹跳球移动更快?动态速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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