nzisaacnz

Moveable Polygons with collisions (rough)

Sep 18th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 7.93 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script>
  4. var Template = {};
  5. var requestAnimationFrame =
  6. window.requestAnimationFrame||
  7. window.webkitRequestAnimationFrame||
  8. window.mozRequestAnimationFrame||
  9. window.oRequestAnimationFrame||
  10. window.msRequestAnimationFrame;
  11.  
  12. var cancelAnimationFrame =
  13. window.cancelAnimationFrame||
  14. window.webkitCancelRequestAnimationFrame||
  15. window.webkitCancelAnimationFrame||
  16. window.mozCancelRequestAnimationFrame||
  17. window.mozCancelAnimationFrame||
  18. window.oCancelRequestAnimationFrame||
  19. window.oCancelAnimationFrame||
  20. window.msCancelRequestAnimationFrame||
  21. window.msCancelAnimationFrame;
  22.  
  23. Template.newTime = Date.now();
  24. Template.time = 0;
  25. Template.animation = function()
  26. {
  27.  if(!Template.stopped)
  28.  {
  29.   Template.oldTime = Template.newTime;
  30.   if(!Template.paused)
  31.   {
  32.    Template.calculate();
  33.   }
  34.   Template.draw();
  35.   Template.newTime = Date.now();
  36.   Template.time=Template.newTime-Template.oldTime;
  37.   Template.animationFrame = requestAnimationFrame(Template.animation);
  38.  }
  39. }
  40. Template.start = function(d,c)
  41. {
  42.  Template.draw = d;
  43.  Template.calculate = c;
  44.  Template.paused = false;
  45.  Template.animationFrame = requestAnimationFrame(Template.animation);
  46. }
  47. Template.stop = function()
  48. {
  49.  cancelAnimationFrame(Template.animationFrame);
  50. }
  51. function Transition(start,end,period,loops,func)
  52. {
  53.  this.start = start;
  54.  this.end = end;
  55.  this.period = period;
  56.  if(!func)this.func = function(a){return a;};
  57.  else this.func = func;
  58.  this.pos = 0;
  59.  this.reversed;
  60.  this.loop = loops;
  61.  this.res = 0;
  62.  this.calc=function()
  63.  {
  64.   if(this.reversed) this.pos -= Template.time/this.period;
  65.   else this.pos += Template.time/this.period;
  66.   if(this.loop===0)
  67.   {
  68.    this.pos = (this.pos+1)%1;
  69.   }
  70.   else
  71.   {
  72.    this.pos = Math.min(this.loop,Math.max(this.pos,0));
  73.   }
  74.   this.res = this.func(this.pos)*(end-start)+start;
  75.  }
  76.  this.get = function()
  77.  {
  78.   return this.res;
  79.  }
  80.  this.reverse = function()
  81.  {
  82.   this.reversed = !this.reversed;
  83.  }
  84. }
  85. Template.random = function(a,b)
  86. {
  87.  return Math.random()*(b-a)+a;
  88. }
  89. </script>
  90. <script>
  91. function Point(x,y)
  92. {
  93.     this.x = x;
  94.     this.y = y;
  95.     this.draw = function(G)
  96.     {
  97.         G.beginPath();
  98.         G.moveTo(this.x+10,this.y);
  99.         G.arc(this.x,this.y,10,0,2*Math.PI);
  100.         G.stroke();
  101.     }
  102. }
  103. function Line(xs,ys,xe,ye)
  104. {
  105.     this.start = new Point(xs,ys);
  106.     this.end = new Point(xe,ye);
  107.     this.Intersects = function(line,x,y,x1,y1)
  108.     {
  109.         x = +x|0;
  110.         y = +y|0;
  111.         x1 = +x1|0;
  112.         y1 = +y1|0;
  113.         return get_line_intersection(this.start.x+x,this.start.y+y,
  114.                                      this.end.x+x,this.end.y+y,
  115.                                      line.start.x+x1,line.start.y+y1,
  116.                                      line.end.x+x1,line.end.y+y1);
  117.     }
  118. }
  119.  
  120. function Mesh(x,y,strokeStyle,fillStyle)
  121. {
  122.     this.prevPoint = null;
  123.     this.lines = [];
  124.     this.x = x;
  125.     this.y = y;
  126.     this.strokeStyle = strokeStyle;
  127.     this.fillStyle = fillStyle;
  128.     this.maxX = 5;
  129.     this.add = function(point,index)
  130.     {
  131.         if(this.prevPoint)
  132.         {
  133.             index = +index|this.lines.length;
  134.             this.lines.splice(index,0,new Line(this.prevPoint.x,this.prevPoint.y,point.x,point.y));
  135.         }
  136.         this.prevPoint = new Point(point.x,point.y);
  137.     }
  138.     this.collisions = function(thing,breakOnIntersect)
  139.     {
  140.         var cols = [];
  141.         if(thing.constructor == Mesh)
  142.         {
  143.             var mesh = thing;
  144.             for(var a=0,aa=this.lines.length; a<aa; a++)
  145.            {
  146.                for(var b=0,bb=mesh.lines.length; b<bb; b++)
  147.                {
  148.                    var collision = this.lines[a].Intersects(mesh.lines[b],this.x,this.y,mesh.x,mesh.y);
  149.                    if(breakOnIntersect && collision) return true;
  150.                    if(collision)cols[cols.length] = collision;
  151.                }
  152.            }
  153.        }
  154.        else if(thing.constructor == Line)
  155.        {
  156.            var line = thing;
  157.            for(var a=0,aa=this.lines.length; a<aa; a++)
  158.            {
  159.                var collision = this.lines[a].Intersects(line,this.x,this.y);
  160.                if(breakOnIntersect && collision) return true;
  161.                if(collision)cols[cols.length] = collision;
  162.            }
  163.        }
  164.        return breakOnIntersect?false:cols;
  165.    }
  166.    this.IsPointInside = function(point)
  167.    {
  168.        return this.collisions(new Line(point.x,point.y,cvs.width*50,point.y),false).length%2==1;
  169.    }
  170.    this.FinishMesh = function()
  171.    {
  172.        if(this.lines.length==0)return false;
  173.        this.add(new Point(this.lines[0].start.x,this.lines[0].start.y));
  174.        this.finished = true;
  175.        return true;
  176.    }
  177.    this.draw = function(G)
  178.    {
  179.        G.strokeStyle = this.strokeStyle;
  180.        G.fillStyle = this.fillStyle;
  181.        if(this.lines.length==0)return false;
  182.        G.beginPath();
  183.        G.moveTo(this.lines[0].start.x+this.x,this.lines[0].start.y+this.y);
  184.        for(var a=0; a<this.lines.length; a++)
  185.        {
  186.            G.lineTo(this.lines[a].end.x+this.x,this.lines[a].end.y+this.y);
  187.        }
  188.        if(this.finished)G.fill();
  189.        G.stroke();
  190.        return true;
  191.    }
  192. }
  193. function get_line_intersection(p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,p3_x,p3_y)
  194. {
  195.    var s1_x, s1_y, s2_x, s2_y;
  196.    s1_x = p1_x - p0_x;     s1_y = p1_y - p0_y;
  197.    s2_x = p3_x - p2_x;     s2_y = p3_y - p2_y;
  198.  
  199.    var cache2=(-s2_x * s1_y + s1_x * s2_y);
  200.  
  201.    if(cache2===0) return false; // Parallel
  202.  
  203.    var s, t;
  204.    var cache0=(p0_y - p2_y);
  205.    var cache1=(p0_x - p2_x);
  206.  
  207.    s = (-s1_y * cache1 + s1_x * cache0) / cache2;
  208.    t = ( s2_x * cache0 - s2_y * cache1) / cache2;
  209.  
  210.    if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
  211.    {
  212.        // Collision detected
  213.        return new Point(p0_x + (t * s1_x),p0_y + (t * s1_y));
  214.     }
  215.  
  216.     return false; // No collision
  217. }
  218.  
  219. </script>
  220. </head>
  221. <body>
  222. <canvas id="cvs" width="800" height="600" onmousemove="mouseEvent(event)" onmousedown="mouseEvent(event)" onmouseup="mouseEvent(event)" ></canvas>
  223. <script>
  224. var mouse = new Point(null,null);
  225. function mouseEvent(e)
  226. {
  227.  mouse.x = e.offsetX;
  228.  mouse.y = e.offsetY;
  229.  switch(e.type)
  230.  {
  231.   case "mousemove":
  232.    if(mouse.down&&mouse.mesh)break;
  233.    
  234.   break;
  235.   case "mousedown":
  236.    for(var a=0; a<meshes.length; a++)
  237.   {
  238.    if(meshes[a].IsPointInside(mouse)&&!mouse.mesh)
  239.    {
  240.     mouse.mesh = meshes[a];
  241.    }
  242.   }
  243.   if(mouse.mesh)mouse.diff = new Point(mouse.x-mouse.mesh.x,mouse.y-mouse.mesh.y);
  244.   mouse.down = true;
  245.  break;
  246.  case "mouseup":
  247.   mouse.down = false;
  248.   mouse.mesh = null;
  249.  break;
  250. }
  251. }
  252. var totalTime = 0;
  253. var button;
  254. var button2;
  255. var res = "No";
  256. var meshes = [];
  257. var intersections = [];
  258. function init()
  259. {
  260. button = new Mesh(200,300,"#000","#FF5555");
  261. button2 = new Mesh(400,300,"#000","#FF5555");
  262. meshes[0] = button;
  263. meshes[1] = button2;
  264. var M = 20;
  265. for(var a=0; a<M; a++)
  266. {
  267.  button.add(new Point(Template.random(40,70)*Math.sin(a/M*Math.PI*2)+50,Template.random(40,70)*Math.cos(a/M*Math.PI*2)+50));
  268.  button2.add(new Point(Template.random(40,70)*Math.sin(a/M*Math.PI*2)+50,Template.random(40,70)*Math.cos(a/M*Math.PI*2)+50));
  269. }
  270. button.FinishMesh();
  271. button2.FinishMesh();
  272. Template.start(draw,calc);
  273. }
  274. function draw()
  275. {
  276. var G = cvs.getContext("2d");
  277. G.clearRect(0,0,cvs.width,cvs.height);
  278. button2.draw(G);
  279. button.draw(G);
  280. G.strokeStyle="#000000";
  281. if(mouse.mesh)G.strokeText(res,mouse.mesh.x+30,mouse.mesh.y+53);
  282. if(intersections)
  283. {
  284.  for(var a=0; a<intersections.length; a++)
  285.  {
  286.   intersections[a].draw(G);
  287.  }
  288. }
  289. }
  290. function calc()
  291. {
  292. intersections = button.collisions(button2);
  293. if(mouse.down && mouse.mesh)
  294. {
  295.  mouse.mesh.x = mouse.x-mouse.diff.x;
  296.  mouse.mesh.y = mouse.y-mouse.diff.y;
  297. }
  298. if(mouse.mesh)
  299. {
  300.  mouse.mesh.fillStyle = "#55FF55";
  301.  res = "Drag Me!";
  302. }
  303.  for(var a=0; a<meshes.length; a++)if(mouse.mesh!=meshes[a])meshes[a].fillStyle = "#FF5555";
  304. }
  305. init();
  306. </script>
  307. </body>
  308. </html>
Advertisement
Add Comment
Please, Sign In to add comment