How can I make the vertices of a polygon in libgdx?
I am trying to make a pentagon for collision detection and I am kind of confused about how the vertices part is done, until now I used rectangle to achieve this
You should make several polygons since a pentagon is a convex shape and not easy to compute if a point is within the shape.
To create a shape with the Polygon class you specify the vertices with a float array. In sequence, you specify the x and y positions. Since Polygon is a 2D shape in Libgdx the Z axis is not needed.
Below is a polygon for a triangle shape, create your pentagon like this from multiple polygons and use Intersector to test the polygon vs various other geometry like lines and points. Note, Intersector does contain method overlapConvexPolygon but I am pretty sure that gives true for something in between the points of your pentagon.
new Polygon(new Array[
0, 0,
10, 0,
5, 10
]);
Related
I'm writing a OpenGL application in which there is a rectangle placed on a 3D object. The 3D object can move around and rotate and the rectangle follows these movements.
What I would like to do is to point with the mouse towards the rectangle so that a dot would appear in that point on the rectangle, and I want the point to follow it as the 3D object that "holds" the rectangle moves around.
I know how to find the intersection on a plane, and I know how to find the world coordinates of the contact point. What I need is a way to convert the world coordinates to the 2D local coordinate system of the rectangle.
For example, suppose I have the plane positioned like this in the 3D world, with a given orientation (sorry, I can't really draw properly):
The black point in the center is the origin of the plane, while the blue point is the one I would like to find. The numbers near the points are their world coordinates; in this example the Z axis "comes out" of the screen.
I would like to map the coordinates of the blue point in the local coordinate system of the plane, like this:
I know that somehow this shouldn't be hard, but I can't find a way at all. Any hints would be appreciated!
You probably already use a transformation matrix to render the rectangle. If you have the 3D position of the point, just transform it with the inverse transformation matrix and you get a 3D position in the rectangle's space. If the local system is aligned with the rectangle, one of the coordinates should always be zero and you can drop it.
I found what I needed in this post here.
Basically the solution for me is to project the point onto the x and y axis of the local coordinate system of the rectangle.
In pseudo code it's like this:
// I compute the direction that go from the origin of the rectangle to its x axis, in world coordinates, and take the norm; same thing for the y axis
var xDir = Norm(mRectangle.LocalToWorld([1, 0, 0]) - mRectangle.LocalToWorld([0, 0, 0]));
var yDir = Norm(mRectangle.LocalToWorld([0, 1, 0]) - mRectangle.LocalToWorld([0, 0, 0]));
// I compute the dot product
mLocalPosition.x = Dot(xDir, (contactPoint - mRectangle.LocalToWorld([0, 0, 0])));
mLocalPosition.y = Dot(yDir, (contactPoint - mRectangle.LocalToWorld([0, 0, 0])));
// I can now set the position of the point converting the local coordinates found to world coordinates
mPoint.SetPosition(mRectangle.LocalToWorld([mLocalPosition.x, mLocalPosition.y, 0]));
I want to shade a polygon with lines which are drawn in a specific angle.
I already have found all points with a scanline algorithm of the polygon in an ArrayList of ArrayList of Points.
This means in the ArrayList<ArrayList<Point>> are ArrayList<Point> which have all Points for a Polygon with one y value. For example these 2 Points for a rectangle (2,2) and (5,2). So the outer ArrayList contains all the horizontal points which can be drawn with drawLine().
Now I have to calculate the end points x2 and y2 for drawLine(x1,y1,x2,y2).
Is there any possibility to get the right points of the polygon for x2 and y2?
Trying to achieve this manually is rather difficult.
Consider setting the clip path in Graphics2D using your polygon as a Shape. Then, from points on the base line of the bounding rectangle of your polygon, draw the shading lines long enough to exceed the parallel line of the bounding rectangle.
You can look on wikipedia for how to find line-line intersections.
The only difficulty is making sure that the intersection is actually on the right side of the line segment. One option is just to check that the coordinates of the intersection are in the boundary of the rectangle formed by the two endpoints of the polygon side. You can easily do this by just comparing the coordinates.
Once you have found the intersection for that line on both sides of the polygon, you can then use g.drawLine() to connect them.
The naive, and simple solution to finding the two sides of the polygon (as long as it's convex), is to loop over all of the sides until you find two that have the intersection in the bounds of the side. Then, just connect those two intersections.
I have an assignment to draw a certain number of circles using java.awt.Graphics.
Drawing the circles is fairly simple, but I am only supposed to draw the circle if it appears within the visible area. I know I can call method getClipBounds() to determine the drawing area, but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
Is that the right way to go about determining if the circle I want to draw will be completely visible or is there a simpler way?
Don't use the Graphics.fillOval(...) method to do the painting.
Instead you can use the Graphics2D.fill(Shape) method. You can create oval Shape objects using the Ellipse2D class.
but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
The Shape object has a method that will allow you to get the rectangular bounds of the Shape. Then you can use the Rectangle.contains(...) method of the your Graphics area to determine if the Shape is fully contained within your panel.
Check out Playing With Shapes for more information and ideas.
use Ellipse2D.Float to instanciate an object for example:
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
basically the parameters,from left to right, are: Height, Width, X of the Top left and Y of the top left, and by keeping the X and Y both greater or equal to zero, your circle will always be visible.
the parameters of the Float(...) are documented for the Ellipse2D.Float in Java SE 7 in
http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Ellipse2D.Float.html
Hello I am making a game in java. I am using a collection of lines to represent a shape to detect collisions. I need to be able to rotate this shape by degrees or radians
As you can see from the diagram above the shape is a collection of line segments with 2 points a and b. I need to know how would I rotate all of lines together and still retain the shape.
Sounds like a job for AffineTransform (assuming you're doing 2D)
Something to this effect:
Point2D rotatedPoints = new Point2D[yourPoints.length];
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(yourDegreeRotation), xToRotateAround, yToRotateAround);
at.transform(yourPoints, 0, rotatedPoints, 0, yourPoints.length);
I have trouble drawing a triangle with the draw(Graphics g) method in Java.
I can draw a rectangle like so:
public void draw(Graphics g) {
g.setColor(colorFill);
g.fillRect(p.x, p.y, width, height);
g.setColor(colorBorder);
g.drawRect(p.x, p.y, width, height);
drawHandles(g);
Where p represents "the top left corner of the shapes". How would I draw the triangle in the same manner?
Could someone give me an example for a standard triangle?
There is not a drawTriangle method neither in Graphics nor Graphics2D. You need to do it by yourself. You can draw three lines using the drawLine method or use one these methods:
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
drawPolygon(Polygon p)
drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
These methods work with polygons. You may change the prefix draw to fill when you want to fill the polygon defined by the point set. I inserted the documentation links. Take a look to learn how to use them.
There is the GeneralPath class too. It can be used with Graphics2D, which is capable to draw Shapes. Take a look:
http://docs.oracle.com/javase/tutorial/2d/geometry/arbitrary.html
You should try using the Shapes API.
Take a look at JPanel repaint from another class which is all about drawing triangles, look to the getPath method for some ideas
You should also read up on GeneralPath & Drawing Arbitrary Shapes.
This method is much easy to apply AffineTransformations to
Use a line algorithm to connect point A with point C, and in an outer loop, let point A wander towards point B with the same line algorithm and with the wandering coordinates, repeat drawing that line. You can probably also include a z delta with which is also incremented iteratively. For the line algorithm, just calculate two or three slopes for the delta change of each coordinate and set one slope to 1 after changing the two others proportionally so they are below 1. This is very important for drawing closed geometrical areas between connected mesh particles. Take a look at the Qt Elastic Nodes example and now imagine drawing triangles between the nodes after stretching this over a skeleton.
As long as it will remain online
there is no command directly to draw Triangle. For Drawing of triangle we have to use the concept of lines here.
i.e, g.drawLines(Coordinates of points)
There is no direct method to draw a triangle.
You can use drawPolygon() method for this.
It takes three parameters in the following form:
drawPolygon(int x[],int y[], int number_of_points);
To draw a triangle:
(Specify the x coordinates in array x and y coordinates in array y and number of points which will be equal to the elements of both the arrays.Like in triangle you will have 3 x coordinates and 3 y coordinates which means you have 3 points in total.)
Suppose you want to draw the triangle using the following points:(100,50),(70,100),(130,100)
Do the following inside public void paint(Graphics g):
int x[]={100,70,130};
int y[]={50,100,100};
g.drawPolygon(x,y,3);
Similarly you can draw any shape using as many points as you want.
You can use Processing library:
https://processing.org/reference/PGraphics.html
There is a method called triangle():
g.triangle(x1,y1,x2,y2,x3,y3)