Colliding Balls + JavaFX
Friday Dec 26, 2008
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 ] } }















Hi,
Just a general comment: it is much more conven...
Hi Dmitry, I tried to run Applet on Sun blog but i...
Hi Vaibhav,
The <code>collision</code&g...
Oh thanks SAM, we need to check it. Thanks for poi...
Hi can you build some demo RIA application in Java...
Why not, please let me know which type of RIA...
Hi Farrukh Obaid,
If you don't like applets you co...