lots of bugs / warnings c2runtime

Discussion and feedback on Construct 2

Post » Mon Apr 18, 2016 2:09 pm

@saiyadjin Personally, I'm really interested in your results. So if you can do some performance tests after your fixes, please post it here. I'll check this topic very often. I think it's great that you do these experiments/fixes in your free time. It might be important what you discovered, it might not, but I think it's worth finding out.
B
135
S
33
G
17
Posts: 1,557
Reputation: 20,717

Post » Mon Apr 18, 2016 2:41 pm

That bug referenced broke the code when it was minified. C2's runtime does not break when it's minified, so there is no issue.
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Mon Apr 18, 2016 5:43 pm

@glerikud - i will keep this post updated with everything i do, so stay tuned.
@R0J0hound - true. but some users report that some stuff doesn't work when minimized from c2. therefore those might get fixed.
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
42
S
14
G
12
Posts: 624
Reputation: 9,421

Post » Mon Apr 18, 2016 6:23 pm

What stuff breaks? I've only seen third party plugins cause minifying to break things. If it's an official plugin that breaks, wouldn't fixing that directly be better?
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Mon Apr 18, 2016 9:31 pm

i've noticed another "bug", while c2runtime.js is being created, i'll have to display that in images. but first things first - here's all JS's cleaned in exporters/html folder.
tested on my big project, works ok.
here's the DL:
https://dl.dropboxusercontent.com/u/136 ... rFiles.rar

still though i've tried exporting empty project and i noticed there is still some problems and i've found out which ones:
if you open c2runtime.js and find this line (1485):

Code: Select all
RenderCell_.prototype.isEmpty = function ()
   {
      if (!this.objects.length)
      {
;
;
         return true;
      }
      if (this.objects.length > this.pending_removal.count())
         return false;
;
      this.flush_pending();      // takes fast path and just resets state
      return true;
   };


as you can notice there are some ";" appearing randomly inside that code which is very very wierd. it happens because the following code which is found in common_prelude.js has some "assert2" statements that are on export translated to ";" instead to empty space:

Code: Select all
RenderCell_.prototype.isEmpty = function ()
   {
      // 'Empty' state is a little non-trivial since there is the set of objects pending_removal
      // to take in to consideration. First of all if objects is empty then we know the cell is empty.
      if (!this.objects.length)
      {
         assert2(this.pending_removal.isEmpty(), "expected empty pending removal list");
         assert2(!this.any_pending_removal, "expected no pending removal state");
         return true;
      }
      
      // 'objects' is not empty. However if there are fewer instances in the removal queue, then
      // even if we called flush_pending we know there would still be instances left.
      // So we can safely indicate that the cell is not empty.
      if (this.objects.length > this.pending_removal.count())
         return false;
      
      // Otherwise every item in objects must be in the pending removal set.
      // The set will be empty if we update it. Use this opportunity to clear the state
      // and indicate empty.
      assert2(this.objects.length === this.pending_removal.count(), "expected pending queue to be same size as object list");
      this.flush_pending();      // takes fast path and just resets state
      return true;
   };


as you all know, to minimize size, comments are removed, and assert2 statements are removed, which leave a semicolon in their place for no obvious reason. since this happens in runtime while exporting i can't fix that one, this one is on Ashley :)
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
42
S
14
G
12
Posts: 624
Reputation: 9,421

Post » Mon Apr 18, 2016 9:34 pm

ok, since i've cleaned most of the stuff i'll start in a few days with optimizations and microopts, hopefully these are taken care of if nothing at least i've done it for my pleasure, regardless if ashley finds them usefull / useless.
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
42
S
14
G
12
Posts: 624
Reputation: 9,421

Post » Fri Apr 22, 2016 9:59 pm

here's a first test i've done.. function "next power of two" - found in common_prelude.js
results here:
http://i.imgur.com/HCpm6ne.png

old code:
Code: Select all
cr.nextHighestPowerOfTwo = function (x) {
       --x;
       for (var i = 1; i < 32; i <<= 1) {
           x = x | x >> i;
       }
       return x + 1;
   };


