21

In the two attached pictures, the desktop screenshot of libgdx functions as expected. The screenshot from my Galaxy Nexus is unfortunately not as expected. I am attempting to create a simple motion blur or trail effect.

Rendering as I expected on my desktop. Libgdx running on my desktop, working as expected

Not rendering as I expected on my Galaxy nexus. Libgdx running on my Galaxy Nexus not working as expected

The circle textures are drawn in a for loop during rendering and the effect is achieved with a pixmap using the RGBA of 0, 0, 0, 0.1f that is drawn before the circles.

screenClearSprite creation

Pixmap screenClearPixmap = new Pixmap(256, 256, Format.RGBA8888);
screenClearPixmap.setColor(Color.rgba8888(0, 0, 0, 0.1f));
screenClearPixmap.fillRectangle(0, 0, 256, 256);
screenClearTexture = new Texture(screenClearPixmap);
screenClearSprite = new Sprite(screenClearTexture);
screenClearSprite.setSize(screenWidth, screenHeight);
screenClearPixmap.dispose();

Render

batch.begin();
font.draw(batch, "fps:" + Gdx.graphics.getFramesPerSecond(), 0, 20);
screenClearSprite.draw(batch);
for (int i = 0; i < circleBodies.size(); i++) {
    tempPos = circleBodies.get(i).getPosition();
    batch.draw(circleTexture, (tempPos.x * SCALE) + screenWidthHalf
            - circleSizeHalf, (tempPos.y * SCALE) + screenHeightHalf
            - circleSizeHalf);
}
batch.end();

So, what did I do wrong? Perhaps there is a better way to get the 'motion blur' effect of movement?

ian.shaun.thomas
  • 3,468
  • 25
  • 40
  • Can you reproduce this behaviour on other android devices? – Stefan Hanke Jun 04 '12 at 16:29
  • 1
    Are you sure that there is a problem at all because the balls on the second screenshot look as if they are just moving slower compared to the balls on the first screenshot? What is your game loop? Do you use the current time for calculating the motion blur - may be the CPU of the nexus is much faster/slower compared to your PC CPU. – Svetlin Mladenov Jun 04 '12 at 17:09
  • Yes this reproducible on my Vibrant and a Galaxy Nexus S. As for the blur, their is no motion blur calculations. Motion blur may not be the most accurate term for the effect. The effect I am looking for is depicted accurately in the top image. The issue appears to be that the Texture drawn as an opacity layer does not calculate up to 1 properly. This causes the nasty streaking which I can no explain as it works as expected on my two pc's. – ian.shaun.thomas Jun 04 '12 at 19:09

2 Answers2

0

Here is a different approach, where you clear your screen each time with solid color and no alpha.

This means that you will have to modify your code some. The good thing about this, is that the way you are doing it has some flaws: It will blur everything in motion, not just the balls. And can quickly produce ugly results/artefacts unless you are careful.

  1. Do the same as you are doing now, but instead of drawing the balls to the batch, draw them onto a texture/bitmap/whatever. Then each frame add an alpha-blended image over the balls-image, and then draw the balls in their current position on top of that. Then add that image to your screen. Very much like you are doing now, except you draw to something else and keep it. This way you don't have to rely on the viewport you are drawing onto, and can keep everything separated. This method is similar to drawing to an accumulation buffer.

  2. Instead of doing it the way you are doing, you can keep track of the n latest positions of each ball. And then draw all of them each frame, with different alpha. This is very easy to implement. Can result in many drawing calls if you have many balls or a large n, but if it's not too much it shouldn't limit your fps and gives nice control.

Matsemann
  • 21,083
  • 19
  • 56
  • 89
  • #1 is probably the best option. I implemented #2 but the balls have to be moving extremely fast to see the trail so I need to change how often I save the previous location to get a good long trail going. Alternately, and I can not test this until later, is that maybe the sprite itself allows for modification of alpha which would be the same approach as my original but might work as intended. So create the pixmap with alpha of 1 then use a sprite alpha of 0.1f. Do sprites have that capability in Libgdx? – ian.shaun.thomas Jun 04 '12 at 19:42
  • @tencent You might use `glBlendFunc` together with `glBlendColor` to multiplicative blend the texture color with a constant color. – Stefan Hanke Jun 05 '12 at 05:04
0

Perhaps there is a better way to get the 'motion blur' effect of movement?

in order to make motion blur in my game i use another approch "The particle effect" it works realy fine with me and i didn't have Android/Desktop problems or with different android devices

all you have to do is to use "Particle Effect Editor" of Libgdx and make your effect then load it in your project finally draw it at the same position you draw your object (and alos draw your object)

Tips to make the right effect file with Paticle Editor :

  • set (use) the same image of the object that you want to blur it motion in the particle effect

  • try to limit the count : the max number of particle allowed

  • Disable the "velocity" and "Angle" parameter

Particle effect help to do motion effect Hope this will help someone !

Netero
  • 3,761
  • 2
  • 29
  • 29