Attaching body to SpriteBatch - java

Im trying to get a SpriteBatch drawn to sync up to the position of a body.
Im not sure if this is the proper way to do this (in andengine you would just use physics connectors) But i tried drawing the sprite at the position the body was storing.
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
elapsedTime += Gdx.graphics.getDeltaTime();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(walkAnimation.getKeyFrame(elapsedTime, true),
mrsaiBody.getPosition().x, mrsaiBody.getPosition().y, width/2, height/2);
batch.end();
mWorld.step(1/30f, 6, 2);
mRenderer.render(mWorld, camera.combined);
The problem im having with this is, on the screen i see mrsaiBody's shape and i see the ground i made. When the shape makes contact with the ground the x and y values still continue to be affected by gravity even though the shape isnt. How can i get the current position of this shape to match up with position when i use batch.draw?

Related

How to delete texture above from under texture?

I'm making a scratchcard mini-game and I want to make scratchcard texture erase-able with other texture, I have scratchcard texture (colorful) and a mask texture (circle while) I'am trying to make so that when mask texture is on scratchcard texture it become a 'hole' you see trough it and see the background.
I've tried making it with blending
This is the code I found on stackoverflow from a different topic which I tried to modify, but it didnt seem to work.
// draw our destination image
super.draw(batch, parentAlpha);
batch.end();
// remember SpriteBatch's current functions
int srcFunc = batch.getBlendSrcFunc();
int dstFunc = batch.getBlendDstFunc();
// Let's enable blending
batch.enableBlending();
batch.begin();
// blend them
batch.setBlendFunction(GL20.GL_ZERO, GL20.GL_ONE_MINUS_DST_ALPHA);
image.setPosition(Gdx.input.getX() - (image.getWidth() / 2), -Gdx.input.getY() + (1280 * GambleRPG.SCALE_Y) - (image.getHeight() / 2));
image.draw(batch, parentAlpha);
// Reset
batch.end();
batch.begin();
batch.setBlendFunction(srcFunc, dstFunc);

LibGDX Texture setWrap method doesn't repeat

Hello I'm tring to make a neverending background. So I try to wrap my texture.
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
And draw it:
bach.begin();
bach.draw(texture, 0, 0);
bach.end();
I'm ending with no changes to the texture when I use setWrap.
If i draw the texture that way:
bach.begin();
bach.draw(texture, 0, 0, texture.width, texture.height, 0, 0, 1, 1);
bach.end();
It repeats the texture but flipped...
If I try to flip the y and x in the bach.draw I get an error.
I only can flip the camera but then the position y flips too (translating up goes negative value / translating down goes positive value)
To repeat a texture, you need it to use UVs bigger than one, which you can do with a TextureRegion that is bigger than the Texture it references.
TextureRegion backgroundTextureRegion = new TextureRegion(texture, bgWidth, bgHeight);
//...
batch.begin();
batch.draw(backgroundTextureRegion, 0, 0, cameraWidth, cameraHeight);
batch.end();
Where bgWidth and bgHeight are how many texels wide and high you want to draw the background. For instance, if your camera's viewportWidth is 1920 and you want your texture to be drawn at 1:1 scale (texture pixels : camera units), then bgWidth would be 1920.
If you need to flip it vertically, you can use -bgHeight instead without messing with the camera.

Libgdx: Stage is messing with batch

I have a problem with my code, which appeared after i started using steps to draw my UI, I have 3 things being drawn with Batch, and a stage with 2 buttons being drawn by Stage, but only 2 of my "Sprites" are being drawn. Here is the code:
public void render(float delta) {
Gdx.gl.glClearColor(0.95F, 0.95F, 0.95F, 0.95F);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
deltaTime = Gdx.graphics.getDeltaTime();
camera.update();
generalUpdate(touch, camera, deltaTime, pointer);
bounce(deltaTime);
stage.setCamera(camera);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(Assets.spr_bg, 0, 0);
batch.draw(Assets.spr_title, 540-Assets.spr_title.getWidth()/2, titleY-Assets.spr_title.getHeight()/2);
Assets.font_small.draw(batch, "some text", 540-Assets.font_small.getBounds("(c)2014 HateStone Games").width/2, 1920-64-Assets.font_small.getBounds("(c) HateStone Games").height/2);
stage.draw();
batch.end();
}
It is the text "Some text" which doesn't get drawn, and if i comment it out, it goes crazy, the title sprite doesn't get drawn either, and a gray box appears at random intervals.
Also if I move "stage.draw();" outside of the batch it doesn't get drawn
The Stage has its own SpriteBatch, which draws everything. So you begin() a SpriteBatch while your other SpriteBatch is drawing. So call batch.end() before calling stage.draw(). This should solve your problem.

