Java LibGDX moving animation - java

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

Related

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

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.

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).

Model renders black in libGdx

After I updated my OS, i found that my models were not rendering their textures properly in LibGDX. The model would appear black instead of having a texture. This happened right after i updated my OS so, i don't know if it has anything to do with that.
Here is how it looks in blender:
and Here is how it looks in libGdx:
Notice how the textures for the model are not rendering.
Here is my create() method:
public void create () {
camera=new PerspectiveCamera(67,800,480);
camera.position.set(0f, 0f, 10f);
camera.lookAt(0f, 0f, 0f);
camera.near=1f;
camera.far=50f;
batch=new ModelBatch();
UBJsonReader jsonreader=new UBJsonReader();
G3dModelLoader modelloader=new G3dModelLoader(jsonreader);
playermodel=modelloader.loadModel(Gdx.files.getFileHandle("paulmodel.g3db", Files.FileType.Internal));
player=new ModelInstance(playermodel,0,0,0);
environment=new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight,0.65f,0.65f,0.65f,1f));
}
Here is my Render() method:
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST);
camera.update();
batch.begin(camera);
batch.render(player, environment);
batch.end();
camera.rotateAround(new Vector3(0f, 0f, 0f), new Vector3(0f, 1f, 0f), 0.5f);
}
Here is my model
Also, i seem to begetting an error distrib/android-emugl/host/libs/Translator/GLES_V2/GLESv2Imp.cpp:glActiveTexture:177 error 0x500
If you need anything else, please ask and i will post it.
Thanks in advance!
EDIT:
Here is the .G3dJ file as requested.
EDIT:
It works perfectly fine on a real phone but, it doesn't work on an AVD. Does this mean that it should work on all phones? Also, i have made the textures in the format of 2^n by 2^n pixels.

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