Liquid Fun - Box2D became even better - SUCCEED !!!

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

Re: Liquid Fun - Box2D became even better

Postby Therdiday » Wed Apr 16, 2014 12:54 am

Yeah, that other build got corrupted somehow, but I do have a working version now for Anchor Center. I'll get it up on Github soon while I continue to add to the convenience functions.
Therdiday
 
Posts: 343
Joined: Thu Oct 20, 2011 11:12 pm

Re: Liquid Fun - Box2D became even better

Postby valka » Wed Apr 16, 2014 11:33 pm

Wow :)
what a great news !!
GForce good work man !!

Can you please explain in general words how you've managed to port it ?
(updated the C libs, compiled and then added manually/automatically the jni wrapper?)
i'm just very curious, because I tried myself for a whole night and got stuck.

Therdiday, please update us with AC working classes, i'll try to connect the physics particlesystem with andengine's batched particle system for a beginning.

damn.. these are great news :)
User avatar
valka
 
Posts: 97
Joined: Tue Aug 13, 2013 3:17 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby GForce » Thu Apr 17, 2014 12:16 pm

So first I threw all new Box2D natives into the jni folder and fixed them to work with all platforms. (for example the new b2Timer class wasn't supported by all platforms)

Next I wrote the the java classes and the interface to the natives. For example inside the ParticleSystem.java you can see this commented lines after each native method. Libgdx got a tool named jnigen. It handles a lot of the jni stuff for you, so you can write your natives code inside the java code and the c++ files and headers are generated by this tool. It's standalone, so if you want to work with jni more in the future you could take a look.

After this everything should just work, but I also got some problems with the compilers. When starting with jni it's just nasty but after working with it a bit you get used to it. So the only way you can fix this problems is spending a lot of time asking goolge.
GForce
 
Posts: 10
Joined: Mon Mar 19, 2012 2:39 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby valka » Thu Apr 17, 2014 12:38 pm

GForce wrote:So first I threw all new Box2D natives into the jni folder and fixed them to work with all platforms. (for example the new b2Timer class wasn't supported by all platforms)

Next I wrote the the java classes and the interface to the natives. For example inside the ParticleSystem.java you can see this commented lines after each native method. Libgdx got a tool named jnigen. It handles a lot of the jni stuff for you, so you can write your natives code inside the java code and the c++ files and headers are generated by this tool. It's standalone, so if you want to work with jni more in the future you could take a look.

After this everything should just work, but I also got some problems with the compilers. When starting with jni it's just nasty but after working with it a bit you get used to it. So the only way you can fix this problems is spending a lot of time asking goolge.


That what i started to do.. so i was on the right direction :)
good to know about that jnigen, coz i thought about rewriting all manually :? and this might be veryy bad :cry: ..
now the next task is to break that jni bottleneck of the looping and copying the pos array each frame..
User avatar
valka
 
Posts: 97
Joined: Tue Aug 13, 2013 3:17 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby UpGamer » Fri Apr 18, 2014 9:46 pm

Hi,
Wow this is great addition to AndeEngine. Thanks valka for opening this topic and GForce to porting LiquidFun to AndEngine :D. Valka, how did you managed to add color to particles?
User avatar
UpGamer
 
Posts: 3
Joined: Fri Mar 28, 2014 1:08 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby valka » Sat Apr 19, 2014 7:30 pm

this is pretty nasty way.. better to make something more sophisticated to make it more beautiful... (shaders is the answer :) :) )

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             /////////////particles sandbox
  2.            
  3.             final int maxParticleCount = 100000;
  4.             final int particleRadius = 10;
  5.            
  6.             final ParticleSystem particleSystem = new ParticleSystem(physicsWorld, new ParticleSystemDef());
  7.             particleSystem.setParticleMaxCount(maxParticleCount);
  8.            
  9.             ParticleGroupDef groupDef = new ParticleGroupDef();
  10.             groupDef.flags.add(ParticleType.b2_waterParticle);
  11.             groupDef.position.set(28f,26f);
  12.            
  13.             PolygonShape parShape = new PolygonShape();
  14.             parShape.setAsBox(8f,16f);
  15.             groupDef.shape = parShape;
  16.             groupDef.linearVelocity.set(0f, 0f);
  17.             particleSystem.setParticleRadius(particleRadius/32f); // CHANGE TO PHYSICS CONSTANT !!!
  18.             particleSystem.createParticleGroup(groupDef);
  19.            
  20.            
  21.             final DynamicSpriteBatch particles = new DynamicSpriteBatch(resourcesManager.gameTextureAtlas4, maxParticleCount, vbom){
  22.                         @Override
  23.                         protected boolean onUpdateSpriteBatch() {
  24.                                 final float[] particlesPos = particleSystem.getParticlePositionBufferArray(true);
  25.                                 for(int i=0;i<particlesPos.length/2;i++)
  26.                                 {
  27.                                         this.draw(resourcesManager.wheelParticleTR, particlesPos[i*2]*32f-particleRadius, particlesPos[i*2+1]*32f-particleRadius, 2f*particleRadius, 2f*particleRadius, 0f, 0.5f, 0.65f, 1f, 1f);
  28.                                 }
  29.                                 return true;
  30.                         }
  31.             };
  32.             this.attachChild(particles);
  33.  
  34.             //////////end of particles
  35.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
