You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
9.2 KiB

2 years ago
let TOP=1,RIGHT=2,BOTTOM=3,LEFT=4,TOP_RIGHT=5,BOTTOM_RIGHT=6,BOTTOM_LEFT=7,TOP_LEFT=8;function collisionDetection(a,e){let j=strokePadding;if(null==j&&(j=0),a.isEllipse&&e.isRectangle){let k=a,l=e;for(let n=0;n<TWO_PI;n+=PI/32){let u=k.x,w=k.y,z=k.x+sin(n)*k.r,A=k.y-cos(n)*k.r,B=l.width+2*j,C=l.height+2*j,D=l.x-j,E=l.y-j;if(z>D&&z<D+B&&A>E&&A<E+C){if(u>D&&u<D+B){if(0<k.v.y)return{isTouching:!0,location:TOP};if(0>k.v.y)return{isTouching:!0,location:BOTTOM}}if(w>E&&w<E+C){if(0<k.v.x)return{isTouching:!0,location:LEFT};if(0>k.v.x)return{isTouching:!0,location:RIGHT}}if(0<n&&n<=PI/2)return{isTouching:!0,location:BOTTOM_LEFT};if(n>PI/2&&n<=PI)return{isTouching:!0,location:TOP_LEFT};if(n>PI&&n<=PI+PI/2)return{isTouching:!0,location:TOP_RIGHT};if(n>PI+PI/2&&n<=TWO_PI)return{isTouching:!0,location:BOTTOM_RIGHT}}}}if(a.isEllipse&&e.isEllipse){let k=a,l=e;if(k.x+k.r>l.x-l.r&&k.x-k.r<l.x+l.r&&k.y+k.r>l.y-l.r&&k.y-k.r<l.y+l.r){let n=sqrt(pow(k.x-l.x,2)+pow(k.y-l.y,2));if(n<k.r+l.r)return{isTouching:!0}}}return{isTouching:!1,location:0}}function performCollision(a,e,j){if(a.isEllipse){let k=a;if(e.isFrameborder){switch(j.location){case BOTTOM:k.v.y*=-1;break;case LEFT:case RIGHT:k.v.x*=-1;}testMode&&j.location==TOP&&(k.v.y*=-1),k.move()}if(e.isBrick){let l,n;switch(j.location){case TOP:case BOTTOM:return k.v.y*=-1,void k.move();case LEFT:case RIGHT:return k.v.x*=-1,void k.move();case TOP_LEFT:l=e.x,n=e.y;break;case TOP_RIGHT:l=e.x+e.width,n=e.y;break;case BOTTOM_LEFT:l=e.x,n=e.y+e.height;break;case BOTTOM_RIGHT:l=e.x+e.width,n=e.y+e.height;}let u=k.x-l,w=k.y-n,z=sqrt(u*u+w*w);u/=z,w/=z;let A=k.v.x*u+k.v.y*w;k.v.x-=2*A*u,k.v.y-=2*A*w,k.move()}if(e.isPaddle){let l,n;switch(j.location){case TOP:case TOP_LEFT:case TOP_RIGHT:return k.v.x=k.calcVelocityX(e,k.v.x),k.v.y=-k.calcVelocityY(),void k.move();case LEFT:case RIGHT:return k.v.x*=-1,void k.move();case BOTTOM_LEFT:l=e.x,n=e.y+e.height;break;case BOTTOM_RIGHT:l=e.x+e.width,n=e.y+e.height;}let u=k.x-l,w=k.y-n,z=sqrt(u*u+w*w);u/=z,w/=z;let A=k.v.x*u+k.v.y*w;k.v.x-=2*A*u,k.v.y-=2*A*w,k.move()}if(e.isEllipse){let l=a,n=e;l.mass=l.r,n.mass=n.r;let u=atan2(n.y-l.y,n.x-l.x),w=atan2(l.v.y,l.v.x),z=atan2(n.v.y,n.v.x),A=l.v.mag*cos(w-u),B=l.v.mag*sin(w-u),C=n.v.mag*cos(z-u),D=n.v.mag*sin(z-u),E=((l.mass-n.mass)*A+2*n.mass*C)/(l.mass+n.mass),F=B,G=(2*l.mass*A+(n.mass-l.mass)*C)/(l.mass+n.mass),H=D,I=cos(u),J=sin(u);l.v.x=I*E-J*F,l.v.y=J*E+I*F,n.v.x=I*G-J*H,n.v.y=J*G+I*H,l.v.mag=sqrt(pow(l.v.x,2)+pow(l.v.y,2)),n.v.mag=sqrt(pow(n.v.x,2)+pow(n.v.y,2)),l.x+=l.v.x,l.y+=l.v.y,n.x+=n.v.x,n.y+=n.v.y}}}function toTimeString(a,e){a=floor(a/10);let j=floor(a%100)+"",k=floor(a/100%60)+"";if(e){let l=floor(a/100/60%60)+"",n=floor(a/100/60/60)+"";2>j.length&&(j="0"+j),2>k.length&&(k="0"+k),2>l.length&&(l="0"+l),2>n.length&&(n="0"+n)}else{let l=floor(a/100/60%60)+"";2>j.length&&(j="0"+j),2>k.length&&(k="0"+k),2>l.length&&(l="0"+l)}return timeString}function setCookie(a,e,j){let k="";if(j){let l=new Date;l.setTime(l.getTime()+1e3*(60*(60*(24*(365*j))))),k="; expires="+l.toUTCString()}document.cookie=a+"="+e+k+"; path=/"}function getCookie(a){let e=a+"=",j=document.cookie.split(";");for(let l,k=0;k<j.length;k++){for(l=j[k];" "==l.charAt(0);)l=l.substring(1,l.length);if(0==l.indexOf(e))return l.substring(e.length,l.length)}return null}function deleteCookies(){for(let a=0;a<arguments.length;a++)setCookie(arguments[a],"",-1)}function deleteAllCookies(){let a=document.cookie.split(";");for(let e=0;e<a.length;e++)deleteCookies(a[e].split("=")[0])}Array.prototype.shuffle=function(){for(let e,j,a=this.length;0!=a;)j=floor(random()*a),a-=1,e=this[a],this[a]=this[j],this[j]=e},Array.prototype.clone=function(){return this.slice(0)},Array.prototype.partitiate=function(a){if(0==a)return this;let e=[];for(;this.length;)e.push(this.splice(0,round(pow(this.length,1/(1+1/a)))).partitiate(a-1));return e},String.prototype.isValidHEX=function(){return /(^#[0-9A-Fa-f]{6}$)|(^#[0-9A-Fa-f]{3}$)/i.test(this)};function debugInformation(){let a=viewPort.x,e=viewPort.y;textSize(12),textStyle(NORMAL),stroke(255),strokeWeight(1),fill(255),