LibGdx - Batch is not drawing the last .draw command - libgdx

So this is pretty weird...
I have an abstract BaseScreen. This screen initializes a SpriteBatch on show(). The init() calls the one of the BaseScreen's abstract methods.
This draw issue is from using the 'gameView' viewport which is an ExtendViewport
SpriteBatch batch;
#Override
public void show(){
batch = new SpriteBatch();
gameView = new ExtendViewport(SV.V_WIDTH, SV.V_HEIGHT);
hudView = new ScreenViewport();
init();
}
This is what I do in the render function:
#Override
public void render(float delta){
/*
Just in case render is being called more than usual 60 times,
I want to call update only 60 times per second. (SV.STEP = 1/60f)
*/
accum += delta;
while(accum >= SV.STEP){
accum -= SV.STEP;
update();
}
/* Clear Screen */
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
/* DRAW WORLD */
batch.setProjectionMatrix(gameView.getCamera().combined);
gameView.apply(true);
batch.begin();
draw(batch, SV.BatchType.World);
batch.end();
/* DRAW UI
batch.setProjectionMatrix(hudView.getCamera().combined);
hudView.apply(true);
batch.begin();
draw(batch, SV.BatchType.UI);
batch.end();*/
}
I have two viewports, one for the game world and one for hud. I use them to set the projection matrix of the batch.
The draw(batch, SV.BatchType.World) command ultimately takes the code to here:
private void drawBody(Batch batch){
Vector2 position = body.getPosition();
//Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
}
Now this piece of code draws this:
Notice how it doesn't draw the text.
Here's the result with batch.draw only - (font.draw is omitted)
Now the issue becomes 'what type of .draw was called last' and it ignores all of them - For example:
private void drawBody(Batch batch){
Vector2 position = body.getPosition();
//Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
}
Will draw
and this
private void drawBody(Batch batch){
Vector2 position = body.getPosition();
//Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
batch.draw(heart, position.x,position.y, h_width*2, h_height*2);
batch.draw(heart, position.x,position.y+ 100, h_width*2, h_height*2);
}
Will draw
Here is an example with 6 draws, but you have to have a 'throwaway' last .draw
private void drawBody(Batch batch){
Vector2 position = body.getPosition();
//Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
batch.draw(heart, position.x,position.y, h_width*2, h_height*2);
batch.draw(heart, position.x,position.y+ 100, h_width*2, h_height*2);
font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
batch.draw(heart, 10,10, h_width*2, h_height*2);
}
Here's one last thing... the last batch draw... if you try to draw it at 0, 0 -> this happens
Seeking any kind of helpful feedback and/or reason why this may be happening. I'm a newbie with LibGdx and would not shun your feedback on how I decided to use this framework.
Thanks a bunch!

So I narrowed down what is causing it:
it doesn't seem that the batch will be bugged when you are
1) Instantiating the batch on a BASE class
2) calling batch.start on the BASE class render method
3) calling one of the abstract methods in the BASE class after batch.start
4) The batch will be bugged.
What I did to solve this problem was to move the batch code into the derived class, and everything works fine.
I'm still opened to answers as to why this bug happens.

Related

Dragging a texture (without jump)

How can I drag a texture (image) without making it jump and centering itself on the mouse cursor (what I have below in my code)?
When I click on the texture, nothing should happen, and when I drag, the mouse cursor should stay in place (relative to the edges of the texture).
Example: https://s31.postimg.org/ojapwbj6j/Untitled_1.jpg
SpriteBatch batch;
Texture texture;
OrthographicCamera camera;
Vector3 spritePosition = new Vector3();
float offsetX, offsetY;
Vector3 input;
#Override
public void create () {
batch = new SpriteBatch();
texture = new Texture("badlogic.jpg");
camera = new OrthographicCamera();
camera.setToOrtho(false);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, spritePosition.x, spritePosition.y);
batch.end();
if (Gdx.input.justTouched()){
int x1 = Gdx.input.getX();
int y1 = Gdx.input.getY();
input = new Vector3(x1, y1, 0);
camera.unproject(input);
offsetX = x1 - spritePosition.x;
offsetY = y1 - spritePosition.y;
}
if (Gdx.input.isTouched()) {
spritePosition.set(Gdx.input.getX() - offsetX, Gdx.input.getY() - offsetY, 0);
}
}
What you need to do is work out the offset of the mouse position relative to your image, and subtract that, rather than half the width / height as you're doing at the moment.
Here's some very rough pseudocode to show what I mean - You'll need to rewrite as Java...
if Gdx.input.justTouched {
get mouse position from Gdx.input
camera.unproject it into cameraX and cameraY
offsetX = cameraX - imageX
offsetY = cameraY - imageY
}
if Gdx.input.isTouched {
spritePisition.set(Gdx.input.getX() - offsetX, Gdx.input.getY - offsetY)
}
Why are you using camera.unproject?
Try this:
Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture, spritePosition.x, spritePosition.y);
batch.end();
if (Gdx.input.isTouched()) {
spritePosition.set(Gdx.input.getX() - texture.getWidth() / 2, Gdx.input.getY() + texture.getHeight() / 2, 0);
}
}

