Colliding Balls + JavaFX
By vaibhavc on 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 convenient to run demos if there is a JNLP app. I recommend using it for your demos.
Posted by Dmitry on December 27, 2008 at 02:06 PM IST #
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 !
Posted by Vaibhav on December 27, 2008 at 02:36 PM IST #
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.
Posted by Uncle SAM on December 29, 2008 at 09:04 AM IST #
Oh thanks SAM, we need to check it. Thanks for pointing it out :)
Posted by Vaibhav on December 29, 2008 at 09:34 AM IST #
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..
Posted by Farrukh Obaid on February 05, 2009 at 07:24 PM IST #
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.
Posted by Vaibhav Choudhary on February 06, 2009 at 06:04 AM IST #
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="">WeatherWidget</a> and <a href="">LocalSearch</a> samples. If you interested in Swing components you can look at the <a href="">ExercisingSwing</a> sample. The <a href="">EffectsPlayground</a> sample is very complex and covers many RIA's <a href="">key characteristics</a>.
Posted by Sergey Malenkov on February 06, 2009 at 07:07 AM IST #
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..
Posted by Farrukh Obaid on February 06, 2009 at 08:26 PM IST #
After all JavaFX is the competitor of Flash and Silverlight there should be some cool website build in JavaFX..
Posted by Farrukh Obaid on February 06, 2009 at 08:30 PM IST #
Thanks for your nice and explanation article here, Vaibhav!
Posted by Ankara Bilgisayar Bilgisayar Satis on February 26, 2009 at 08:30 AM IST #
thank you !
Posted by Vaibhav on February 26, 2009 at 09:07 AM IST #
thanks..
Posted by kelebek on April 26, 2009 at 07:35 AM IST #
thanks admin
Posted by konya chat on November 06, 2009 at 02:16 PM IST #
thank you very very good.thank you very very good.
Posted by program indir on November 22, 2009 at 12:49 PM IST #
thanks you very very much.thanks you very very much.
Posted by porno izle on November 23, 2009 at 10:53 AM IST #
thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.thnxxxxx.
Posted by adult video on November 23, 2009 at 11:52 AM IST #
oh very very good.oh very very good.
Posted by porno izle on November 23, 2009 at 06:03 PM IST #
thanks you .thanks you .thanks you .thanks you .
Posted by online sinema izle on November 24, 2009 at 08:18 AM IST #
hımm.. oh very very hell.hımm.. oh very very hell.
Posted by mp3 indir on November 25, 2009 at 08:31 AM IST #
rt5vr
Posted by guest on December 02, 2009 at 09:07 AM IST #
Posted by watchgy on December 20, 2009 at 12:23 PM IST #
After all JavaFX is the competitor of Flash and Silverlight there should be some cool website build in JavaFX..
Posted by araç sorgulama on January 08, 2010 at 08:47 AM IST #
chat
Posted by chat on February 11, 2010 at 11:40 PM IST #
mırc
Posted by mırc on February 11, 2010 at 11:41 PM IST #
thank
Posted by chat sitesi on March 04, 2010 at 09:04 PM IST #