Simple Polygon in Box2d

  ... discussions about development with the GLES1 branch of AndEngine.

Re: Simple Polygon in Box2d

Postby droidy » Thu Aug 19, 2010 4:32 pm

That would be super ! I can wait
droidy
 
Posts: 5
Joined: Thu Aug 19, 2010 9:59 am

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 5:15 pm

Ok, I have written what I think should be an OK Polygon class, but I cannot get it to display anything...

Does anyone here have any experience with the AndEngine Shape Classes? If so, I could do with a hand sorting out my Polygon class.
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby blandonfrank » Thu Aug 19, 2010 5:58 pm

Adam,
Are you extending the GLShape class?
If so, can you post some code to see where it might be going wrong?
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 6:03 pm

Sure, I will post and attach both class I have created.

First up, the Polygon Class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.andengine.entity.primitive;
  2.  
  3. import javax.microedition.khronos.opengles.GL10;
  4. import javax.microedition.khronos.opengles.GL11;
  5.  
  6. import org.anddev.andengine.engine.camera.Camera;
  7. import org.anddev.andengine.entity.shape.GLShape;
  8. import org.anddev.andengine.entity.shape.IShape;
  9. import org.anddev.andengine.opengl.buffer.BufferObjectManager;
  10. import org.anddev.andengine.opengl.util.GLHelper;
  11. import org.anddev.andengine.opengl.vertex.PolygonVertexBuffer;
  12. import org.anddev.andengine.opengl.vertex.VertexBuffer;
  13.  
  14. public class Polygon extends GLShape
  15. {
  16.        
  17.         // ===========================================================
  18.         // Fields
  19.         // ===========================================================
  20.        
  21.         private float[] mVertices;
  22.        
  23.         private final PolygonVertexBuffer mPolygonVertexBuffer;
  24.        
  25.         // ===========================================================
  26.         // Constructors
  27.         // ===========================================================
  28.  
  29.         public Polygon(float pX, float pY, float[] pVertices)
  30.         {
  31.                 this(pX, pY, pVertices, new PolygonVertexBuffer(pVertices.length, GL11.GL_STATIC_DRAW));
  32.         }
  33.        
  34.         public Polygon(float pX, float pY, float[] pVertices, final PolygonVertexBuffer pPolygonVertexBuffer)
  35.         {
  36.                 super(pX, pY);
  37.                
  38.                 this.mVertices = pVertices;
  39.                
  40.                 this.mPolygonVertexBuffer = pPolygonVertexBuffer;
  41.                 BufferObjectManager.getActiveInstance().loadBufferObject(this.mPolygonVertexBuffer);
  42.                 this.updateVertexBuffer();
  43.         }
  44.        
  45.         // ===========================================================
  46.         // Getter & Setter
  47.         // ===========================================================
  48.        
  49.         // ===========================================================
  50.         // Methods for/from SuperClass/Interfaces
  51.         // ===========================================================
  52.        
  53.         @Override
  54.         protected void onInitDraw(GL10 pGL)
  55.         {
  56.                 super.onInitDraw(pGL);
  57.                 GLHelper.disableTextures(pGL);
  58.                 GLHelper.disableTexCoordArray(pGL);            
  59.         }
  60.        
  61.         @Override
  62.         protected VertexBuffer getVertexBuffer()
  63.         {
  64.                 return this.mPolygonVertexBuffer;
  65.         }
  66.  
  67.         @Override
  68.         protected void onUpdateVertexBuffer()
  69.         {
  70.                 this.mPolygonVertexBuffer.update(mVertices);
  71.         }
  72.  
  73.         @Override
  74.         protected void drawVertices(GL10 pGL, Camera pCamera)
  75.         {
  76.                 pGL.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVertices.length / 3);
  77.         }
  78.  
  79.         @Override
  80.         protected boolean isCulled(Camera pCamera)
  81.         {
  82.                 // TODO Auto-generated method stub
  83.                 return false;
  84.         }
  85.  
  86.         @Override
  87.         public boolean collidesWith(IShape pOtherShape)
  88.         {
  89.                 // TODO Auto-generated method stub
  90.                 return false;
  91.         }
  92.  
  93.         @Override
  94.         public float getBaseHeight()
  95.         {
  96.                 // TODO Auto-generated method stub
  97.                 return 0;
  98.         }
  99.  
  100.         @Override
  101.         public float getBaseWidth()
  102.         {
  103.                 // TODO Auto-generated method stub
  104.                 return 0;
  105.         }
  106.  
  107.         @Override
  108.         public float getHeight()
  109.         {
  110.                 // TODO Auto-generated method stub
  111.                 return 0;
  112.         }
  113.  
  114.         @Override
  115.         public float[] getSceneCenterCoordinates()
  116.         {
  117.                 // TODO Auto-generated method stub
  118.                 return null;
  119.         }
  120.  
  121.         @Override
  122.         public float getWidth()
  123.         {
  124.                 // TODO Auto-generated method stub
  125.                 return 0;
  126.         }
  127.  
  128.         @Override
  129.         @Deprecated
  130.         public boolean contains(float pX, float pY)
  131.         {
  132.                 return false;
  133.         }
  134.  
  135.         @Override
  136.         @Deprecated
  137.         public float[] convertLocalToSceneCoordinates(float pX, float pY)
  138.         {
  139.                 return null;
  140.         }
  141.  
  142.         @Override
  143.         @Deprecated
  144.         public float[] convertSceneToLocalCoordinates(float pX, float pY)
  145.         {
  146.                 return null;
  147.         }
  148.  
  149. }
  150.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Next the PolygonVertexBuffer Class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.andengine.opengl.vertex;
  2.  
  3. import java.nio.FloatBuffer;
  4.  
  5. public class PolygonVertexBuffer extends VertexBuffer
  6. {
  7.         public PolygonVertexBuffer(int pVerticesCount, int pDrawType)
  8.         {
  9.                 super(2 * pVerticesCount * BYTES_PER_FLOAT, pDrawType);
  10.         }
  11.        
  12.         public void update(float[] pVertices)
  13.         {
  14.                 final FloatBuffer buffer = this.getFloatBuffer();
  15.                 buffer.position(0);
  16.                
  17.                 buffer.put(pVertices);
  18.                
  19.                 buffer.position(0);
  20.                
  21.                 super.update();
  22.         }
  23. }
  24.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
