Simple Polygon in Box2d

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

Re: Simple Polygon in Box2d

Postby blandonfrank » Thu Aug 19, 2010 10:08 pm

Adam,

Even though the polygons are been generated...their vertices are not drawn correctly.

I'm thinking it has to do with this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.         protected void drawVertices(GL10 pGL, Camera pCamera)
  3.         {
  4.                 pGL.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVertices.length / 3);
  5.         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

Re: Simple Polygon in Box2d

Postby Radzell » Thu Aug 19, 2010 11:36 pm

Is there anyway you can make pass vector2 arrays?
Radzell
 
Posts: 465
Joined: Sun Jul 04, 2010 8:52 am

Re: Simple Polygon in Box2d

Postby blandonfrank » Thu Aug 19, 2010 11:48 pm

Well, I think it's possible to do it. But, then you'll have to add the Vector2 class to the engine as it's not there.
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

Re: Simple Polygon in Box2d

Postby Radzell » Thu Aug 19, 2010 11:50 pm

Mayby you can use a PointF and then find a way to turn a PointF to Vector2
Radzell
 
Posts: 465
Joined: Sun Jul 04, 2010 8:52 am

Re: Simple Polygon in Box2d

Postby blandonfrank » Fri Aug 20, 2010 12:52 am

Possibly. But, why do you want to use Vector2?
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

Re: Simple Polygon in Box2d

Postby Radzell » Fri Aug 20, 2010 12:55 am

because create from polygon when make a b2body use vector2 so you could pass the some vertice to both and it would give you a exact same shape in the physics and andengine world.
Radzell
 
Posts: 465
Joined: Sun Jul 04, 2010 8:52 am

Re: Simple Polygon in Box2d

Postby Nicolas Gramlich » Fri Aug 20, 2010 11:28 pm

Hi guys,

I currently only see the need for such an untextured "Polygon"-class. "Unfortunately" quite a couple of thing need to know the width and height of such a shape which isn't trivial anymore for Polygons. In most of the cases it will be so much simpler to achieve a complex looking polygon, by using a textured sprite.

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Simple Polygon in Box2d

Postby Mimminito » Sat Aug 21, 2010 1:12 am

Yes I know what you mean Nicolas, I was doing this for two reasons:
1. My own personal knowledge and gain. So I could edit AndEngine and have something workable at the end.
2. Personal preference. Its nice to be able to do something without textures for once. Then creating the textures when its finalised is an easy option.
---------------------------------------
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 ichi » Fri Oct 22, 2010 4:04 pm

Anyone who has made a newer version of this? I can draw polygons using this but I don't understand the positioning. I've looked a bit at "Line.java" for reference and it seems that the draw positions (polygonvertexbuffer values for each vertex) are wrong.

I'm trying to fix this myself but I'm getting nowhere atm, does anyone have an already fixed updated version of this Polygon?

Edit: Sorry, think I was wrong. I think the previous examples on how to define the vertices in this post was wrong but I'm gonna try some more to see if I'm right.

Ya, this is just wrong:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. #
  2. float[] vertices = {
  3.  180f, -180f, 0.0f,
  4.  180f, 180f, 0.0f,
  5. -180f, -180f, 0.0f,};
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

This is proper I think, makes the same "result" as above:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. #
  2. float[] vertices = {
  3.  180f, -180f,
  4.  0.0f, 180f,
  5.  180f, 0.0f};
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
ichi
 
Posts: 62
Joined: Mon Sep 06, 2010 1:47 pm

Re: Simple Polygon in Box2d

Postby chozabu » Sat Nov 27, 2010 6:21 pm

Ichi - The column of 0.0f is because opengl is 3d - it is the z Co-ord.

The code from a page or two ago did not seem to compile with the latest andengine from git, so here it is with my (tiny) updates

Nice one for writing this Mimminito!

the test poly blandonfrank provided displays fine, once I have my level data loading correctly I'll see if I can add texturing support :)

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.  
  151.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.andengine.opengl.vertex;
  2.  
  3. //import java.nio.FloatBuffer;
  4.  
  5. import org.anddev.andengine.opengl.util.FastFloatBuffer;
  6.  
  7. public class PolygonVertexBuffer extends VertexBuffer
  8. {
  9.  
  10.         final static int BYTES_PER_FLOAT = Float.SIZE/8;
  11.         public PolygonVertexBuffer(int pVerticesCount, int pDrawType)
  12.         {
  13.                 super(2 * pVerticesCount * BYTES_PER_FLOAT, pDrawType);
  14.         }
  15.        
  16.         public void update(float[] pVertices)
  17.         {
  18.                 final FastFloatBuffer buffer = this.getFloatBuffer();
  19.                 buffer.position(0);
  20.                
  21.                 buffer.put(pVertices);
  22.                
  23.                 buffer.position(0);
  24.                
  25.                 //super.update();
  26.         }
  27. }
  28.  
  29.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
chozabu
 
Posts: 106
Joined: Sat Oct 30, 2010 10:57 pm

PreviousNext

Return to GLES1

Who is online

Users browsing this forum: Google [Bot], illunara, ttttkk and 16 guests