User avatar
valka
 
Posts: 97
Joined: Tue Aug 13, 2013 3:17 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby GForce » Sat Apr 19, 2014 8:51 pm

A good way to render particles is first rendering each particle to an offscreen buffer with some kind of a luminace texture using glDrawArrays (it's very important to use this command instead of looping through all particles manually). The next step is to blur the result, but since we are on low end devices you should maybe (or actually you have to) skip this. The last step is to use a stepshader to draw the offscreen framebuffers texture to the screen.

I wrote a renderer for libgdx but you should be able to use this in andengine with some modifications.

The shader for the particles:
Syntax: [ Download ] [ Hide ]
Using c Syntax Highlighting
  1.                 final String vertexShader =
  2.                                 "attribute vec4 a_position;\n" //
  3.                                 + "\n" //
  4.                                 + "uniform float particlesize;\n" //
  5.                                 + "uniform float scale;\n"
  6.                                 + "uniform mat4 u_projTrans;\n" //
  7.                                 + "\n" //
  8.                                 + "void main()\n" //
  9.                                 + "{\n" //
  10.                                 + "   gl_Position =  u_projTrans * vec4(a_position.xy, 0.0, 1.0);\n" //
  11.                                 + "   gl_PointSize = scale * particlesize;\n" //
  12.                                 + "}\n";
  13.                 final String fragmentShader = "#ifdef GL_ES\n" //
  14.                               + "#define LOWP lowp\n" //
  15.                               + "precision mediump float;\n" //
  16.                               + "#else\n" //
  17.                               + "#define LOWP \n" //
  18.                               + "#endif\n" //
  19.                                         + "void main()\n"//
  20.                                         + "{\n" //
  21.                                         + " float len = length(vec2(gl_PointCoord.x - 0.5, gl_PointCoord.y - 0.5));\n" //
  22.                                         + " if(len <= 0.5) {\n" //
  23.                                         + "     gl_FragColor = vec4(" + pColor.r + "," + pColor.g + "," + pColor.b + "," + pColor.a + " * (1 - len * 1.8));\n" //
  24.                                         + " } else {\n" //
  25.                                         + "     gl_FragColor = vec4(0, 0, 0, 0);\n" //
  26.                                         + " }\n" //
  27.                                         + "}";
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


The step shader:
Syntax: [ Download ] [ Hide ]
Using c Syntax Highlighting
  1. "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
  2.                 + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
  3.                 + "uniform mat4 u_projTrans;\n" //
  4.                 + "uniform vec4 u_stepColor1;\n" //
  5.                 + "uniform vec4 u_stepColor2;\n" //
  6.                 + "varying vec2 v_texCoords;\n" //
  7.                 + "varying vec4 v_stepColor1;\n" //
  8.                 + "varying vec4 v_stepColor2;\n" //
  9.                 + "\n" //
  10.                 + "void main()\n" //
  11.                 + "{\n" //
  12.                 + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
  13.                 + "   v_stepColor1 = u_stepColor1;\n" //
  14.                 + "   v_stepColor2 = u_stepColor2;\n" //
  15.                 + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
  16.                 + "}\n";
  17.                 String fragmentShader = "#ifdef GL_ES\n" //
  18.                         + "#define LOWP lowp\n" //
  19.                         + "precision mediump float;\n" //
  20.                         + "#else\n" //
  21.                         + "#define LOWP \n" //
  22.                         + "#endif\n" //
  23.                         + "varying vec2 v_texCoords;\n" //
  24.                         + "varying vec4 v_stepColor1;\n" //
  25.                         + "varying vec4 v_stepColor2;\n" //
  26.                         + "uniform sampler2D u_texture;\n" //
  27.                         + "void main()\n"//
  28.                         + "{\n" //
  29.                         + "  gl_FragColor = mix(v_stepColor1, v_stepColor2, smoothstep(.4, .6, texture2D(u_texture, v_texCoords)[3]));\n" //
  30.                         + "}"
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


The render call:
Syntax: [ Download ] [ Hide ]
Using c Syntax Highlighting
  1.         public void render(boolean pUpdatePositionBuffer) {
  2.                 //Render the liquid to offscreen buffer
  3.                 mStepFrameBuffer.begin();
  4.                 Gdx.gl.glClearColor(0, 0, 0, 0);
  5.                 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  6.                
  7.                 Gdx.gl.glEnable(GL20.GL_BLEND);
  8.                 Gdx.gl.glBlendFunc(GL20.GL_ONE, GL20.GL_ONE);
  9.                 //Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  10.                 Gdx.gl.glEnable(GL20.GL_VERTEX_PROGRAM_POINT_SIZE);
  11.                 Gdx.gl20.glEnable(GL11.GL_POINT_SPRITE_OES);
  12.                
  13.                 mShader.begin();
  14.                 mShader.setUniformf("particlesize", mSystem.getParticleRadius());
  15.                 mShader.setUniformf("scale", mRadiusScale / mDivids);
  16.                 mShader.setUniformMatrix("u_projTrans", mCombinedMatrix);
  17.                
  18.                 mMesh.setVertices(mSystem.getParticlePositionBufferArray(pUpdatePositionBuffer));
  19.                 mMesh.render(mShader, GL20.GL_POINTS, 0, mSystem.getParticleCount());
  20.                 mShader.end();
  21.                 mStepFrameBuffer.end();
  22.                
  23.                 //Now render the step
  24.                 Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  25.                
  26.                 mStepFrameBuffer.getColorBufferTexture().bind(0);
  27.         mStepShader.begin();
  28.        
  29.         mStepShader.setUniformMatrix("u_projTrans", mStandMatrix);
  30.         mStepShader.setUniformi("u_texture", 0);
  31.         mStepShader.setUniform4fv("u_stepColor1", backgroundColor, 0, 4);
  32.         mStepShader.setUniform4fv("u_stepColor2", waterColor, 0, 4);
  33.        
  34.         mStepMesh.render(mStepShader, GL20.GL_TRIANGLES, 0, 6);
  35.        
  36.         mStepShader.end();
  37.         }
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


As the result you should get something like this:
Image
GForce
 
Posts: 10
Joined: Mon Mar 19, 2012 2:39 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby valka » Sun Apr 20, 2014 4:04 am

LiquidFun is awesome :)
thats what i added to my game (integrated it with RUBE)
Image

