Problem with rotation sprite in LibGdx. The sprite doesn't rotate - java

When I try to rotate an object around its axis around its axis a sprite of game objects I using the rotate() and setRotation() commands, the sprite does not rotate.
Also, when I try to rotate the actor I using the rotateBy() and setRotation() commands, nothing happens too.
In my project, drawing game objects (actors) occurs via the draw() method in Stage.
I noticed that if I draw an object using the batch.draw(sprite, x, y) method, the object will not rotate.
code from public class MyGdxGame extends ApplicationAdapter
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(sprite, 0, 0);
sprite.rotate(1);
batch.end();
}
And if I draw an object using the sprite.draw(batch) method, the object rotates calmly.
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
sprite.rotate(1);
batch.end();
}
Perhaps this is the reason for my problem.

Related

Textures in different positions from bodies in LibGDX

I'm making a prototype project in LibGDX and I'm using Box2D for the physics.
I've created a map and added some collision to him, i added entities that has circle shapes too.
These circles are correctly positioned in the World of Box2D, but the textures that i want to get fixed to them are getting based on other coordinates, from the user camera I think.
This is the result:
My render method in the base "Entity" class
public void update() {
handleInput();
}
public void render(SpriteBatch batch) {
update();
batch.begin();
batch.draw(texture, body.getPosition().x, body.getPosition().y);
batch.end();
}
My render method in the BaseMap class
private void update(OrthographicCamera camera) {
camera.position.x = player.getPosition().x;
camera.position.y = player.getPosition().y;
world.step(1/60f, 6, 2);
}
public void render(OrthographicCamera camera, SpriteBatch batch) {
update(camera);
renderer.setView(camera);
batch.begin();
renderer.render();
batch.end();
for(Entity entity : entities) {
entity.render(batch);
}
}
The renderer here is a OrthogonalTiledMapRenderer
Make sure you're setting the matrix for the batch as well:
batch.setProjectionMatrix(camera.combined);
Likewise for the Box2DDebugRenderer:
renderer.render(world, camera.combined);
This is assuming you aren't using a scaled box2d world.

The text drawn using "batch" is hidden by a rectangle drawed using "renderer"

I've just begun using LibGDX and I'm encountering a problem using batch:
I have a render method in my HomeScreen that renders pretty much everything on the screen, including some Buttons. In my class Button, I render the buttons and afterwards, I use a batch to draw a text. The problem is that the text is drawn behind the rectangle of the button (even if the batch begins after the renderer of the rectangle) don't know if it's clear so here's some code:
In the Screen class:
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
viewport.apply();
vector3.set(Gdx.input.getX(), Gdx.input.getY(), 0);
viewport.unproject(vector3);
renderer.setProjectionMatrix(camera.combined);
renderer.setAutoShapeType(true);
renderer.begin(ShapeRenderer.ShapeType.Filled);
homeStars.render(renderer);
for (Button button: buttons)
{
button.render(renderer);
}
renderer.setColor(1,1,1,1);
//realCursor = cursorToWorldPosition(Gdx.input.getX(),Gdx.input.getY());
//renderer.circle((vector3.x+1)*Const.WORLD_WIDTH/2,(vector3.y+1)*Const.WORLD_HEIGHT/2,5);
renderer.circle(vector3.x,vector3.y,5);
renderer.set(ShapeRenderer.ShapeType.Line);
for(HomeStar star : HomeStars.stars)
{
if(vector3.dst(star.position)<Const.MAX_DIST_MOUSE_STAR){
renderer.line(vector3.x, vector3.y,star.position.x,star.position.y,Color.WHITE, Color.BLUE);
}
}
}
And in the Button class:
public void render(ShapeRenderer renderer){
if (mouseOn() == true){
renderer.rect(position.x-width/2,position.y-height/2,width, height, Color.BLACK, Color.VIOLET, Color.BLACK, Color.VIOLET);
}
else if (mouseOn() == false){
renderer.rect(position.x-width/2,position.y-height/2,width, height, Color.BLACK, Color.GRAY, Color.BLACK, Color.GRAY);
}
batch.begin();
font.draw(batch, "yo", position.x, position.y);
batch.end();
}
Any help would be much appreciated, thank you!
You can't nest SpriteBatch and ShapeRenderer. Make sure to call renderer.end() before calling batch.begin() and then call renderer.begin() after calling batch.end():
renderer.end();
batch.begin();
font.draw(batch, "yo", position.x, position.y);
batch.end();
renderer.begin();
Since you are learning, that should do it for now. Note however that this will get quickly very inefficient, because it defeats the purpose of batching. You should avoid constantly switching between ShapeRenderer and SpriteBatch. SpriteBatch is perfect to render rectangles, so you might want to work to using only SpriteBatch for your buttons (or use scene2 for UI).

Setting the applcation to use "y-down" in LibGDX

http://badlogicgames.com/forum/viewtopic.php?f=11&t=2447
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html
There's hundreds of other links I could show you that I've looked at, but it's just not worth it because they all say the same thing.
public class InGame implements Screen {
SpriteBatch batch;
GameWorld world;
OrthographicCamera camera;
#Override
public void show() {
batch = new SpriteBatch();
world = new GameWorld();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
world.render(batch, delta);
batch.end();
}
}
What am I doing wrong? WHY is my world still being rendered with the 0,0 being at the bottom right. The math behind this while trying to work on my Tile-System is driving me absolutely insane.
World->Render
public void render(SpriteBatch batch, float delta) {
for(int xAxis = 0; xAxis < worldColumns; xAxis++) {
for(int yAxis = 0; yAxis < worldRows; yAxis++) {
tiles[xAxis][yAxis].render(batch, delta);
}
}
}
WorldTile->Render
public void render(SpriteBatch batch, float delta) {
myShape.begin(ShapeType.Filled);
myShape.setColor(tileColor);
myShape.rect(pos.getX(), pos.getY(), TILE_WIDTH, TILE_HEIGHT);
myShape.end();
}
The "pos" is the Position(x, y) that was passed in the World class.
If you are drawing a Sprite or TextureRegion using your SpriteBatch the code should work fine. However, you pass your SpriteBatch 'batch' all the way down to WorldTile.render and never use it?! Instead you use myShape which I assume is a ShapeRenderer. You need to set the projection matrix for the ShapeRenderer as well otherwise it will draw 'upside-down'.
Try calling myShape.setProjectionMatrix(camera.combined); before you use your myShape.
Its probably best to declare and initialise myShape in your InGame class, usemyShape.setProjectionMatrix(camera.combined);, and then pass myShape down to tiles.render() as you did with your SpriteBatch.
Hope this helps.

Java LibGDX moving animation

How can I move Sprite fluently?I have tried in this way.
#Override
public void render(float delta) {
Gdx.graphics.getGL20().glClearColor( 1F, 1F, 1F, 1F );
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
mySprite.draw(batch);
mySprite.setX(BladeAnimation.bladeFalling());
batch.end();
}
I have changed xPosition with other function but the animation is not playing fluently.
You have to run the code that is changing the position in the render() method, other wise it changes only once. Unless if you are changing the position elsewhere. Show the code that changes the position to get a better answer

Not sure why this isnt rendering

So I have a splash image that is not rendering and I'm not really sure why. I have done the correct process, creating anew sprite and then in my render method rendering it with the batch like so:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void show() {
splashSprite = new Sprite(new Texture("data/xidstudios_splash.png"));
batch = new SpriteBatch();
}
As you can see, I have done the correct process of steps, am I missing one?
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
You must provide the viewports to opengl either using a camera or directly calling the opengl func so that your projection are currectly set to view the image

Categories