How to detect collision between two sprites with random shapes with libgdx - java

I'm new to libgdx and to game development in general. I'm trying to make a 2D game with libgdx on Android. For now, for detecting collisions I was using rectangles and the "overlaps" method, it worked very well. I now want to add very specific objects into my game that are randomly generated and with various shapes like circles, stars etc..
Can you guys help me find a way to detect these kind of collisions ? I heard about Box2D, should I use that ?
Thank you very much for your help !

I believe you can use the overlaps method with any polygon. You pass it in as an array of the vertices of the object.

Related

Make Custom Shape JavaFX

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.

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.

Java 2d game not using a tiled map

I decided to make a 2d game in Java (using the slick2d library and MarteEngine) and I attempted to do it without a tiled map. It seems like my upcoming tasks are going to be very difficult without having a tiled map. Those tasks are pathing and collisions (between buildings and players/NPCs).
Is it going to be nearly impossible to easily implement a pathfinding and collisions system? I suppose I could always manually create a grid in the game but that might become kinda messy considering I'd have to move it since I have a functional camera.
Also, by collisions I mean units walking into each other and becoming a single unit. I guess that could be categorized under pathing but I have no solution to fixing them from overlapping.
Any ideas are appreciated!
A lot of games still divide the map into tiles even if it isn't a tile-based game.
The reason is that you can do collision detection by checking whether an object is overlapping any of the objects in its current tile or any of the neighbouring tiles. As long as your objects are no larger than the tiles, this collision detection scheme is guaranteed to work out all possible collisions.
EDIT
If you have exisiting graphics that are not tile-based, it is still worth using this kind of "virtual" grid for collision detection etc. You can mark specific grid squares as being wholly or partially "blocked" if you want to detect collisions with map features and suchlike.

Java shapes sharing borders

I'm writing a program that generates penrose tilings and I wanted to use Java's Graphics2D class. I was wondering if there was any way to check if 2 or more shapes share a border with each other. Should I just compare the slopes and base points or is there an alternate method?
Thanks!
I feel it is best to compare the coordinates with collision detection.
I found these tutorials, they may help you if you need it.
2d collision detection - http://zetcode.com/tutorials/javagamestutorial/collision/
3d collision detection - http://www.java-tips.org/other-api-tips/java3d/collision-detection-with-java3d-2-2.html
Hope this helps

Categories