Attachments
Polygon.zip
(1.52 KiB) Downloaded 298 times
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 6:06 pm

Bare in mind that I have not done this sort of thing before, not in 2D anyway, and certainly not using another persons engine
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby blandonfrank » Thu Aug 19, 2010 7:18 pm

Adam,

The code looks good to me. Show me when you are creating the polygons.
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 7:29 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //TEST
  2.                 float[] vertices = {    -0.25f, -0.25f, 0.0f,
  3.                                                 0.25f, -0.25f, 0.0f,
  4.                                                 -0.25f, 0.25f, 0.0f,
  5.                                                 0.25f, 0.25f, 0.0f };
  6.  
  7.                
  8.                 Polygon poly = new Polygon(20, 20, vertices);
  9.                 poly.setColor(1, 0.5f, 0.2f);
  10.                 scene.getTopLayer().addEntity(poly);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby blandonfrank » Thu Aug 19, 2010 8:33 pm

Adam,

I think the mistake is in creating the polygon.
You're creating a 0.25 or .50 pixel polygon..(pretty tiny I think :D)
I created a retarded one just for kicks:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. float[] vertices = {    180f, -180f, 0.0f,
  2.                                                 180f, 180f, 0.0f,
  3.                                                 -180f, -180f, 0.0f,};
  4.                                                                                        
  5.  
  6.                
  7.                 Polygon poly = new Polygon(180, 60, vertices);
  8.                 poly.setColor(1, 0f, 0f);
  9.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Try that and it should print something.
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 8:40 pm

*Phew* It works! I thought my code was correct!

Yay, so some tidying up, and I should be able to send this over to Nicolas to be added into the AndEngine. Should be a welcome addition I feel, I know it will come in handy for a lot of peopl when using it with Box2D as well.
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby blandonfrank » Thu Aug 19, 2010 8:45 pm

It's definitely a nice addition.. specially for debugging box2D bodies.

Thanks for taking the time and creating it :D
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

PreviousNext

Return to GLES1

Who is online

Users browsing this forum: No registered users and 8 guests