I'm new in libgdx and box2d and I would like to know if there's an easy way to put a polygon randomly inside the bounds of another polygon.
EDIT
I want something like this:
Where the black polygon could be positioned in any other zone inside the green polygon and never outside.
There is no possibility of easy placing one polygon inside another in Box2D. Solution is shown on the drawing below:
Yellow rectangles is the first body, green - second. Each yellow rectangle is separate fixture.
Related
I made a sketch with the behavior I'm trying to get.
Sketch
So I want to display a rectangle in the middle of the screen and a triangle that covers part of the rectangle. The problem is, when I use a Stackpanethe rectangle is centered but the triangle as well and I can't move it to the bottom right position. When I use a Groupit is not centered. Is there any way to get my intended behavior?
Set alignment of your triangle. Use static setAlignment(Node child, Pos value) method:
StackPane.setAlignment(triangle, Pos.BOTTOM_RIGHT)
Does anyone have any suggestions on the best way to handle triangle intersections with rectangles and circles in LibGDX? I see the Intersector class has methods for testing intersections with triangles and rays, but I'm not sure how I can directly apply that to rectangles or circles. I also see there's an isPointInTriangle method, which I could perhaps use to cycle through the points of the triangle and rectangle?
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html
Maybe I'm just missing an easy, already existing function within LibGDX? Any help would be very much appreciated.
Triangles and rectangles are both considered to be polygons so use libGDX Polygon class to describe your triangles and rectangles and then use Intersector class to intersect them.
For circles you can either:
Approximate it with polygon (say 10 points around in circle) and then use Intersector class.
Create your own custom circle to polygon collision checker.
A simple algorithm to check polygon and circle collision that comes to mind:
if center of circle is inside the polygon =>>> they overlap.
else if distance between circle center and any of the polygons vertices is less then circle radius =>>> they overlap.
else =>>> they dont overlap.
This is considering you don't need to know what exactly is the overlapping portion of these figures and you only want to know if they overlap. If you need to know the overlapping portion then approximating your circle with polygon is the only way to go.
I would like to draw rectangles on another rectangle as shown in the figure. So, when I move the rectangle1 or rectangle2, the other rectangles should also move with it. Is there a way to draw rectangles in this way?
Assuming that the outer rectangle represents a selection rectangle meant to select the enclosed rectangles, you can maintain a List<Rectangle> and update the position of each rectangle as the mouse is dragged. A complete example using List<Node> is cited here.
I'm working in a simple game for desktop and Android using LibGDX framework. The game spawn some balls that move from one side of the screen to the opposite one. I want to show white circles over a black background but I also want to draw some white texts. My problem with this is that I want to draw the intersection between balls and between balls and text in black. I want something like this:
Other of my problems is that I'm using ShapeRenderer to draw the circles and they are too pixelated.
What is the best way to render circles in libGDX (Ill have to render between 1 and 100). Is it possible to get the effect shown in the image?
I have a program containing five buttons:
i) Square ii) Rectangle iii) Circle iv) Triangle v) Clear
Clicking on any of the first four buttons will draw the respective shape on the drawing canvas using the following code:
The shapes can be dragged. They also should be resized. I was just trying to change the cursor of the mouse around the four edges of the shape (NW, NE, SW, SE). This works fine for squares and rectangles since they have four sides. However, I have some problem implementing the same for the circle and triangle.
This is what I did in my mouseClicked event:
What I want to do is that if the string shape_type contains circle, for example, it draws a border around it so that the user can see the boundaries of the shape.
However, I can't just use the following code inside the if statement:
How can I draw the border please? Thanks :)
You are correct that you can't do painting in a MouseListener method. What you can do is set shapeUnderMouse in your mouseClicked method, then call repaint. You'll get better performance if you only repaint the areas that you know are changing visually, but it's not strictly necessary.
The if (shapeUnderMouse != null) block should be placed in your paint method.