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
Related
Java LibGdx Box2d
I would like to make body borders invisible or at least different color. One body should work as sensor and thus not be visible on the screen. I could not find a valid answer for that in hours and I cannot believe there is no such option in this library so I assume I must have missed one liner somewhere in Docs.
I would like to make this square transparent/invisible or at least different color but still keep it to discover movement of these circles.
Looking over stackoverflow and googling it in docs.
Closest things I found:
Make invisible body line box2d libgdx
so maybe the only solution to this is setting some flags in render method or unique render methods for each body?
What I can see on your screenshot is Box2d's shapes for debugging.
They were not designed to have adjustable visuals and are here only to show you the precise shapes and positions of your Box2d's entities.
According to the wiki (https://libgdx.com/wiki/extensions/physics/box2d#debug-renderer):
Debug Renderer
The next thing we are going to do is setup our debug renderer. You generally will not use this in a released version of
your game, but for testing purposes we will set it up now like so: ...
You will need some other kind of renderer if you want to adjust the visuals. For example:
ShapeRenderer, for simple shapes (https://libgdx.com/wiki/graphics/opengl-utils/rendering-shapes)
SpriteBatch, for textured visuals of any kind (https://libgdx.com/wiki/graphics/2d/spritebatch-textureregions-and-sprites)
PolygonSpriteBatch / CpuSpriteBatch / any custom renderer you may find or create yourself
Retrieve the positions / shapes / sizes of your Box2d entities and use one of the more advanced renderers to draw shapes or textures in positions of your Box2d entities. Box2d is not designed for drawing stuff, it just simmulates the physics and gives you position / rotation of bodies and some other info about its world.
Just make the box the same color as the background. Adding color to lines Box2d (EdgeShape)
This post shows how to change the color
Let's say I have a triangular face in 3d space, and I have the 3d coordinates of each vertex of this triangle, and would also have other information about the triangle(angles, lengths of sides, etc.). In Java, if I have the viewing screen and its information, how can I draw that plane, without using libraries like LWJGL, to that image, assuming I can properly project, accounting for perspective, any 3d point to that 2d image.
Would the best course of action just be to run a loop that draws each point on the plain to a point on the image(i.e. setting the corresponding pixel), which will most likely set the same pixel multiple times? If I'd do this, what would be the best way to identify each point in an oblique triangle, or a triangle that doesn't line up nicely with the axes?
tl;dr: I have a triangular face in 3d space, a "camera" looking at the face, and an image in which I can set each pixel. Using no GL libraries, what's the best way to project and draw that face onto the image?
Projection :
won't detail as you seems to know it
Drawing a line
you can look at Bresenham algorithm if you wanna start with the basics
(hardwared in recent graphics card)
Filling
you can fill between left and right borders of the triangle while you use Bresenham on both (you could use a floodfill algorithm starting ... i don't know, maybe at the projection of the center of the triangle)
Your best bet is to check out the g.fillPolygon() function for Java. It allows you to draw polygons with as many sides as possible and theres also g.drawPolygon() if you don't want it solid. Then you can just do some simple maths for the points. Such as each point is basically it's x and y except if the polygon is further away the points move closer to the center of the polygon and if the polygon is closer they move further away from the center of the polygon.
A second idea could be using some sort of array to store pixels and then researching line drawing algorithms and drawing lines then putting all the line data in another array and using some sort of flood-fill. Then whilst it's in that array you could try and do some weird stuff to the pixels if you wanted textures or something.
I have a sprite which is ball. Let's say, it represents a glass ball.
I am rendering the graphics with SpriteBatch.
Is it possible in libgdx to have a breaking glass effect for the ball? Meaning, I want to split the sprite to different pieces with abnormal borders (not rectangular) and then draw them flying to different directions.
Use a PolygonSprite to represent the non-rectangular chunks of your sprite.
To generate the chunks, I suggest picking a random spot near the center of your sprite, and then creating several triangles from that point to the corners and 2 or 3 points on each side of the square sprite. You should be able to define a PolygonRegion for each shard, and use that to build PolygonSprite instances.
I haven't actually used the PolygonRegion API before (and it looks a bit obtuse), so you might want to check the examples.
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.
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.