How to create a Rectangle Path when given start and end Rectangle - java

I'm checking for collisions between snooker balls using the Intersects call in the Rectangle class - each ball has a Rectangle which defines its hitbox.
How my code works is that I have rectangle representing the white ball's old position and a new one for its updated position. From here I need to check if there was a collision with another ball between the old position and the new position.
This is where I'm hoping for some assistance, what would be the best way to create a path / check if there's a possible collision between the old and new position?
Image representation

To test if two circles have collided, you can compare the distance between the center of both circles to the sum of the radiuses. If the distance is smaller, they have collided.
For Rectangles, you can use Rectangle.intersects(Rectangle)

Related

Collision Polygon and Circle - Lingdx

I want to detect when a polygon and a Circle collide. Other possibility that i have think is with rectangle, but it can't be possible because i want to rotate the rectangle, so what is the solution?
Thank you
There is Intersector class in LibGDX, it should work just fine for your needs.
libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html
Use intersectSegmentCircle method.
It does take Vector2 variables as arguments, so you have to use Vector2 for storing x and y coordinates of your points - circle center and polygon vertices.
There are also Polygon and Circle classes, you can use those for storing coordinates as Vector2.
Anyway, lets assume you already have circle and polygon described by set of Vector2 points:
Vector2 circleCenter, PolyVertex1, PolyVertex2, PolyVertex3, ..., PolyVertexN;
There are line segments between polygon vertices, you have to check, if any of those segments intersects with circle. So, for every pair of verices, check, if:
intersectSegmentCircle(PolyVertex1, PolyVertex2, circleCenter, radius^2);
intersectSegmentCircle(PolyVertex2, PolyVertex3, circleCenter, radius^2);
...
intersectSegmentCircle(PolyVertexN-1, PolyVertexN, circleCenter, radius^2);
intersectSegmentCircle(PolyVertexN, PolyVertex1), circleCenter, radius^2);
returns true. If yes, it means your polygon and circle colided.
In some cases (small circle and large polygon), there can be no intersection between polygon edges and circle, whole circle can be inside polygon. Then you will have to use isPointInPolygon() method. It takes Array of Vector2 (polygon vertices) and Vector2 (cirlce center) as arguments.
So, store polygon vertices in array, and check if isPointInPolygon returns true. If yes, then again, there is collision.
Array <Vector2> PolyVertices;
...
isPointInPolygon(PolyVertices,circleCenter);
Dont copy&paste above code, wont work because of lack of initialization and thelike ;) But i hope, idea is clear.

running on every pixel inside a circle

Let's say I have many small bitmaps and I draw a big circle around them but not necessary all bitmaps are inside of the circle (like some can be half way in or have their edges stick out) and I want to run on every single pixel of the bitmaps in the circle (meaning pixels that are outside of the circle wont be counted, only the parts that are inside), how do I go about doing that, I know how to run on every pixel of all the bitmap, but not in a specific shape..
You need to create an imaginary grid, or rather a grid that is only useful in that it will help you solve the problem at hand. This is the grid that you will assign all the bitmaps to a position on, imagining that the circle's center is to be located at (0,0).
You then use a little math
to find if a pixel as it is relative to its bitmap's position on the grid, is within the radius of the circle.
Of course the distance formuala is
Or if you rather it is the sqrt( a^2 + b^2 ). where 'a' is the difference in x and 'b' is the difference in y between 2 points.

Detect collisions between circles with rectangles inside

I'm working on a project, and I need to be able to detect collisions between circles. I already found a mathematical formula for that : http://cgp.wikidot.com/circle-to-circle-collision-detection
But I've got a question, how can I detect if there is a rectangle in this area ? Or just a part of a rectangle inside ?
I've got : coordinates of the center of a circle and the radius, and for the rectanble I've got a x and y coordinate, and width an height. I guess that x and y are just a point and with that I'm able to guess the form with the width and the height.
Any idea ?
Thanks a lot !
Write a method to check whether a point lies within a circle or not.
Call that method for all corner points of the rectangle (calculated from x, y, width and height) on both circles.
Use your existing circle intersection detector method to prune calls.
Hope this helps.
Good luck.
You can use java.awt.geom.Area class.
It has a constructor to create an Area from Shape
public Area(Shape s)
So create simple areas for your circles and rectangle. Then you can either combine areas using the methods to obtain new areas.
public void add(Area rhs)
public void subtract(Area rhs)
And check whether area intersects or contains another area via
public void intersect(Area rhs)
public boolean contains(Rectangle2D r)
This sounds like a variation of the technique described in this answer.
The same two cases apply (either the circle's centre is in the region, or one or more of the rectangle's edges intersects the region)... the difference is that instead of considering the circle in general, you need to consider the intersection of the circle.
The first case is easy because you can swap centre points. If the rectangle's centre point is in the intersection of the circles, then the rectangle is partly inside. This is easy to determine: find the centre point of the rectangle, see if it's in the first circle, see if it's in the second circle.
The second case is complicated, because it requires you to calculate the curves where the circles intersect. If the edges of the rectangle intersect either of those curves then the rectangle overlaps the intersection. As a special case, if one circle lies completely inside the other one, then the line to check is the border of the smaller circle.
If you don't need an exact answer, then the second case can be approximated. First, find the points where the two circles intersect (or use the method you've already come up with, if you can). These two points can be used to construct a bounding rectangle (they are either the top left/bottom right or top right/bottom left points of a rectangle). If this bounding rectangle intersects with your rectangle, then your rectangle probably overlaps the circle intersection.
All in all, this is fairly complicated if you want to an exact answer that works properly with all of the special cases (one circle completely inside the other, the rectangle intersects both circles but not their intersection, etc). I hope this helps a little.
A library I've used before called the JTS topology suite might be appropriate for your needs. It's orientated more towards GIS operations than pure euclidean geometry, but it can easily do all of these calculations for you once you've got the shapes defined:
import com.vividsolutions.jts.util.*
GeometricShapeFactory gsf = new GeometricShapeFactory();
gsf.setSize(100);
gsf.setNumPoints(100);
gsf.setBase(new Coordinate(100, 100));
//configure the circle as appropriate
Polygon circleA = gsf.createCircle();
//configure again and create a separate circle
Polygon circleB = gsf.createCircle();
//configure a rectangle this time
Polygon rectangle = gsf.createRectangle();
Geometry circleIntersection = circleA.intersection(circleB);
return rectangle.intersects(circleIntersection);