new code:
Code: Select all
   cr.nextHighestPowerOfTwo = function (x) {

      x--;
      x |= x >> 1;
      x |= x >> 2;
      x |= x >> 4;
      x |= x >> 8;
      x |= x >> 16;
      return x+1;
   };
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
42
S
14
G
12
Posts: 624
Reputation: 9,421

Post » Sat Apr 23, 2016 9:50 am

next up, improvement in collision detection:
http://i.imgur.com/OguZ26n.png
since i've got a project that achieves around 60 000 collision checks per second, these values seem pretty good for improvement. it changes the section intersect code. i've noticed that it can be further improved by moving the code to gpu, but that's another pair of glasses. in preview i noticed around 3-4% less cpu usage when high collision checks hits in. found in common_prelude.js
old code:
Code: Select all
// Segment intersection
   cr.segments_intersect = function(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y)
   {
      var max_ax, min_ax, max_ay, min_ay, max_bx, min_bx, max_by, min_by;
      
      // Long-hand code since this is a performance hotspot and this type of
      // code minimises the number of conditional tests necessary.
      if (a1x < a2x)
      {
         min_ax = a1x;
         max_ax = a2x;
      }
      else
      {
         min_ax = a2x;
         max_ax = a1x;
      }
      
      if (b1x < b2x)
      {
         min_bx = b1x;
         max_bx = b2x;
      }
      else
      {
         min_bx = b2x;
         max_bx = b1x;
      }
      
      if (max_ax < min_bx || min_ax > max_bx)
         return false;
      
      if (a1y < a2y)
      {
         min_ay = a1y;
         max_ay = a2y;
      }
      else
      {
         min_ay = a2y;
         max_ay = a1y;
      }
      
      if (b1y < b2y)
      {
         min_by = b1y;
         max_by = b2y;
      }
      else
      {
         min_by = b2y;
         max_by = b1y;
      }
      
      if (max_ay < min_by || min_ay > max_by)
         return false;
         
      var dpx = b1x - a1x + b2x - a2x;
      var dpy = b1y - a1y + b2y - a2y;
      var qax = a2x - a1x;
      var qay = a2y - a1y;
      var qbx = b2x - b1x;
      var qby = b2y - b1y;

      var d = cr.abs(qay * qbx - qby * qax);
      var la = qbx * dpy - qby * dpx;
      
      if (cr.abs(la) > d)
         return false;
      
      var lb = qax * dpy - qay * dpx;
      
      return cr.abs(lb) <= d;
   };


new code:
Code: Select all
// Segment intersection   
   cr.segments_intersect = function (a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y) {
      
   var s02_x, s02_y, s10_x, s10_y, s32_x, s32_y, s_numer, t_numer, denom, t;
   s10_x = a2x - a1x;
   s10_y = a2y - a1y;
   s32_x = b2x - b1x;
   s32_y = b2y - b1y;
    denom = s10_x * s32_y - s32_x * s10_y;
   
    if (denom == 0)
    {
      return false;
   }
      
    var denomPositive = denom > 0;
    s02_x = a1x - b1x;
    s02_y = a1y - b1y;
    s_numer = s10_x * s02_y - s10_y * s02_x;
   
    if ((s_numer < 0) == denomPositive)
   {
        return false;    
   }
   
    t_numer = s32_x * s02_y - s32_y * s02_x;
   
    if ((t_numer < 0) == denomPositive)
   {
        return false;
   }
   
    if (((s_numer > denom) == denomPositive) || ((t_numer > denom) == denomPositive))
   {
        return false;
    }
   
    return true;
   };
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
42
S
14
G
12
Posts: 624
Reputation: 9,421

Post » Wed May 18, 2016 9:26 pm

here comes a big one, which i didn't use in ingame engine (i did but reverted change) but @Ashley might describe where the problem is and why:

