Drawing Sprites although Origin is offscreen Libgdx - java

I´m developing my first game in Libgdx.
I have a Orthographic camera which follow a ball jumping like "Flappy Bird". My spinning obstacles have the origin in the middle because they are "attached" to some box2d bodies.
My problem is that Spritebatch doesn't draw the sprites while the origin of the sprite is offscreen and it looks like the obstacle is "teleporting" when the origin is onscreen and the sprite is drawn.
How can i "force" Spritebatch to draw a sprite even if the origin is offscreen ? I can't change the origin to the edge down because I need the sprite to spin.
Sorry for my bad english

Related

Libgdx Coordinate System Origin(0,0), not at bottom left of screen

I am working on a program to visualize an n-body particle simulation. To do this I have created a camera which is used to project particle positions unto the screen. Then, using a spritebatch these particles are rendered in the correct positions. However, for this to work, the origin of the coordinate system must be located at the bottom left of the screen. This is often not the case and instead the origin is located about a fourth of the screen up and to the left in all cases, and this changes when resizing the screen.
How do I guarantee the coordinate system origin is always at the bottom left of the screen, instead of elevated by a few hundred pixels in either axis?
Thank you.
If you decided to use sprite you should stick with proposed workflow and draw them with:
sprite.draw(batch);
I mean, what's the purpose using sprites if you are getting textures and drawing them?
When you draw a texture to screen at position (0,0) it means that it's bottom left corner will be at screen bottom left corner. That' is sprite's "hot-spot" or "origin" is at it's left bottom corner by default.
But you can change that with setOrigin method:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOrigin-float-float-
That is, origin is kinda "center" of the sprite, so i.e. if it rotates it will rotate around that spot. Also when you draw sprite at some coordinates sprites origin will be exactly at those coordinates. That way you don't have to adjust sprite drawing position.
Check on that page what methods you have for sprite class...
I fixed my problem. Instead of using:
sprite.draw(batch);
I used:
batch.setColor(color);
batch.draw(sprite.getTexture(),sprite.getX(),sprite.getY(),sprite.getWidth()*sprite.getScaleX(),sprite.getHeight()*sprite.getScaleY());
I do not know why this worked.

LibGDX - Basic 2D lighting, don't know what to do

It's my first time attempting to create a basic light system that uses a black texture with a white circle on top. I read various threads about the issue but I just don't know what I am doing wrong.
What I want is the ambient to be dark and the light to be well white but changing the spritebatch color to something darker will cause the light to be darker to EVEN if I reset the color when drawing the light texture
So this is what I want (did this by forcing the light texture draw 5 times but that isn't a solution, it's an hack):
This is what I get (only 1 light texture is drawn but isn't very visible):
This is what I get without darkening the spritebatch:
Main code:
Game.sb.begin();
//Make stuff darker
Game.sb.setColor(0.1f, 0.1f, 0.1f,1f);
sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
lvl.render();
//Reset color
Game.sb.setColor(1f, 1f, 1f,1f);
sb.setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_SRC_ALPHA);
//This draws all lights by drawing a texture with the above blending function
lightM.render();
Game.sb.end();
Light object draw method:
Game.sb.setColor(c.r,c.b,c.g, 1f);
Utils.drawTexture(Assets.get("sprites/lightcircle2.png", Texture.class), pos, size, true);
Game.sb.setColor(1,1,1,1);
Am I making some kind of error with the setcolor? I been considering using a FrameBuffer but I am not sure if it will give me the light effect I want
By the way this is my light texture (it's CC0):
You can achieve your requirement by these ways :
By using Shaders. Here is a small video and article on that video.
By use FBO and Blending, Here is one of my answer on this topic.
You can use box2dlight, even without using box2dbody(if you don't want any shadows)
World world = new World(new Vector2(0,0),false);
RayHandler rayHandler = new RayHandler(world);
rayHandler.setCombinedMatrix(stage.getCamera().combined); //<-- pass your camera combined matrix
new PointLight(rayHandler,1000, Color.WHITE,radius,x_position,y_position);
And at last call rayHandler.updateAndRender(); after all your rendering in your render() method.

LibGDX Sprite + Box2D CircleShape: Positioning while taking into account origin

My problem is, that my Box2D Body has another position than the LibGDX Sprite I want to render for that body. In my render-loop, for each body, I'm setting the position of it's sprite to the one of the body, and then rendering it.
When creating a Box2D Shape other than a circle, Box2D does not move it from it's origin, and neither does the LibGDX-sprite. If I now move or set the position of the body, my sprite will always follow it. But, unfortunately, this is not possible with CircleShapes: Since LibGDX's Sprite#setPosition does not take into account the origin of the sprite (Which is only used for scaling and rotating), the sprite is set by it's lowerleft corner. So here is the problem: The Box2D CircleShape is moved by taking into account the origin! So my sprite always starts in the origin of the shape. Does anyone know how to fix that? And, ultimately, I'd want to always move both while taking into account the origin. How do I do that?
Box2d body origin is never changed. The Circle and Box shape's origin is middle and the polygon shape's origin is the bottom left corner.
The only way to fix it, you change the sprite origin to middle that is Sprite.setOriginCenter();.
If body is a circle or box shape, the sprite position set like as sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);.
If body is a polygon shape the code should be like as sprite.setPosition(body.getPosition().x, body.getPosition().y);.

Draw all textures flipped?

First off, with my background in XNA i just cannot get used to the inverted Y axis. So in LibGDX i flipped the OrthographicCamera with cam.setToOrtho(true,width, height) but this obviously ends up in drawing all my textures upside down.
I can create Sprites and TextureRegions from all my textures to flip each and every one of them but that takes a lot of extra code. So is there a efficient way to have all my textures flipped around there center?
I tried adding a flipped matrix to the spritebatch transformMatrix but that cancels out the flipped ortho cam. I also tried to create a Sprite for drawing all my textures and flips them but without success.
you can probably just use flip, like so.
textureRegion.flip(false, true);
for every TextureRegion you have.
Since Sprites extend TextureRegions, this should work for them too

Check a prefect collision between rotating texture and a curve (libgdx)

I'm developing a game where there is a texture that the player need to move around the screen without touching the borders which are black lines and curves.
The figure rotates while the player moves it and the borders are fixed the entire game.
I need a good method to check a prefect collision between the border and the texture.
I tried to make a rectangle around the texture and check for black and green (texture color) pixels that are near each other inside the rectangle but it's too slow.

Categories

Resources