computing the intersection point of an ellipse and a line in Processing

I'm having trouble computing the intersection point of an ellipse and a line.
Let's say I have an ellipse at point (0/0) with radius 500. Additionaly I'm drawing a line from point (0/0) to (mouseX, mouseY).
First I check if the mouse coordinates are outside the ellipse by doing
if((mouseX*mouseX)+(mouseY*mouseY)) > 500*500){/*...*/}
Now, whenever the mouse coordinates are outside that ellipse, I want to draw the line not until the mouse, but until the 'border' of the ellipse. In order to do so, I must have the intersection point of the line and the ellipse.
Are there any libraries that simplify such trigonometric tasks? Is there any other more or less easy way to compute the intersection?
From what you've said, I'm making the following assumptions:
The ellipse is always circular (same radius all the way round).
The line is always being drawn from the centre of the circle.
If those are true then the problem is actually very simple. All you need to do is truncate the line so that its length is the same as the circle's radius, and that gives you the intersection point.
If the mouse is outside the ellipse:
Store the vector describing the line; in this case (mouseX, mouseY).
Normalize the vector (divide each component by the length of the line).
Multiply the vector by the radius of the circle.
The vector now contains the intersection point, relative to the centre of the circle.
You don't have to use a vector class for this, although it might help.
If your circle and line aren't starting on the origin (0,0) then you'll need to compensate. At step 1, subtract your new origin from (mouseX,mouseY). After step 3, add the origin back in to get the display coordinates.

How to rotate a single shape in 3d space in opengl?

so I'm pretty new with opengl and creating 3d shapes. So for my example I have two squares, one with a height/width 2 with the center at the origin coordinate (0,0,-10), and one that is to the far left side of the window. I am trying to rotate the square that lies in the origin along the x-z plane without rotating the square that is located to the far left side of the screen. My approach to this was to save each xyz coordinate of the center square to a variable, and creating a method that uses the behavior of cos(theta) to rotate the square along the x-z plane. My code works, but I assume this is a horrible approach as there must be some more efficient method that is already created that can do the same functionality. I looked at glRotatef(), but from what I understood this only rotates my camera view which in the end would rotate both the middle square and the far left square whereas I only want to rotate the middle square. Is there some other method that already exists that can easily rotate a single 2d shape in 3d space?
In case its relevant, I have included the rotating code I made myself for the middle square: (btw the blue class is just some class I made that has the squares coordinates and the circle degree for cos(theta))
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
blue.setCircle(blue.getCircle()+1f);//getCircle is initially zero and gets incremented by 1 for everytime the program loops with the user holding the left button.
blue.setXfrontTR((float)Math.cos(Math.toRadians(blue.getCircle())));//Changing top-right x coordinate of the middle square
blue.setZfrontTR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f)))); //Changing top-right z coordinate of the middle square.
blue.setXfrontTL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontTL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing top-left x,z coordinates
blue.setXfrontBL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontBL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing bottom-left x,z coordinates
blue.setXfrontBR((float)Math.cos(Math.toRadians(blue.getCircle())));
blue.setZfrontBR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f))));//Changing bottom-right x-z coordinates
}
If you give each object that requires independent movement a model-view matrix you can achieve this. The other option to quickly draw/move a few independent objects is to:
for each object:
pushMatrix()
draw object
popMatrix()
while in the modelview matrix...
The method of drawing depends greatly on the OpenGL version you're coding to but the above will work for simple drawing. I'm not an expert on OpenGL / 3D programming so, if you wait a bit you may hear(see) better wisdom than what I offer :)

Categories

Resources