Using filledcircle and pixmap in libgdx

I am using Libgdx.
I want to simulate fog in my game using pixmap, but I have a problem during generating the "fogless" circle. First, I make a pixmap, filled with black (it is transparent a little bit). After filling I want to draw a filled circle onto it, but the result is not that I expected.
this.pixmap = new Pixmap(640, 640, Format.LuminanceAlpha);
Pixmap.setBlending(Blending.None); // disable Blending
this.pixmap.setColor(0, 0, 0, 0.9f);
this.pixmap.fill();
//this.pixmap.setColor(0, 0, 0, 0);
this.pixmap.fillCircle(200, 200, 100);
this.pixmapTexture = new Texture(pixmap, Format.LuminanceAlpha, false);
In the procedure render()
public void render() {
mapRenderer.render();
batch.begin();
batch.draw(pixmapTexture, 0, 0);
batch.end();
}
If I use Format. Alpha when creating the Pixmap and Texture, I neither see the more translucent circle.
Here is my problem:
Problem
Could somebody help me? What should I do, what should I init before to draw a full transparent circle? Thanks.
UPDATE
I have found the answer for my problem. I have to disable blending to avoid the problem.
Now my code:
FrameBuffer fbo = new FrameBuffer(Format.RGBA8888, 620, 620, false);
Texture tex = EnemyOnRadar.assetManager.get("data/sugar.png", Texture.class);
batch.begin();
// others
batch.end();
fbo.begin();
batch.setColor(1,1,1,0.7f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw( tex, 100, 100);
batch.end();
fbo.end();
But I don't see the circle (it's a png image, represents transparent bg, white filled circle).
I am not sure if this works for you but i just share it:
You could use FrameBuffers and do the following:
Draw everything you want to draw on screen.
End your SpriteBatch and begin your FrameBuffer, begin the SpriteBatch again.
Draw the Fog, which fills the whole "screen" (FrameBuffer) with a black, non transparent color.
Draw the "Fogless" circle as a white circle, at the position you want to delete the fog.
Set the FrameBuffers alpha channel (transparancy) to 0.7 or something like that.
End the SpriteBatch and the FrameBuffer to draw it to screen.
What happens? You draw the normal scene, without fog. You create a "virtual screen", fill it with black and overdraw the black with a white circle. Now you set a transparacy to this "virtual screen" and overdraw your real screen with it. The part of the screen, which is under the white circle seems to be bright, while the black rest makes your scene darker.
Something to read: 2D Fire effect with libgdx, more or less the same as fog.
My question to this: Libgdx lighting without box2d
EDIT: another Tutorial.
Let me know if it helps!
EDIT: Some Pseudocode:
In create:
fbo = new FrameBuffer(Format.RGBA8888, width, height, false);
In render:
fbo.begin();
glClearColor(0f, 0f, 0f, 1f); // Set the clear color to black, non transparent
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the "virtual screen" with the clear color
spriteBatch.begin(); // Start the SpriteBatch
// Draw the filled circles somehow // Draw your Circle Texture as a white, not transparent Texture
spriteBatch.end(); // End the spritebatch
fbo.end(); // End the FrameBuffer
spriteBatch.begin(); // start the spriteBatch, which now draws to the real screen
// draw your textures, sprites, whatever
spriteBatch.setColor(1f, 1f, 1f, 0.7f); // Sets a global alpha to the SpriteBatch, maybe it applies alo to the stuff you have allready drawn. If so just call spriteBatch.end() before and than spriteBatch.begin() again.
spriteBatch.draw(fbo, 0, 0); // draws the FBO to the screen.
spriteBatch.end();
tell me if it works

libgdx - removing the circle outline rendered on Box2d CircleShape

CircleShape circle = new CircleShape();
circle.setRadius(1f);
...
using
...
batch.draw(textureRegion, position.x - 1, position.y - 1,
1f, 1f,
2, 2,
1, 1,
angle);
I use this to set the body for a Box2d collision but I get a silly circle shape around my texture in libGdx, i.e. my textured sprite (ball) has a circle over the top of it with a line running from center along the radius.
Any ideas on how to remove the overlying circle lines?
Not 100% sure, haven't used Box2d with libgdx in a while. But look if you have any Box2DDebugRenderer that renders the box2d world. If so, just don't call its render() method.
yes, uncomment the following line.
debugRenderer.render(world, viewport.getCamera().combined);

Categories

Resources