X

Vaibhav's Blog Space

  • Java
    December 26, 2008

Colliding Balls + JavaFX

Guest Author

Here is an example of one more physics system - Colliding Balls. This is one of the most common examples we find everywhere. I have written this on transparent window. And so, if we increase the width and height of the stage + width, height variable in MotionBall.fx, it will give us a feeling like FlyingSaucer which keep on running all the way on Desktop. Here it looks on top of my code :D.

So, the netbeans window on back and application is running on top of it. Actually dragged out of the browser and running. Just for adding information, we can run an application from netbeans on browser by Properties -> Run -> Run in Browser. Remeber to set the size of applet correct else the application display will not full.

Initially position in this application has been generated randomly + Damping motion has been applied. So, in sometime all the balls will settle down on base. These all parameters can be adjusted in the file. Ball Effect has been generated as usual by Gradient Effect. Some variable may be unused because of future plan :D.

Here goes the code:

MotionBall.fx :

package collidingballs;
import java.lang.\*;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.\*;
import javafx.scene.shape.\*;
var spring : Number = 0.05;
var gravity : Number = 0.05;
var width : Number = 240;
var height: Number = 320;
var start: Number = 0;
public class MotionBall extends CustomNode {
    public var x: Number;
    public var y: Number;
    public var velocityX: Number;
    public var velocityY: Number;
    public var mass: Number;
    public var radius: Number;
    public var color: Color;
    public function collision( balls: MotionBall[]) {
        for(ball in balls) {
            var dx : Number = ball.x - x;
            var dy : Number = ball.y - y;
            var distance : Number = Math.sqrt( dx \* dx + dy \* dy );
            var minDist : Number = ball.radius + radius;
            if( distance < minDist ) {
                var angle : Number = Math.atan2( dy, dx );
                var tx : Number = x + Math.cos( angle ) \* minDist;
                var ty : Number = y + Math.sin( angle ) \* minDist;
                var ax : Number = ( tx - ball.x ) \* spring;
                var ay : Number = ( ty - ball.y ) \* spring;
                velocityX -= ax;
                velocityY -= ay;
                ball.velocityX += ax;
                ball.velocityY += ay;
            }
        }
    }
    public function move(balls: MotionBall[]) {
        velocityY += gravity;
        x += velocityX;
        y += velocityY;
        if( x + radius >= width ) {
            x = width + start - radius;
            velocityX \*= - 0.9;
        }
        if( x - radius <= start ) {
            x = start + radius;
            velocityX \*= -0.9;
        }
        if( y + radius >= height ) {
            y = height - radius;
            velocityY \*= -0.9;
        }
        if( y - radius <= 40 ) {
            y = 40 + radius;
            velocityY \*= -0.9;
        }
    }
    public override function create(): Node {
        return Group {
            content: [
                Circle {
                    centerX: bind x,
                    centerY: bind y,
                    radius: bind radius
                    fill: bind RadialGradient {
                        centerX: 0.25
                        centerY: 0.25
                        proportional: true
                        stops: [
                            Stop {
                                offset: 0.0
                                color: color },
                            Stop {
                                offset: 1.0
                                color: Color.BLACK },
                        ]
                    }
                }
            ]
        };
    }



Main.fx :

package collidingballs;
import collidingballs.MotionBall;
import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

var balls: MotionBall[];
var rnd : Random = new Random();
for( i in [1..5] ) {
    insert MotionBall {
        x: rnd.nextInt( 240 ),
        y: rnd.nextInt( 320 ),
        radius: 30
        color: Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255))
    } into balls;
}
// timeline for colliding balls
var t_collide = Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame {
            time: 30ms
            action: function() {
                for(ball in balls) {
                    ball.collision(balls);
                    ball.move(balls);
                }
            }
        }
    ]
}
t_collide.play();
Stage {
    title: "Collding Balls"
    style: StageStyle.TRANSPARENT
    width: 240
    height: 320
    scene: Scene {
        fill: Color.TRANSPARENT
        content: [
            balls
        ]
    }
}

Join the discussion

Comments ( 25 )
  • Dmitry Saturday, December 27, 2008

    Hi,

    Just a general comment: it is much more convenient to run demos if there is a JNLP app. I recommend using it for your demos.


  • Vaibhav Saturday, December 27, 2008

    Hi Dmitry, I tried to run Applet on Sun blog but its not working. Don't know why but it is so. JNLP link is a good option, I will take care of it from next time.

    thanks !


  • Uncle SAM Monday, December 29, 2008

    Hi Vaibhav,

    The <code>collision</code> function does not check that the current ball is in the <code>balls</code> sequence.

    The <code>move</code> function does not use the <code>balls</code> parameter.


  • Vaibhav Monday, December 29, 2008

    Oh thanks SAM, we need to check it. Thanks for pointing it out :)


  • Farrukh Obaid Thursday, February 5, 2009

    Hi can you build some demo RIA application in JavaFX it will be great i have not seen such demo yet. i am not happy with the deployment model of Applets i have installed jre u 11 but still it takes so much time to load..


  • Vaibhav Choudhary Friday, February 6, 2009

    Why not, please let me know which type of RIA you are looking for. Deployment model is under work and it will be very nice soon.


  • Sergey Malenkov Friday, February 6, 2009

    Hi Farrukh Obaid,

    If you don't like applets you could deploy applications via Java WebStart. What do you want from the RIA samples? If you interested how to get data from internet you can look at the <a href="http://javafx.com/samples/WeatherWidget/">WeatherWidget</a> and <a href="http://javafx.com/samples/LocalSearch/">LocalSearch</a> samples. If you interested in Swing components you can look at the <a href="http://javafx.com/samples/ExercisingSwing/">ExercisingSwing</a> sample. The <a href="http://javafx.com/samples/EffectsPlayground/">EffectsPlayground</a> sample is very complex and covers many RIA's <a href="http://en.wikipedia.org/wiki/Rich_Internet_application">key characteristics</a>.


  • Farrukh Obaid Friday, February 6, 2009

    Thanks Sergey Malenkov for the links i have seen all the demos, from RIA application i mean a demo type website or application that contains custom components and animations..


  • Farrukh Obaid Friday, February 6, 2009

    After all JavaFX is the competitor of Flash and Silverlight there should be some cool website build in JavaFX..


  • Ankara Bilgisayar Bilgisayar Satis Thursday, February 26, 2009

    Thanks for your nice and explanation article here, Vaibhav!


  • Vaibhav Thursday, February 26, 2009

    thank you !


  • kelebek Sunday, April 26, 2009

    thanks..


  • konya chat Friday, November 6, 2009

    thanks admin


  • program indir Sunday, November 22, 2009

    thank you very very good.thank you very very good.


  • porno izle Monday, November 23, 2009

    thanks you very very much.thanks you very very much.


  • adult video Monday, November 23, 2009

    thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.


  • porno izle Monday, November 23, 2009

    oh very very good.oh very very good.


  • online sinema izle Tuesday, November 24, 2009

    thanks you .thanks you .thanks you .thanks you .


  • mp3 indir Wednesday, November 25, 2009

    hımm.. oh very very hell.hımm.. oh very very hell.


  • guest Wednesday, December 2, 2009

    rt5vr


  • watchgy Sunday, December 20, 2009
  • ara&ccedil; sorgulama Friday, January 8, 2010

    After all JavaFX is the competitor of Flash and Silverlight there should be some cool website build in JavaFX..


  • chat Thursday, February 11, 2010

    chat


  • m&#305;rc Thursday, February 11, 2010

    mırc


  • chat sitesi Thursday, March 4, 2010

    thank


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.