Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 9.52 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Submerged Area from AS3 to LUA
  2. local intoIndex2 = (intoIndex ) % m_vertexCount;
  3. local outoIndex2 = (outoIndex ) % m_vertexCount;
  4.        
  5. function ComputeSubmergedArea(params)
  6.  
  7.             local normal = params.normal;
  8.             local offset = params.offset;
  9.             local the_body=params.body;
  10.             --vertices arrray
  11.             -- get it from the body or pass it in
  12.             local m_vertices=params.vertices;
  13.             -- basically we're using just rectangles so we've got 4 vertexes
  14.             -- use count from above
  15.             local m_vertexCount= 4 --params.vcount;
  16.             local xf={};
  17.             xf.position={the_body.x, the_body.y};
  18.             local RR={};
  19.             RR.col1={1,0};
  20.             RR.col2={0,1};
  21.             xf.R=RR;
  22.  
  23.                 function SetAngle(par)
  24.  
  25.                     local c = math.cos(par.angle);
  26.                     local s= math.sin(par.angle);
  27.                     local R=par.R;
  28.                     R.col1[1] = c; R.col2[1] = -s;
  29.                     R.col1[2] = s; R.col2[2] = c;
  30.  
  31.                     return R;
  32.                 end
  33.  
  34.                 function Dot(para)
  35.  
  36.                     local a=para.first;
  37.                     local b=para.second;
  38.                     local d=a[1] * b[1] + a[2] * b[2];
  39.  
  40.                     return d;
  41.                 end
  42.  
  43.                 function MulTMV(para)
  44.  
  45.                     local u={Dot({first=para.v, second=para.A.col1}), Dot({first=para.v, second=para.A.col2})}
  46.  
  47.                     return u;
  48.                 end
  49.  
  50.             xf.R=SetAngle({R=xf.R,angle=the_body.rotation});
  51.  
  52.  
  53.             -- Transform plane into shape co-ordinates
  54.  
  55.  
  56.             local normalL= MulTMV({A=xf.R, v=normal});
  57.             local offsetL=  offset +  Dot({first=normal, second=xf.position});
  58.             --print('offset', offset)
  59.             --print ("offsetL ",offsetL)
  60.             -- for i,v in ipairs(normalL) do
  61.                     -- print (i,v)
  62.  
  63.  
  64.  
  65.  
  66.             -- end
  67.             local depths={};
  68.             local diveCount  = 0;
  69.             local intoIndex  = -1;
  70.             local outoIndex = -1;
  71.  
  72.             local lastSubmerged = false;
  73.             local isSubmerged;
  74.             local i;
  75.  
  76.  
  77.  
  78.             for  i = 1, m_vertexCount, 1  do
  79.                      --- DOT && MIN_VALUE
  80.                     depths[i] = Dot({first=normalL, second=m_vertices[i]}) - offsetL;
  81.                 --- print(i,depths[i])
  82.                      isSubmerged = depths[i] > 0.000001;
  83.                     if (i > 1) then
  84.  
  85.                         if (isSubmerged) then
  86.  
  87.                             if not(lastSubmerged) then
  88.  
  89.                                 intoIndex = i - 1;
  90.                                 diveCount=diveCount+1;
  91.                             end
  92.  
  93.                         else
  94.  
  95.                             if (lastSubmerged) then
  96.                                 outoIndex = i - 1;
  97.                                 diveCount=diveCount+1;
  98.                             end
  99.                         end
  100.  
  101.  
  102.                     end
  103.                     lastSubmerged = isSubmerged;
  104.  
  105.  
  106.             end
  107.  
  108.  
  109.              if (diveCount==0) then
  110.  
  111.                 if (lastSubmerged ) then
  112.                         print('Completly submerged')
  113.                         -------------------------------------------------------totaly submerged
  114.                         return 100;
  115.  
  116.                     else
  117.                         --Completely dry
  118.                          print('Completly dry')
  119.                         return 0;
  120.                     end
  121.  
  122.  
  123.             elseif (diveCount==1) then
  124.                if (intoIndex == -1) then -- one vertex in water
  125.  
  126.                         intoIndex = m_vertexCount - 1;
  127.  
  128.                     else -- one vertex out of water
  129.  
  130.                         outoIndex = m_vertexCount - 1;
  131.                     end
  132.  
  133.             end
  134.  
  135.                      --- Something is wrong here ---
  136.  
  137.             local intoIndex2 = (intoIndex ) % m_vertexCount;
  138.             local outoIndex2 = (outoIndex ) % m_vertexCount;
  139.         --  print(intoIndex, intoIndex2, outoIndex, outoIndex2);
  140.  
  141.             local intoLamdda = (0 - depths[intoIndex]) / (depths[intoIndex2] - depths[intoIndex]);
  142.             local outoLamdda = (0 - depths[outoIndex]) / (depths[outoIndex2] - depths[outoIndex]);
  143.  
  144.             print ("intoLamdda:",intoLamdda)
  145.  
  146.             local intoVec =  {m_vertices[intoIndex][1] * (1 - intoLamdda) + m_vertices[intoIndex2][1] * intoLamdda,
  147.                                                 m_vertices[intoIndex][2] * (1 - intoLamdda) + m_vertices[intoIndex2][2] * intoLamdda};
  148.             local outoVec = {m_vertices[outoIndex][1] * (1 - outoLamdda) + m_vertices[outoIndex2][1] * outoLamdda,
  149.                                                 m_vertices[outoIndex][2] * (1 - outoLamdda) + m_vertices[outoIndex2][2] * outoLamdda};
  150.  
  151.             -- Initialize accumulator
  152.  
  153.              area = 0;
  154.             center = {0,0};
  155.              p2= m_vertices[intoIndex2];
  156.             p3={}
  157.  
  158.             -- An awkward loop from intoIndex2+1 to outIndex2
  159.                 i = intoIndex2;
  160.                 while (i ~= outoIndex2) do
  161.  
  162.                     i = (i ) % m_vertexCount;
  163.                     if(i == outoIndex2) then
  164.                         p3 = outoVec;
  165.                     else
  166.                     --print("nr i ",i)
  167.  
  168.                         p3 = m_vertices[i];
  169.                         return 22;
  170.                     end
  171.                     local triangleArea  = 0.5 * ( (p2[1] - intoVec[1]) * (p3[2] - intoVec[2]) - (p2[2] - intoVec[2]) * (p3[1] - intoVec[1]) );
  172.                     area =  area + triangleArea;
  173.                     -- Area weighted centroid
  174.                     center[1] = center[1] + triangleArea * (intoVec[1] + p2[1] + p3[1]) / 3;
  175.                     center[2] = center[2] + triangleArea * (intoVec[2] + p2[2] + p3[2]) / 3;
  176.  
  177.                     p2 = p3;
  178.  
  179.                 end
  180.  
  181.  
  182.             --Normalize and transform centroid
  183.             function Multiply(p)
  184.                 local v= {}
  185.  
  186.                 v[1]=p.vector[1] * p.factor;
  187.                 v[2]=p.vector[2] * p.factor;
  188.  
  189.                 return v;
  190.  
  191.             end
  192.  
  193.  
  194.             --center=Multiply({factor=1 / area, vector=center});
  195.             --c.SetV(b2Math.MulX(xf, center));
  196.  
  197.                 --print ("Submerged Area: ", area );
  198.  
  199.             return area;
  200.  
  201. end
  202.        
  203. public override function ComputeSubmergedArea(
  204.         normal:b2Vec2,
  205.         offset:Number,
  206.         xf:b2Transform,
  207.         c:b2Vec2):Number
  208. {
  209.     // Transform plane into shape co-ordinates
  210.     var normalL:b2Vec2 = b2Math.MulTMV(xf.R, normal);
  211.     var offsetL:Number = offset - b2Math.Dot(normal, xf.position);
  212.  
  213.     var depths:Vector.<Number> = new Vector.<Number>();
  214.     var diveCount:int = 0;
  215.     var intoIndex:int = -1;
  216.     var outoIndex:int = -1;
  217.  
  218.     var lastSubmerged:Boolean = false;
  219.     var i:int;
  220.     for (i = 0; i < m_vertexCount;++i)
  221.     {
  222.         depths[i] = b2Math.Dot(normalL, m_vertices[i]) - offsetL;
  223.         var isSubmerged:Boolean = depths[i] < -Number.MIN_VALUE;
  224.         if (i > 0)
  225.         {
  226.             if (isSubmerged)
  227.             {
  228.                 if (!lastSubmerged)
  229.                 {
  230.                     intoIndex = i - 1;
  231.                     diveCount++;
  232.                 }
  233.             }
  234.             else
  235.             {
  236.                 if (lastSubmerged)
  237.                 {
  238.                     outoIndex = i - 1;
  239.                     diveCount++;
  240.                 }
  241.             }
  242.         }
  243.         lastSubmerged = isSubmerged;
  244.     }
  245.     switch(diveCount)
  246.     {
  247.         case 0:
  248.         if (lastSubmerged )
  249.         {
  250.             // Completely submerged
  251.             var md:b2MassData = new b2MassData();
  252.             ComputeMass(md, 1);
  253.             c.SetV(b2Math.MulX(xf, md.center));
  254.             return md.mass;
  255.         }
  256.         else
  257.         {
  258.             //Completely dry
  259.             return 0;
  260.         }
  261.         break;
  262.         case 1:
  263.         if (intoIndex == -1)
  264.         {
  265.             intoIndex = m_vertexCount - 1;
  266.         }
  267.         else
  268.         {
  269.             outoIndex = m_vertexCount - 1;
  270.         }
  271.         break;
  272.     }
  273.     var intoIndex2:int = (intoIndex + 1) % m_vertexCount;
  274.     var outoIndex2:int = (outoIndex + 1) % m_vertexCount;
  275.     var intoLamdda:Number = (0 - depths[intoIndex]) / (depths[intoIndex2] - depths[intoIndex]);
  276.     var outoLamdda:Number = (0 - depths[outoIndex]) / (depths[outoIndex2] - depths[outoIndex]);
  277.  
  278.     var intoVec:b2Vec2 = new b2Vec2(m_vertices[intoIndex].x * (1 - intoLamdda) + m_vertices[intoIndex2].x * intoLamdda,
  279.                                     m_vertices[intoIndex].y * (1 - intoLamdda) + m_vertices[intoIndex2].y * intoLamdda);
  280.     var outoVec:b2Vec2 = new b2Vec2(m_vertices[outoIndex].x * (1 - outoLamdda) + m_vertices[outoIndex2].x * outoLamdda,
  281.                                     m_vertices[outoIndex].y * (1 - outoLamdda) + m_vertices[outoIndex2].y * outoLamdda);
  282.  
  283.     // Initialize accumulator
  284.     var area:Number = 0;
  285.     var center:b2Vec2 = new b2Vec2();
  286.     var p2:b2Vec2 = m_vertices[intoIndex2];
  287.     var p3:b2Vec2;
  288.  
  289.     // An awkward loop from intoIndex2+1 to outIndex2
  290.     i = intoIndex2;
  291.     while (i != outoIndex2)
  292.     {
  293.         i = (i + 1) % m_vertexCount;
  294.         if(i == outoIndex2)
  295.             p3 = outoVec
  296.         else
  297.             p3 = m_vertices[i];
  298.  
  299.         var triangleArea:Number = 0.5 * ( (p2.x - intoVec.x) * (p3.y - intoVec.y) - (p2.y - intoVec.y) * (p3.x - intoVec.x) );
  300.         area += triangleArea;
  301.         // Area weighted centroid
  302.         center.x += triangleArea * (intoVec.x + p2.x + p3.x) / 3;
  303.         center.y += triangleArea * (intoVec.y + p2.y + p3.y) / 3;
  304.  
  305.         p2 = p3;
  306.     }
  307.  
  308.     //Normalize and transform centroid
  309.     center.Multiply(1 / area);
  310.     c.SetV(b2Math.MulX(xf, center));
  311.  
  312.     return area;
  313. }