how to draw custom shapes using a simple texture in libgdx? - java

I want to draw custom static shapes in my game just like the angry birds done:
but the problem is that the texture is in square shape.
how we can tell to the "SpriteBatch" to draw shapes with some vertices? does anybody has some ideas?

According to the docs:
A SpriteBatch is used to draw 2D rectangles that reference a texture (region)
So essentially, this method does not allow you to specify any other geometry than a rectangle. However, there is a draw method that allows you to specify the four coordinates of the "rectangle" and turn it into something else. You'd've to decompose the geometry into smaller pieces that fits this primitives. Looks like this method doesn't lend itself to geometry changes.
Better you create a mesh and just draw it. The mesh does not support triangulation, so use the ObjLoader.loadObj* methods to load a mesh in Alias Wavefront OBJ format. Specifying the geometry shouldn't be a hassle; the article links to some example files.

Related

How do I make an isometric hitbox?

I am making a game in Java that uses an isometric grid pseudo-3D system. It uses tiles that have this basic shape:
I can't work out how to make a hitbox that covers all the area of a tile, and only the area of a tile. I need this because all my tiles are stacked and hitbox stacking can produce unwanted results.
It would depend a bit on your targeted framework (as it might already support the functionality), but at a very basic level, you could make use of the Java2D's Shape API to create a polygon representing the shape of the tile and use it's contains and intersects functionality
As a conceptual example

How to Fill color inside shape using setPixel method?

I need to fill the color inside custom shape. I know one of the pixel coordinate inside the shape and also know the color of the shape. I need to get all pixel inside the shape calculate from knowing pixel coordinate how can i write logic for this can some one please help.
For already rendered shapes
use Flood Fill or Boundary fill algorithms. For example see:
my C++ implementation of Flood&Boundary fill
beware most implementations are recursive so there is a high risk of stack/heap overflow for bigger areas. There are also iterative approaches using dynamic point lists which are safer.
For polygons in vector form
Use convex polygon filling. In case you got concave polygons you need to divide them into convex polygons first. Another options is to triangulate. Both triangle and convex polygon rasterization is the same see:
Algorithm to fill triangle
how to rasterize rotated rectangle (in 2d by setpixel)
If you need triangulation see:
Wiki: Polygon triangulation
In case of very complicated shapes and not possible triangulation for any reason you can try fill all the pixels in the bounding box of polygon that are inside polygon. For that you need:
hit-test

Java how to draw and fill a Polygon which has holes

I am currently trying to draw and fill a Polygon which has a hole in it in Java. Normally this would not be a big problem, since I would draw the exterior ring and then draw the interior ring with the color of the background.
But the problem is, that the polygon is displayed above a image which should be "seen" through the hole.
I am writing the code in Java and am using JTS Topology Suite for my geometry data.
This is my current code, which just paints the border and fills the polygon with a color.
private void drawPolygon(com.vividsolutions.jts.geom.Polygon gpoly, Color color, Graphics2D g2d){
java.awt.Polygon poly = (java.awt.Polygon)gpoly;
for(Coordinate co : gpoly.getExteriorRing().getCoordinates() {
poly.addPoint(co.x, co.y);
}
g2d.setColor(col);
g2d.fill(poly);
g2d.setColor(Color.BLACK);
g2d.draw(poly);
}
Sadly java.awt.Polygon does not support Polygons with holes.
Use the Polygon as the basis for an Area (e.g. called polygonShape).
Create an Ellipse2D for the 'hole', then establish an Area for it (ellipseShape).
Use Area.subtract(Area) something like:
Area polygonWithHole = polygonShape.subtract(ellipseShape);
There are some ways to draw shapes or areas that are more complex than a simple polygon (another answer already mentioned Area).
Besides those, you could try to tessellate your final polygon. There are lots of algorithms to do this. For more complex shapes, the algorithms get a bit more complex as well. Basically, you're dividing your final shape into little polygons (usually triangles, but it also can be something else) and then draw those polygons.
You can take a look at your possibilities by searching for "Tessellation Algorithm", there are also some already implemented libraries for Java.
You can use java.awt.geom.Path2D to render a "compound shape" with a hole in it:
If you have java.awt.Shape objects defining the outside & inside edges of the shape, use append(shape, false) to add each shape.
If you have a set of path points for the outside edge and a set of path points for the inside edge, use lineTo() to add the first set of points - creating a closed loop by either ending with the same point you started with, or calling closePath() to automatically close the loop. Then use moveTo() to create a break before adding the inner set of points via more lineTo() calls.
In either case, you must create the path passing Path.WIND_NON_ZERO to the constructor - otherwise the hole won't be left unfilled.
See How to create shape with a hole? for a longer code example.
You could fill the polygon first, and then draw the holes on top, giving the illusion that it filled everything but the holes.

Thickness of OpenGL 2D Textures

What is the easiest way to give 2D texture in OpenGL (lwjgl) some kind of "Thickness". Of course i could get the border of the texture somehow and add Quads, orriented by the normal of the quad that the texture is drawn on, in the color of the adjacent texture pixel. But there has to be an easier way to do it.
Minecraft is using lwigl as well and there are the (new) 3D Items, that spin down on the ground and don't cause as much of a performance issue, as is if they were drawn of dozends of polygons. As well, when you hold an item in your hand, there is that kind of "stretched" Texture in depth, which also works with high resolution textures.
Does anyone know how that is done?
A 2D texture is always infinitely thin. If you want actual thickness (when you look edge onto it) you need geometry. In Minecraft things look blocky, because they've been modeled that way.
If you look at some angle and ignore the edges you can use parallax mapping to "fake" some depth in the texture. Or you can use a depth map and use a combination of tesselation shaders and vertex shaders to implement a displacement map, that generates geometry from the texture.

Detect lines and its curve hand drawn by Graphics2d in a java draw application

I'm making a music application that enables you to freedraw lines from specified colors and then a trackbar would pass on top of it and should detect the curves and color to output sound and modified pitch etc depending on the curves.
What I'm looking for is help on detecting the lines and curves. They are drawn in a Label as a BUfferedImage. The following is a screenshot:
The Black line I drew to represent the trackbar, but infact it would be a Rectangle drawn by drawRect or it could be a imagepanel, depends on the way you detect lines.
My question:
How could I detect the yellow, green, etc lines and its curve and handle that data? If I get that data I could easily modify gain and pitch for each assign sound for each color. Thanks!
You want to store the coordinates of the user's drawing motions at the very least. You could store these as points or convert them into representations of line segments and/or curves depending on your requirements. Of course, store the stroke, color and anything you want as part of this model.
To accomplish this, I think the path of least resistance would probably have you utilizing java.awt.Shape and everything in java.awt.geom. Specifically, I think you would want to represent each user-drawn element as one or more Path2D and/or Area objects in response to user drawing motions. Then, have your GUI render these models on your custom Component.
Path2D would store the movements as lines and curves accessible through a PathIterator. Given a trackbar represented by a Rectangle2D, you can use the various methods available for comparing and combining geometries--namely the Shape interface's intersects(Rectangle2D) here. If a geometry model intersects your trackbar, you could then iterate through the path components until you find the actual sub-segment that is intersecting. This gets you the local slope.

Categories