I want to collide a rectangle with a semi-circle that comes rotating. A rectangular hitbox for the semi-circle, of course, is not appropriate as the side of the rectangular hitbox on the round side of the semi-circle will touch other targets before the semi-circle touches the targets. I am not inclined to use pixel collision detection as the two images are large.
I am thinking of using stripes of narrow hitbox rectangles of varying heights that approximately cover the contours of the semi-circle. How well would this work?
Not sure how you are doing collision detection, so I will just assume parametric equations and you can easily test for collisions between basic shapes.
If you think of a semi-circle as a circle missing a triangular element, then this is easy.
if (collide(rect, circle) && !collide(rect, triangle)) {
// then collision semi-circle
}
Use an isosceles triangle. The tip of the triangle is the center of the circle. The height of the triangle is the radius of the circle. The angle is based on what is missing from the circle.
Related
I need to fill the color inside custom shape. I know one of the pixel coordinate inside the shape and also know the color of the shape. I need to get all pixel inside the shape calculate from knowing pixel coordinate how can i write logic for this can some one please help.
For already rendered shapes
use Flood Fill or Boundary fill algorithms. For example see:
my C++ implementation of Flood&Boundary fill
beware most implementations are recursive so there is a high risk of stack/heap overflow for bigger areas. There are also iterative approaches using dynamic point lists which are safer.
For polygons in vector form
Use convex polygon filling. In case you got concave polygons you need to divide them into convex polygons first. Another options is to triangulate. Both triangle and convex polygon rasterization is the same see:
Algorithm to fill triangle
how to rasterize rotated rectangle (in 2d by setpixel)
If you need triangulation see:
Wiki: Polygon triangulation
In case of very complicated shapes and not possible triangulation for any reason you can try fill all the pixels in the bounding box of polygon that are inside polygon. For that you need:
hit-test
I'm making an Android game and I'm having some problems with bitmap collision detection. The problem is that one of the bitmaps is a triangle and the second one is a rectangle. I don't know how to detect collision between them because now I use this code:
if(policeY<((canvas.getHeight()/20)+eye.getHeight()) && (policeY+police.getHeight())>(canvas.getHeight()/20)){
if((policeX+police.getWidth())>triangleLeft && policeX<(triangleLeft+eye.getWidth())){
//collision
play = false;
}else if((policeX+police.getWidth())<triangleLeft && policeX>(triangleLeft+eye.getWidth())){
//collision
play = false;
}
}
However, this code handles both of them as rectangles and this causes collision even when triangle isn't even touching rectangle.
Like in this picture the circled area is considered as a collision
I'd set up the problem as an intersection problem. Define the edges of the triangles as lines then use the line-line intersection formula with the edges of the rectangle. Then check whether this point is on the edge of the rectangle or if it is outside the rectangle.
I'm creating some rectangles surrounding my sprites for my player and bullet class to detect collisions with the overlaps method in Intersector class of LibGDX.
I have a question:
When I instantiate Player and Bullet, I create a bounding box around the sprite using sprite.getBoundingRectangle() which returns a Rectangle object. I update the movement of these somewhere else in the main class.
When I update the movement of the bullet/player sprite, do I also need to update the position of the bounding box surrounding the bullet/player sprite? Or since the bounding Rectangle surrounds the sprite, will the box automatically move with the sprite?
Thanks
As per getBoundingRectangle javadoc:
Returns the bounding axis aligned Rectangle that bounds this sprite. The rectangles x and y coordinates describe its bottom left corner. If you change the position or size of the sprite, you have to fetch the triangle again for it to be recomputed.
Indeed, if you open Sprite source code, you'll see that the bounding rectangle is updated only when getBoundRectangle is called.
I've implemented a kind of Pong where the paddles (Rectangle2D) can rotate.
To obtain more accuracy, many things are managed by Graphics2D.
The rotation too is managed by the methods rotate(...) of the previous told class.
To reach a realistic bounce, I need to know where the ball hits the paddle (only the side, not the particular point).
I've tried to define (and rotate) two Rectangle2D that represent the back and the front side of the paddle and then recognize the bounce in one of these two by the method hit(Rectangle2D r, Shape s, boolean onStroke), but it doesn't work properly.
Here is the java class Graphics2D:
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
Have you any idea?
Assuming you have one rectangle for your paddle and know the center of rectangle and circle and the rotation of your rectangle.
Assuming a rotation of zero means, your rectangle is aligned horizontally (width > height).
Calculate the difference vector (center of circle) - (center of rectangle)
Get the angle of that vector and subtract the rotation of your rectangle (angle of vector is Math.atan2(y, x))
The resulting value a tells you the relative direction of your circle
Make sure a lies between 0 and 2*pi
q = Math.atan2(height of rectangle, width of rectangle)
If a is between q and pi-q, your circle has hit on the front long (upper) side.
If a is lower than q or bigger than 2*pi-q it's on the right side.
If a is between pi-q and pi+q it's on the left side.
If a is between pi+q and 2pi-q it's hit the bottom of your rectangle
I am currently working on a simple space game for android. My collision detection will be done using rectangular and circular bounding boxes. These bounding boxes need to be able to rotate, so my question is: what is the best to detect a collision between a rotated rectangle and a circle? Thanks for your help :)
Ok, I have solved my own problem! There are only two cases when a circle intersects a rectangle:
1. The center of the circle is inside the rectangle
2. The circle is intersecting one of the sides of the rectangle
So to check for the collision, I first check if the center of the circle is inside the rectangle, after rotating the center of the circle according to the rotation of the rectangle, to simplify my calculations. If the center of the circle is inside the rectangle, I know there is an intersection, and return true.
If the first check returns false, then I check for intersections between each side of the rectangle, and the circle. If there is an intersection I return true. Feel free to comment if anyone wants the code, thanks for the help guys! :)
Generally, bounding boxes just define the bounds of an object, (a shape generated by the vertices of an object's max & min's X & Y) - this makes it much simpler to calculate, bounding boxes do not need to rotate as their purpose is met simply as I have explained. If you want to use them for collision detection simply check if the center of the circle plus its radius intersects the rectangle in both axis such as:
public boolean boxintersectscircle(BoundingBox box, BoundingCircle circle) {
if (box.x > circle.centerx+circle.radius) return false;
if (box.y > circle.centery+circle.radius) return false;
if (box.x+box.width < circle.centerx-circle.radius) return false;
if (box.y+box.height < circle.centery-circle.radius) return false;
return true;
}
However bounding boxes aren't accurate - they can leave a lot of unoccupied space so if that's a worry for your game (player frustration), Personally I would implement the separating axis theorem or line/circle detection.