Box2d Body to follow mouse movement

I am trying a box2d body to rotate following the mouse. Here's an image to clarify what I mean.
The red and blue circles are current point of the mouse and the body (of corresponding color) moving/rotating to follow it. Basically the rectangle should rotate with its one end pointed towards where the mouse pointer is.
Here's my code so far,
World world;
Body body, bullet;
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
#Override
public void create() {
world = new World(new Vector2(0, 0f), true);
debugRenderer = new Box2DDebugRenderer();
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(10, 10);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(2, 5);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
Gdx.input.setInputProcessor(this);
camera = new OrthographicCamera(200, 100 * (h / w));
camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
camera.update();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
camera.update();
debugRenderer.render(world, camera.combined);
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
int mouseX = screenX;
int mouseY = Gdx.graphics.getHeight() - screenY;
float rotateAngle = MathUtils.radiansToDegrees * MathUtils.atan2((float) mouseY - (float) body.getPosition().y, (float) mouseX - (float) body.getPosition().x);
body.setTransform(body.getPosition(), rotateAngle / 57.2957795f);
return true;
}
And here's a gif of how it appears now.
as you can see the body gets skewed and also doesn't rotate properly. What am I missing?
The skew looks like a problem with aspect correction, try,
camera = OrthographicCamera(200, 100 * (w / h));
or even,
camera = OrthographicCamera(200, 100);
To get the end of the box to follow the mouse as in the picture just redefine the box,
shape.setAsBox(5, 2, Vector2(offset, 0), 0);

libgdx BitMapFont not working

I am trying to use BitMapFont in libgdx based game.I do this:
BitmapFont font;
this.font = new BitmapFont();
render{
Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f,
0xff / 255.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.app.log(TAG, " FPS: " + Gdx.graphics.getFramesPerSecond());
batch.setProjectionMatrix(CameraManager.camera.combined);
batch.begin();
font.setColor(Color.BLACK);
font.draw(batch, "Time", 4, 4);
batch.end();
}
And my camera is managed as:
public class CameraManager {
public static OrthographicCamera camera;
public CameraManager(){
camera = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
camera.viewportWidth = (camera.viewportHeight)
* ((float) Gdx.graphics.getWidth() / (float) Gdx.graphics
.getHeight());
camera.position.set(camera.viewportWidth / 2,
camera.viewportHeight / 2, 0);
camera.update();
}
public void resize(int width, int height) {
camera.viewportWidth = (camera.viewportHeight)
* ((float) width / (float) height);
camera.update();
}
}
But instead of text, several rectangles are displayed whose color is the color I pass to setColor().I really have no idea whats happening here. Most of the tutorials use this simple piece of code.But it is just not working for me.
Please advise.
I would recommend downloading some .ttf fonts and passing them in into the bitmap font object like so
BitmapFont font = new BitmapFont(Gdx.files.internal("fontfile.ttf"));
Here are a lot of great .ttf fonts you can download https://www.google.com/fonts

my sprite dont show in libgdx

