Filled updatable Arch with AndEngine

  ... the case you feel the need for a new feature or want to submit one.

Filled updatable Arch with AndEngine

Postby micegames » Mon May 12, 2014 10:06 pm

Hi folks,
I'm new on AndEngine forum and AndEngine in general; I'm making my firs creation with this incredible framework and, after too much help got from here I decided to start giving!!!

The first thing i want to share is my custom filled ellipse Mesh that is able to draw filled arch with updatable features.

Attached you can find the class ready to use. The usage is really simple:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 ellipse = new Ellipse(getWidth() / 2, getHeight() / 2, getWidth() / 2, pVertexBufferObjectManager, getCurrentTimePerc());
  2.                                 ellipse.setColor(Color.RED);
  3.                                 ellipse.setAlpha(0.3f);
  4.                                 attachChild(ellipse);
  5.  
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


where getCurrentTimePerc() is a function returning a complete perc from 0 to 1 float.

To update the mesh arch completion use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ellipse.updateVertices(getCurrentTimePerc());
  2.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


Since I cannot add java attachments belo is the widget code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import org.andengine.entity.primitive.DrawMode;
  2. import org.andengine.entity.primitive.Line;
  3. import org.andengine.entity.primitive.Mesh;
  4. import org.andengine.entity.primitive.Rectangle;
  5. import org.andengine.entity.primitive.vbo.HighPerformanceMeshVertexBufferObject;
  6. import org.andengine.extension.debugdraw.primitives.PolyLine;
  7. import org.andengine.opengl.vbo.DrawType;
  8. import org.andengine.opengl.vbo.VertexBufferObjectManager;
  9. import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute;
  10.  
  11. /**
  12.  *
  13.  * @author Rodrigo Castro
  14.  * @since 16:47:01 - 31.01.2012
  15.  */
  16. public class Ellipse extends PolyLine {
  17.         // ===========================================================
  18.         // Constants
  19.         // ===========================================================
  20.        
  21.         static final int LOW_RESOLUTION = 15;
  22.         static final int MEDIUM_RESOLUTION = 30;
  23.         static final int HIGH_RESOLUTION = 50;
  24.         static final int DEFAULT_RESOLUTION = HIGH_RESOLUTION;
  25.        
  26.         // ===========================================================
  27.         // Fields
  28.         // ===========================================================
  29.  
  30.         protected final int mResolution;
  31.         private float mRadiusA;
  32.         private float mRadiusB;
  33.        
  34.         // ===========================================================
  35.         // Constructors
  36.         // ===========================================================
  37.        
  38.         /**
  39.          * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  40.          */
  41.         public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final VertexBufferObjectManager pVertexBufferObjectManager) {
  42.                 this(pX, pY, pRadiusA, pRadiusB, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager);
  43.         }
  44.        
  45.         /**
  46.          * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  47.          */
  48.         public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) {
  49.                 this(pX, pY, pRadiusA, pRadiusB, pLineWidth, DEFAULT_RESOLUTION, pVertexBufferObjectManager);
  50.         }
  51.        
  52.         /**
  53.          * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  54.          */
  55.         public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager) {
  56.                 this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, DrawMode.LINE_LOOP);
  57.         }
  58.        
  59.         public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode) {
  60.                 this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, pDrawMode, DrawType.STATIC);
  61.         }
  62.  
  63.         /**
  64.          * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  65.          */
  66.         public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) {
  67.                 this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, pDrawMode, pDrawType, 0, 360);
  68.         }
  69.  
  70.         /**
  71.          * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  72.          */
  73.         public Ellipse(final float pX, final float pY, final float pRadius, final VertexBufferObjectManager pVertexBufferObjectManager, float percentComplete) {
  74.                 this(pX, pY, pRadius, pRadius, 1, HIGH_RESOLUTION, pVertexBufferObjectManager, DrawMode.TRIANGLE_FAN, DrawType.STATIC, (180 - (180 * percentComplete)), (180 + (180 * percentComplete)));
  75.         }
  76.  
  77.         /**
  78.          * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  79.          */
  80.         public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType, final float fromDegree, final float toDegree) {
  81.                 super(pX, pY, buildEllipseVertices(pRadiusA, pRadiusB, pResolution, fromDegree, toDegree, null), pLineWidth, pVertexBufferObjectManager, pDrawMode, pDrawType);
  82.                
  83.                 mResolution = pResolution;
  84.                 mRadiusA = pRadiusA;
  85.                 mRadiusB = pRadiusB;
  86.         }
  87.        
  88.         // ===========================================================
  89.         // Getter & Setter
  90.         // ===========================================================
  91.  
  92.         /**
  93.          *
  94.          * @param pRadiusA
  95.          * @param pRadiusB
  96.          * @param toDegree
  97.          * @param fromDegree
  98.          * @return      true if vertices were correctly updated
  99.          *                      false otherwise
  100.          */
  101.         /*
  102.         public boolean setRadius( float pRadiusA, float pRadiusB )
  103.         {
  104.                 return this.updateVertices(buildEllipseVertices(pRadiusA, pRadiusB, mResolution));
  105.         }
  106.         */
  107.        
  108.         // ===========================================================
  109.         // Methods for/from SuperClass/Interfaces
  110.         // ===========================================================
  111.  
  112.         // ===========================================================
  113.         // Methods
  114.         // ===========================================================
  115.  
  116.         private static float[] buildEllipseVertices(float pRadiusA, float pRadiusB, int pResolution, float fromDegree, float toDegree, float[] bufferData) {
  117.                 double start = 2.0f * Math.PI * ((fromDegree + 90f) / 360.0f);
  118.                 double end = 2.0f * Math.PI * ((toDegree + 90f) / 360.0f);
  119.                 double gap = end - start;
  120.  
  121.                 int pRes = 0;
  122.  
  123.                 float[] vertices = null;
  124.                 if (bufferData == null) {
  125.                         pRes = pResolution;
  126.                         vertices = new float[VERTEX_SIZE * (pRes + 2)];
  127.                 } else {
  128.                         pRes = (bufferData.length / VERTEX_SIZE) - 2;
  129.                         vertices = bufferData;
  130.                 }
  131.                 vertices[Mesh.VERTEX_INDEX_X] = 0;
  132.                 vertices[Mesh.VERTEX_INDEX_Y] = 0;
  133.  
  134.                 for( int i = 0; i <= pRes; i++)
  135.                 {
  136.                         double theta = (gap * (double)i / (double) pRes) + start;
  137.                         float x = (float) ( (double)pRadiusA * Math.cos( theta ));
  138.                         float y = (float) ( (double)pRadiusB * Math.sin( theta ));
  139.                        
  140.                         vertices[((i + 1) * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_X] = x;
  141.                         vertices[((i + 1) * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_Y] = y;
  142.                 }
  143.                 return vertices;
  144.         }
  145.  
  146.         public void updateVertices(float percentComplete) {
  147.                 buildEllipseVertices(mRadiusA, mRadiusB, mResolution, (180 - (180 * percentComplete)), (180 + (180 * percentComplete)), getBufferData());
  148.                 onUpdateVertices();
  149.         }
  150.        
  151.         // ===========================================================
  152.         // Inner and Anonymous Classes
  153.         // ===========================================================
  154. }
  155.  
  156.  
Parsed in 0.020 seconds, using GeSHi 1.0.8.4


Hoping to help someone.
Best regards.
Micegames.it
==========================
Mobile games. Try Trace Word Game for iOS and Android.
http://www.micegames.it
User avatar
micegames
 
Posts: 1
Joined: Mon May 12, 2014 9:48 pm
Location: Naples (Italy)

Filled updatable Arch, Ellipse, Circle with AndEngine

Postby arslaman » Wed Jun 25, 2014 9:39 am

Thank you very much! You made my day!
I have added method to draw arc by passing degrees:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void updateVertices(float degreeLeft, float degreeRight) {
  2.         buildEllipseVertices(mRadiusA, mRadiusB, mResolution, 90 - degreeLeft, 90 - degreeRight, getBufferData());
  3.         onUpdateVertices();
  4.     }
Parsed in 0.022 seconds, using GeSHi 1.0.8.4
arslaman
 
Posts: 1
Joined: Wed Jun 25, 2014 9:33 am


Return to Features

Who is online

Users browsing this forum: No registered users and 9 guests