This demo shows how to create physical bodies, apply forces to them, set transformations like scale, color
It uses collision detection via the PhysicsJS Engine and Howler.js for sound






object world object renderer number width number height object sun number count=0 object sky object galaxy1 object galaxy2 object gamma1 object gamma2 object gaz object comet object novaAnim list novas // this method is called on scene start // if you have multiples files per scene, only one of this method is allowed method void OnInit () { width=1600 app.setLogicalWidth(width) height=app.getLogicalHeight() world=app.createWorld() renderer=app.createRenderer() world.addRenderer(renderer) world.addBehavior("newtonian",0.38) number sc galaxy1=renderer.createSprite("sky","orbital-demo/galaxie1_620x470.png",rnd(width),rnd(height),1920,1200) sc=.4+rnd(.5) galaxy1.setScale(sc/5) galaxy1.sc=sc galaxy2=renderer.createSprite("sky","orbital-demo/galaxie2_620x470.png",rnd(width),rnd(height),1920,1200) sc=.4+rnd(.5) galaxy2.setScale(sc/5) galaxy2.sc=sc gaz=renderer.createSprite("sky","orbital-demo/nuage de gaz_440x390.png",rnd(width),rnd(height),1920,1200) sc=.4+rnd(.5) gaz.setScale(sc/5) gaz.sc=sc gamma1=renderer.createSprite("sky","orbital-demo/sursaut gamma_260x300.png",rnd(width),rnd(height),1920,1200) sc=.4+rnd(.5) gamma1.setScale(sc/5) gamma1.sc=sc gamma2=renderer.createSprite("sky","orbital-demo/sursaut gamma2_260x300.png",rnd(width),rnd(height),1920,1200) sc=.4+rnd(.5) gamma2.setScale(sc/5) gamma2.sc=sc sky=renderer.createTilingSprite("sky","orbital-demo/sky_1600x1000.png",width/2,height/2,1920,1200) sun = world.createBody("circle", "ball", "dynamic", "orbital-demo/sun_225x225.png", width/2, height/2,-0.012, 0, 90, 90) sun.setMass(25) sun.setAngularVelocity(-0.0002) object earth = world.createBody("circle", "ball", "dynamic", "orbital-demo/earth_188x188.png", width/2, height*0.825, 0.15, 0, 45, 45) earth.setMass(1) earth.setAngularVelocity(0.0005) object cometAnim=renderer.createAnimation(["orbital-anims/comete1_444x200.png","orbital-anims/comete2_432x198.png","orbital-anims/comete3_441x200.png"]) comet=renderer.createAnimatedSprite("comet",cometAnim,-100,rnd(height),,,10) sc=1.5+rnd(.6) comet.setScale(sc/5) comet.sc=sc comet.setAnimationLoop(true) comet.playAnimation() novaAnim=renderer.createAnimation(["orbital-anims/supernova1_244x202.png","orbital-anims/supernova2_424x274.png","orbital-anims/supernova3_498x304.png","orbital-anims/supernova4_528x332.png","orbital-anims/supernova5_470x390.png"]) } // this method is called each frame // if you have multiples files per scene, only one of this method is allowed method void OnUpdate () { count=count+1 if count>3000 log("angular velocity:"+sun.getAngularVelocity()) log("angle:"+sun.getAngle()) log("position x:"+sun.getPositionX()) log("position y:"+sun.getPositionY()) log("velocity x:"+sun.getVelocityX()) log("velocity y:"+sun.getVelocityY()) log("bouciness:"+sun.getBounciness()) log("slideness:"+sun.getSlideness()) log("mass:"+sun.getMass()) log("visibility:"+sun.isVisible()) count=0 endif galaxy1.scroll(-galaxy1.sc/10,0) if galaxy1.getPositionX()<-80 galaxy1.setPosition(width+80,galaxy1.getPositionY()) endif galaxy2.scroll(-galaxy2.sc/10,0) if galaxy2.getPositionX()<-80 galaxy2.setPosition(width+80,galaxy2.getPositionY()) endif gaz.scroll(-gaz.sc/10,0) if gaz.getPositionX()<-90 gaz.setPosition(width+90,gaz.getPositionY()) endif gamma1.scroll(-gamma1.sc/10,0) if gamma1.getPositionX()<-80 gamma1.setPosition(width+80,gamma1.getPositionY()) endif gamma2.scroll(-gamma2.sc/10,0) if gamma2.getPositionX()<-80 gamma2.setPosition(width+80,gamma2.getPositionY()) endif comet.setPosition(comet.getPositionX()+comet.sc/5,comet.getPositionY()) if comet.getPositionX()>width+100 comet.setPosition(-100,comet.getPositionY()) endif sky.scroll(-0.15,0) number x=sun.getPositionX() number y=sun.getPositionY() if xwidth/3*2 sun.applyForce(-0.0005,0) endif if yheight/3*2 sun.applyForce(0,-0.0005) endif if rndInt(300)==50 object nova=renderer.createAnimatedSprite("nova",novaAnim,100+rnd(width-200),80+rnd(height-160)) nova.setAnimationLoop(false) nova.setAnimationSpeed(.2) nova.playAnimation() novas.push(nova) endif foreach object nov in novas if not nov.isAnimationPlaying() log("removing nova") novas.pop(nov) renderer.remove(nov) endif next } // this method is called each time one or more collisions are detected on bodies that you registered collision-detection behavior // if you have multiples files per scene, only one of this method is allowed method void OnCollision(list collisions) { }