no shaders implemented yet :(
but im going to implement GForce's last post soon :)
User avatar
valka
 
Posts: 97
Joined: Tue Aug 13, 2013 3:17 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby UpGamer » Tue Apr 22, 2014 12:14 am

Finally I had some time to test this and it’s fun :D. I used valka's way of rendering particles. Test was done on Samsung Galaxy S3 with 2500 particles and it runs with 33+- fps. Here is a video :D.

Despite I am a beginner in programming, I will give it a try to implement GForce’s way of rendering ;).
User avatar
UpGamer
 
Posts: 3
Joined: Fri Mar 28, 2014 1:08 pm

Re: Liquid Fun - Box2D became even better - SUCCEED !!!

Postby valka » Tue Apr 22, 2014 12:02 pm

actually im implementing now shaders and it really works faster.
still iv'e got a lot to work on, coz i dont know nothing about shaders and im doing it with trial&error :)

so far ive managed to draw points (with size>1px) directly from the position buffer without manually copying arrays into spritebatch. only doing loop to multiply by PIXEL TO METER RATIO, but i think that this can be done faster with the correct shader code...
Last edited by valka on Tue Apr 22, 2014 1:38 pm, edited 1 time in total.
User avatar
valka
 
Posts: 97
Joined: Tue Aug 13, 2013 3:17 pm

PreviousNext

Return to Features

Who is online

Users browsing this forum: No registered users and 9 guests