Hi i am new to libgdx and try to build a ludo game.right know i could draw the board but when i try to show dice or any peaces nothing happens I attach game code .
please read and help.
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera = new OrthographicCamera(1, h / w);
boardtexture = new Texture(Gdx.files.internal("data/ludo-boardx2.png"));
boardtexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
boardsprite = new Sprite(boardtexture);
boardsprite.setSize(1f,
1f * boardsprite.getHeight() / boardsprite.getWidth());
boardsprite.setOrigin(boardsprite.getWidth() / 2,
boardsprite.getHeight() / 2);
boardsprite.setPosition(-boardsprite.getWidth() / 2,
-boardsprite.getHeight() / 2);
Texture dice = new Texture(Gdx.files.internal("data/dices.png"));
dice.setFilter(TextureFilter.Linear, TextureFilter.Linear);
dtemp = new Sprite(dice);
dtemp.setSize(1f, 1f * dtemp.getHeight() / dtemp.getWidth());
dtemp.setPosition(0f, 0f);
// System.out.println(w/2+" " +h/2);
// System.out.println(dtemp.getWidth()+":"+dtemp.getHeight());
// animatedDice.setPosition(new Vector2(0, 0));
System.out.println(animatedDice.getX() + ":" + animatedDice.getY()
+ "\n" + w + ":" + h);
}
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
// camera.update();
handleInput();
batch.begin();
boardsprite.draw(batch);
dtemp.draw(batch);
//animatedDice.draw(batch);
batch.end();
// animatedDice.update();
}
You are drawing your die at a position of (w/2, h/2), which is way out of range of your viewport. You set your viewport so the X coordinate of the right side of your screen is 0.5, but your sprite's left side is at x=w/2, where w is the number of pixels wide your window is.
Even if you had set your camera size to match the window ((w, h)), you would have an issue here because you'd be drawing your sprite so its left edge matched the right edge of the screen. By default, the origin of your die sprite will be its lower left corner.
If you aren't planning to zoom your scene in and out, it might be simpler to set your camera viewport to the size of the window by using camera = new OrthographicCamera(w, h); Then you'll know that your units are equivalent to pixels.

AS3: Problems with ScaleX/ScaleY and Width/Height

I'm using FlashDevelop to test a menu-type system that I want to use for a game, and I'm having serious issues figuring out either the Width and Height or the ScaleX and ScaleY (I'm not really sure which set is causing the problems) of the Sprite subclasses. I've read many things that seem to be the solution to my problem, including resizing the sprite subclass after I add any children, but none seem to work. Right now, I have 3 separate Classes.
Main:
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var BF:BFi = new BFi;
this.addChild(BF);
this.width = 640;
this.height = 480;
trace(this + "Dimensions: " + width + ", " + height);
trace(this + "Coordinates: " + x + ", " + y);
}
}
BFi:
public class BFi extends Sprite
{
public function BFi()
{
var BFMenu:BFiMenu = new BFiMenu;
addChild(BFMenu);
trace(getChildAt(0));
this.x = this.y = 0;
this.width = 640;
this.height = 480;
BFMenu.x = this.y = 0;
BFMenu.width = 640;
BFMenu.height = 480;
trace(this + "Dimensions: " + width + ", " + height);
trace(this + "Coordinates: " + x + ", " + y);
}
}
and BFiMenu:
public class BFiMenu extends Sprite
{
[Embed(source = "../Assets/Button1.png")]private var Button1:Class;
public function BFiMenu()
{
var Bounds:Sprite = new Sprite;
Bounds.graphics.lineStyle(2, 0, 1);
Bounds.graphics.drawRect(0, 0, 640, 480);
addChild(Bounds);
var NewButton:ComplexButton = new ComplexButton(220, 405, 200, 50, Button1, "PLAY", "Block", 50, 0x000000, function Button1(e:MouseEvent):void { /*parent.removeChildAt(0);*/ } );
//var NewButton:ComplexButton = new ComplexButton(0, 0, 200, 50, Button1, "PLAY", "Block", 50, 0x000000, function Button1(e:MouseEvent):void { parent.removeChildAt(0); } );
addChild(NewButton);
this.width = 640;
this.height = 480;
trace(this + "Dimensions: " + width + ", " + height);
trace(this + "Coordinates: " + x + ", " + y);
trace(NewButton + "Dimensions: " + NewButton.width + ", " +NewButton.height);
trace(NewButton + "Coordinates: " + NewButton.x + ", " + NewButton.y);
}
}
the rectangle named "Bounds" has the height and width of what I want (640x480), but the button that I placed in, supposed to have a y-coord of about 450, instead has a y-value of nearly 800, and is clearly distorted, but still on screen, even though the height is only 480.
The "ComplexButton" Class that is called is my own button class, and I've tested that enough to know that it works fine. when I run this program, it Distorts everything and nothing is where I wanted it to be. Any help would be very appreciated.
Few notes:
Any time a display object is created, the initial x and y coordinates are defaulted to zero, so you don't need to initialize them to 0 yourself.
Changing the width and height of an empty display object will do nothing. If the display object has graphical content, then it will scale that content so that it matches the desired width and height, which would also change the scale factors.
If you are going to draw a shape in a Disp.Object to give it a width and height, you do not then need to set those values explicitly.
Try removing each of the 3 places where you explicitly set those values.