Libgdx, collision, 2 rectangles, rotation, noob - java

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.

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.

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

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.

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 + Box2D | How to make a joint between 2 bodies stiff? / How to make objects fall faster?

I use LibGDX as well as Box2D for my platformer.
I got a prototype working before but I had problems with slopes, because I just applya force to the center of the player to the left or the right. So I thought why not let Box2D do the work, since I am already using it. I tried to implement this:
http://www.badlogicgames.com/wordpress/?p=2017 (this video shows the idea pretty good)
The Basic idea is to make the player not just out of a box. But instead use a circle shape for the feet. This way slopes shouldn't be such a pain the make. So I created 2 bodies. Joint them to test it and I found something odd. Both bodies together took way more impuls to let the player body jump than it took a box with the same width, height.
Example for equal jumping of the box and the player:
if (GameKeys.isPressed(GameKeys.SPACE)) {
if (cl.isPlayerOnGround()) {
playerBody.applyLinearImpulse(new Vector2(0, 1800), playerBody.getWorldCenter(), true);playerBody.getWorldCenter(), true);
box.applyLinearImpulse(new Vector2(0, 10), playerBody.getWorldCenter(), true);
}
}
The reason for that is that I applied a high desity to the player body to prevent the joint from bouncing and make it more stiff.
My questions are:
Is there a better way to prevent the joint between the feets and the body of the player to be very bouncy than to just set it's density very high?
Is there any commonly used formula to get a good density, or is it just tweaking the values until it feels right?
Is there a way to let an object fall faster besides messing with the fall constant (-9.81f)? The player object and the box feel a bit to floaty at the moment.
I know these are multiple questions and I hope I don't ask for too much, but thanks to anyone who even reads all this mess ^^
The Pixels per Meter (PPM) is 32!
My Play.java:
http://pastebin.com/HB9h6MV6
P.s.: Sry for my bad english. I'm not a native English speaker (German). :)

Libgdx rotate ModelInstance

I am developing my first libgdx 3D game. Until now i can move arround in a maze-like (hardcoded) world, collision detection works. Also i have some enemies with working A* Pathfinding.
I also loded my first (pretty ugly) Blender model, using FBX-Conv to get a .g3db file. For some reason the model lise on the floor instead of standing. Maybe i had some wrong settings when i exported it as .fbx.
For that i tryed to rotate() him arround the z-Axis by 90 degrees by calling:
modelInstance.transform.rotate(Vector3.Z, 90) in the show() method of my Screen, after loading the Model and instantiating my ModelInstance (at a given position). For some reason it did not rotate. Then i put the rotate method in the render(delta), thinking, that it would now rotate 90 degrees every render loop. But instead it was standing still, like it should.
Okay, but now i want the modelInstance to rotate to where it actually looks, meaning it should rotate, depending on my enemies Vector3 direction.
I am allready setting his position with modelInstance.transform.setTotranslation(enemie.getPosition()) which works perfect. So i thought i can also use modelInstance.transform.setToRotation(Vector3 v1, Vector3 vs), with v1 = enemie.getPosition() and v2 = enemie.getPosition().add(enemie.getDirection). Note, that the position Vector is not used directly, as it would change its values inside the add() method.
Doing this, i don't see the object anymore, meaning also its position is wrong.
Why is this happening?
And how can i rotate my modelInstance by using the direction vector?
Thanks a lot.
I solved this with #Xoppas help. The problem was:
i used setToTranslation to move my Model to a given position, but this als resets the rotation
I missunderstood the setToRotation(Vector3, Vector3) method.
So the solution was to to the setToTranslation first, and then use setToRotation(Vector3 direction, Vector3 face), where direction is the direction, in which my Model is looking and face is the face, which should look in this direction, in my case the Vector3.X.
Hope it helps someone else.
Worse case scenario, you could modify the transformation matrix using:
ModelInstance.transform.rotate()

Categories