so there's this function that tests if a point is in quad (Quad.prototype.contains_pt = function (x, y)), so here's it's code:
Code: Select all
Quad.prototype.contains_pt = function (x, y)
   {
      var tlx = this.tlx;
      var tly = this.tly;
      var v0x = trx - tlx;
      
      var v0y = try_ - tly;
      var v1x = brx - tlx;
      var v1y = bry - tly;
      var v2x = x - tlx;
      var v2y = y - tly;

      var dot00 = v0x * v0x + v0y * v0y;
      var dot01 = v0x * v1x + v0y * v1y;
      var dot02 = v0x * v2x + v0y * v2y;
      var dot11 = v1x * v1x + v1y * v1y;
      var dot12 = v1x * v2x + v1y * v2y;

      var invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
      var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
      var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
            
      if ((u >= 0.0) && (v > 0.0) && (u + v < 1))
      {
         return true;
      }
                  
      v0x = blx - tlx;
      v0y = bly - tly;

      dot00 = v0x * v0x + v0y * v0y;
      dot01 = v0x * v1x + v0y * v1y;
      dot02 = v0x * v2x + v0y * v2y;

      invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
      u = (dot11 * dot02 - dot01 * dot12) * invDenom;
      v = (dot00 * dot12 - dot01 * dot02) * invDenom;

                  // Point is in second triangle
      return (u >= 0.0) && (v > 0.0) && (u + v < 1);
   };


so i've wanted to upgrade this, and after checking manual and doing some work of my own, i've noticed that every quad is split into 2 triangles and then the point is checked in each, if found in first return true, else check other and return true if there or it's false. it's being done with some mad math.
so i went to see what is the most performant way to do it, and here's what i got:

Code: Select all
      if((((this.try_ - this.tly) * (x - this.tlx) - (this.trx - this.tlx) * (y - this.tly)) | ((this.bry - this.try_) * (x - this.trx) - (this.brx - this.trx) * (y - this.try_)) | ((this.tly - this.bry) * (x - this.brx) - (this.tlx - this.brx) * (y - this.bry))) >= 0)
               {
                  return true;
               }
                if(!compareResult)
               {
                  if((((this.bry - this.tly) * (x-this.tlx) - (this.brx - this.tlx) * (y - this.tly)) | ((this.bly - this.bry) * (x - this.brx) - (this.blx - this.brx) * (y- this.bry)) | ((this.tly - this.bly) * (x - this.blx) - (this.tlx - this.blx) * (y - this.bly))) >= 0)
                  {
                     return true;
                  }
                  else
                  {
                     return false;
                  }
               }

this code does the same. takes the first 3 points (tl, tr, br) and 2nd 3 points (tl,br,bl) and checks if in those triangles the point is in. and really this works way faster and in my testing examples gives the same results. here's a link to "performance difference" which is almost double.
http://imgur.com/GPFfzbe

so where's the problem, i've put this function instead of the original and what happened is that in my game that i was testing it with happend this - the bombs sometimes fly through the objects and sometimes explode on the objects (they should explode every time they hit the object). now there is probably something different which i can't get through what and i was wondering if @Ashley you could take a look into it. you could gain some performance in collisions if this is optimized and people would be gratefull. it's not much but it will help. funny thing is that when i go from right side to left side bombs hit well, but form the left side to right side they "miss". like something is wrongly / badly calculated. do variables change in realtime in quads? that might be possibly a problem.

p.s. - here are articles i've been using to "improve" solution
http://jsfiddle.net/z7x0udf7/3/
http://stackoverflow.com/questions/2049 ... d-triangle (last answer is JS)


i'm off to optimize some more and some other things.. i'll be back :)

p.p.s. pls check 4 posts behind @Ashley about those assert statements that leave junk in code.
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
42
S
14
G
12
Posts: 624
Reputation: 9,421

Post » Wed May 18, 2016 9:31 pm

saiyadjin wrote:http://imgur.com/GPFfzbe

:O wow just wow... 110% more efficient ... cant wait to see this at work :D
B
75
S
22
G
68
Posts: 1,337
Reputation: 43,271

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: mammothe and 27 guests