Make Custom Shape JavaFX - java

I am making a 2D game in JavaFX and when detecting collisions, I am getting rather inaccurate results due to the player sprite being set as the fill of a rectangle and therefore not having the intended borders. Is there a way I could make my own shape so thatI could get as accurate as possible?
Another idea I had is checking if the pixel that collided was transparent and then not ending the game if it was. Does anyone know of a way I can get the coordinates of the pixel that collides so that from there I can use PixelReader to check?
If anyone knows a better way, please let me know!
Thanks,
Ethan

There are different ways to do this. Here is one way I have used with good success. I would make hit boxes, that were themselves rectangles. Then during collision detection, I would iterate through all the hit boxes to see if they collided with the flying projectile's hit boxes.
What this allows you to do is fill in complex shapes with smaller rectangles. For example a plane would have one long horizontal rectangle and one smaller rectangle crossing at the middle.
Currently I am using libGDX. In libGDX I use their Polygon object as stated here. https://stackoverflow.com/a/28540488/1490322 I have not seen similar functionality in JavaFX, but it would not be hard to copy what libGDX is doing into JavaFX code... their code is open sourced.

Related

How to define highlight-able, clickable custom objects in libgdx (Risk game)

I am making a game similar to Risk and struggling to find a way to implement the interaction with countries.
The basic idea is to create custom objects that are not rectangular and be able to change their colour by clicking them, highlight them with mouseover, or as the game progresses.
How would I go about having highlight-able countries that can be selected? The problem with sprites is their bounding boxes are rectangular, and if I define Box2D vertices and make polygons it gets really messy. Also, there are a lot of countries so a lot of the platformer style solutions don't fit.
How should I also change the colours of what is selected? Would it be best to have an individual sprite for every country and keep switching between them or is there a better way?
One way is to use polygons like you tried but I wonder why and what you mean it got messy. There are tools out there that let you draw vertices over a image and let you export that. You probably need to clean up the data a bit and import it into your app. It's also not very hard to make such an app yourself, have it import your image and start drawing and export to your favorite format. The more detailed you draw your polygons the more detail you get in your.
Perhaps an easier solution would be to use the opacity of each image of a country. Each country gets it's own image and you need to overlap the bounding rectangles to line them all up. When your mouse is hovering over one or more of these bounding boxes you check if the mouse is over a transparent pixel. If it is transparent you are obviously not hovering over the actual country. Some things to consider:
I would create the game in a pixel perfect manner so each pixel of your images is translated to a single pixel of the screen your outputting to.
To align your whole map I would create one big world map in your drawing application. Then save each country but remain the canvas size of the complete map. When packing these images with the LibGDX TexturePacker remove the whitespace (transparent pixels) and you will get an offset in your atlas. You can use this offset for each country to line them up and save precious texture space by removing all that whitespace.
Always check for a simple collision first before diving in deeper.
If you want to have "hover" functionality then don't do pixmap = texture.getTextureData().consumePixmap() each update since it's rather expensive. You might be better off creating your own 2D boolean array that represents the clickable area when you initialize the country object.

Java 2D Graphics

I want to develop a java graphic application. The concept is that there will be a circle in the middle and several balls will rotate around the circle. Is it possible to make the circle stable forever and only change the position of balls? If it is, how? Also, Is it possible to get a circle or ball like a normal java object?
When I use repaint() method, all of the drawings are repeated. But I do not want to change the location of circle, I only want to change the position of ball that I need.
Thanks.
It is not a game, but you can use a loop of update-render-sleep.
https://www.google.pt/#q=java+game+loop
And as for the fact tht you want to mantain the balls, use math:
Where will the center ball be?
How much will the radius of the center ball measure?
And the others balls? How will they rotate? What is their radius? How many are they?
etc..
Answer this and many aothers to yourself, and make a bit of studies in internet.
I hope I could help you.

Libgdx, collision, 2 rectangles, rotation, noob

