Hollow circles with nape?



  • I've used Box2d in the past and I've worked with curved surfaces in a way that required creating essentially a high-n ngon to allow round objects to roll within it. Is there a way to create a hollow circle within nape using masks or something, or is the solution to go the ngon route?



  • I figured out a way to make it happen that works pretty nicely. Hopefully this little code chunk helps someone else out. It composes two polygons with a number of segments, flips the second array to make it easy to connect the two vertex lists together, creates polygons from the simplifications, then uses nape's GeomPoly to simplify the solids into simpler shapes. Then we add it to the NapeSprite.

    public function createRingBody(radius:Float = 16):Void
    {
      var segments = 200;
    
      if (body != null) 
      {
        destroyPhysObjects();
      }
      
      centerOffsets(false);
      setBody(new Body(BodyType.STATIC, Vec2.weak(x, y)));
    
      // Create a poly object
      var p1 = Polygon.regular(radius, radius+30, segments);
      var p2 = Polygon.regular(radius-10, radius-10+30, segments);
      p2.reverse();
    
      var gPoly = new GeomPoly();
    
      for (p in p1)
      {
          gPoly.push(p);
      }
    
      gPoly.push(p1[0]);
      gPoly.push(p2[p2.length-1]);
    
      for (p in p2)
      {
          gPoly.push(p);
      }
      
    
      gPoly.triangularDecomposition().foreach(function (gpoly)
      {
          var verts = new Array<Vec2>();
          var it = gpoly.iterator();
          while (it.hasNext())
          {
              verts.push(it.next());
          }
    
          body.shapes.add(new Polygon(verts));
      });
      
      setBodyMaterial();
    }
    

Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.