User Tools

Site Tools


script_llxperiment_boing_no_overlap

====== About the script ====== * Purpose : This script will bounce all items randomly around,they will bounce off the screen borders and each other. * Author : F43nd1r * Concept : This script is based on the [[script_llxperiment_boing|boing script]] by TrianguloY * Current Version : 1.1 * Link : https://plus.google.com/105066926163073195690/posts/BMYiNUEss8B (scroll down in the comments) * Video : https://plus.google.com/115366157037831519359/posts/SXcGo6A2gDo ====== Changelog ====== * Version 1.0 (3/5/2014): initial release in wiki * Version 1.1 (18/5/2014): reduced jumps ====== How to use the script ====== * detach all items in a container * set up configuration * run the script in the container ====== Issues and hints ====== * unexpected effects may occur if the items overlap when you start the script * sometimes an item jumps. This happens because the item has no space where it should move to. * If you have set velocity to 1 or bigger, you can cancel the script by opening any app * You'll need a high end phone to run this smoothly with a lot of items. It should work on average phones for up to 5 items. //please report all bugs in the g+ community// ====== Script code ====== <code> var bounciness = 1;//Reduction every tick, between 0 (only one tick) and 1 (always bouncing) [Recommended 0.95] var inVel = 0.02 //initial velocity. set it to 0 for random one [recommended 0.5] var frecuency = 1000;//ticks per second [Recommended 60] var event = LL.getEvent(); var cont = event.getContainer(); var items; items=cont.getItems(); for(var i=items.getLength()-1; i>=0;--i){ var item=items.getAt(i); if(item.getType()=="StopPoint")continue; var ang = Math.random()*2*Math.PI; var invel=inVel; if(invel<=0)invel=Math.random(); invel*=(cont.getHeight()+cont.getWidth())/2; //saving item specific data in the tag var data=new Object(); data.vel = [ Math.cos(ang)*invel , Math.sin(ang)*invel ]; data.size = [ item.getWidth()*item.getScaleX() , item.getHeight()*item.getScaleY() ]; item.setTag(JSON.stringify(data)); } tick(); function tick(){ var changed=false;//value if something changed for(var a=items.getLength()-1; a>=0;--a){ var i=items.getAt(a); if(i.getType()=="StopPoint")continue; var data=JSON.parse(i.getTag()); //continue statement if((Math.abs(data.vel[0])<0.2 && Math.abs( data.vel[1])<0.2)||LL.isPaused()) continue; var changed=true; //Reduction data.vel=[ data.vel[0]*bounciness , data.vel[1]*bounciness ]; var newpos = [ i.getPositionX()+ data.vel[0] , i.getPositionY()+ data.vel[1] ]; //checking all other items after the current for overlap for(var b=a-1; b>=0;--b){ var i2=items.getAt(b); if(i2.getType()=="StopPoint")continue; var data2=JSON.parse(i2.getTag()); var newpos2 = [ i2.getPositionX()+ data2.vel[0] , i2.getPositionY()+ data2.vel[1] ]; var p;//new position of the item with the smaller width/height var p2;//higher width/height var s;//size of the item with the smaller width/height var s2; //higher width/height //horizonatal: if(data.size[0]<data2.size[0]) { //the first item is smaller p=newpos[0] p2=newpos2[0]; s=data.size[0]; s2= data2.size[0]; } else { //the second item is smaller p=newpos2[0] p2=newpos[0]; s=data2.size[0]; s2= data.size[0]; } var overx= ((p2<p&&p<p2+s2)||(p2<p+s&&p+s<p2+s2));//check if horizontal coordinates match //the same again, but for vertical if(data.size[1]<data2.size[1]) { p=newpos[1]; p2=newpos2[1]; s=data.size[1]; s2= data2.size[1]; } else { p=newpos2[1]; p2=newpos[1]; s=data2.size[1]; s2= data.size[1]; } var overy= ((p2<p&&p<p2+s2)||(p2<p+s&&p+s<p2+s2)); if(overx && overy){ //the distances on the sides of the first items, how far it overlaps distl=newpos2[0]+data2.size[0]-newpos[0]; distr=newpos[0]+data.size[0]-newpos2[0]; distt=newpos2[1]+data2.size[1]-newpos[1]; distb=newpos[1]+data.size[1]-newpos2[1]; if(distr<distb && distr<distt && distr<distl)//overlapping right { newpos[0]-=2*distr; data.vel[0]=-data.vel[0]; data2.vel[0]=-data.vel[0]; } else if(distb<distt && distb<distl)//overlapping bottom { newpos[1]-=2*distb; data.vel[1]=-data.vel[1]; data2.vel[1]=-data.vel[1]; } else if(distt<distl)//overlapping top { newpos[1]+=2*distt; data.vel[1]=-data.vel[1]; data2.vel[1]=-data.vel[1]; } else //overlapping left { newpos[0]+=2*distl; data.vel[0]=-data.vel[0]; data2.vel[0]=-data.vel[0]; } } i2.setTag(JSON.stringify(data2)); } var t; //right t=(newpos[0]+ data.size[0])-( cont.getPositionX()+cont.getWidth()/cont.getPositionScale() ) ; if(t>0){newpos[0]-=2*t; data.vel[0]=-data.vel[0];} //bottom t=(newpos[1]+ data.size[1])-( cont.getPositionY()+cont.getHeight()/cont.getPositionScale() ) ; if(t>0){newpos[1]-=2*t; data.vel[1]=-data.vel[1];} //left t= newpos[0]- cont.getPositionX(); if(t<0){newpos[0]-=2*t; data.vel[0]=-data.vel[0];} //top t= newpos[1]- cont.getPositionY(); if(t<0){newpos[1]-=2*t; data.vel[1]=-data.vel[1];} //set i.setPosition(newpos[0] , newpos[1] ); i.setTag(JSON.stringify(data)); } if(!changed)return; //repeat setTimeout(tick,1000/frecuency); } </code>

script_llxperiment_boing_no_overlap.txt · Last modified: 2016/06/28 12:31 by f43nd1r