Here is my problem,
I have 2 rectangles.
I want to detect collision between these 2 rectangles.
However one rectangle should be able to rotate around a given position(player midpoint,not constant).
My Problem is, that i don't know how to rotate this one Rectangle.
I would be grateful for any help.
Here is a sketch of my problem:
for a simple collision detection I had always rectangles:
playerrect = new Rectangle(playerposition.x,playerposition.y,playersizeX,playersizeY);
enemyrect = new Rectangle(enemyposition.x,enemyposition.y,enemysizeX,enemysizeY);
and this;
if(playerrect.overlaps(enemyrect)){.....}
and this was enough for me.
This time this noob needs the playerrect at various angles, like 5°,10°,15°.....
So I need something like
playerrect.setRotation
which is not available :).
Libgdx Rectangle can't do that unfortunately. If you want that kind of collision detection, the easier way would be using Box2d.

Building a drag and drop puzzle-type interface with snap alignment

I'm building an Android puzzle game where the user rotates and shifts pieces of a puzzle to form a final picture. It's a bit like a sliding block puzzle but the shape and size of pieces is not uniform - more like a sliding block version of tetris.
At the moment I've got puzzle pieces as imageViews which can be selected and moved around a view to position them. I've got the vector forms of the shapes behind the scenes as ArrayLists of Points.
But...I'm stuck on how to snap align the pieces together. I.e. when a piece is nearby another, shift one piece so that the nearby edges overlay each other (i.e. essentially share a boundary).
I'm sure this has been done plenty of times but can't find examples with code (in any language). It's similar to snapping to a grid but not the same and is the same kind of functionality you get in a diagramming type interface when you can snap objects to each other.
Can anyone point me toward a tutorial (any langauge) / code / or advise on how to implement it?
Urs is like Tangram game. I think it cannot be done with pieces of image to form a final picture. It can be done by Creating Geometry shapes(for both Final shape and pieces/slices of final picture) using android.Graphics package. Its quite easy to determine the final shape from the edges and vertices of pieces/slices.
http://code.google.com/p/photogaffe/ is worth checking out. It is an opensource sliding puzzle consisting of 15 pieces and allows the user to choose an image from their gallery.
You would only have to figure out your various shapes and how to rotate them. And if you are supplying your own images...how to load them.
Hope that helps.
What about drawing a box around each shape. Afterwards you define the middle of it. Then you can store a value for the rotation for each piece. And you would need to store the neighbours together with a vector the their middle.
Then you simply have to compute that the vector is in a reasonable range and the rotation is +-X degree. For example if the vector is in a range of +-10pixels and the rotation is +-3° you could rotate the piece and fit it into the puzzle.

converting a 2d window point to a 3d point

i am writing a 3d modeler similar to Blender for a game i am making. Since programs like blender export very complicated file types with alot of unneeded data i wanted to write a simple editor for my game. what i cannot figure out is how to map a point from a 2d projection on the window to where i have clicked in the 3d world with the world being rotated.
If anyone knows any good tutorials on how to do this or the method any help would be appreciated. I know i could use ray tracing but that would be to complicated i think.
The two main methods of mouse picking are:
Intersection Testing
Color Picking
Intersection tests are the more popular of the two, and at the simplest level involves 'shooting' out a ray and checking if it has intersected any points. The ray can also be replaced by a polytope if one wants to achieve more sensitive picking (useful for choosing points on vertices).
Color picking involves disabling AA, blending, shadows, etc. and re-drawing the scene using solid colors for the objects. glReadPixels is then used to find the color at the point of the mouse and this color is used to determine if it clicked on an applicable object.
Ray Picking:
Mouse Ray Picking Explained
Picking, Alpha Blending, Alpha Testing, Sorting
Color Picking:
OpenGL Selection Using Unique Color IDs
Picking Tutorial
The term you are looking for is mouse picking.
The method you need is gluUnProject. You'll need window x,y and the depth.
I think, in your case, it might be a lot easier to write a simple exporter for Blender.

Categories