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.
Related
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.
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.
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
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
I'm working in a simple game for desktop and Android using LibGDX framework. The game spawn some balls that move from one side of the screen to the opposite one. I want to show white circles over a black background but I also want to draw some white texts. My problem with this is that I want to draw the intersection between balls and between balls and text in black. I want something like this:
Other of my problems is that I'm using ShapeRenderer to draw the circles and they are too pixelated.
What is the best way to render circles in libGDX (Ill have to render between 1 and 100). Is it possible to get the effect shown in the image?