Polygon Collision Detection!!!!

ok so i create my polygon right here

	Polygon poly;
	public void createDetectionRadius(Graphics2D g, int xpos, int ypos, double size, int width, int height){
		poly = new Polygon();
		poly.addPoint(xpos, ypos);
		poly.addPoint(xpos-(int)size*width, ypos+50+(int)size*height);
		poly.addPoint(xpos - width*3-(int)size*(int)size + height, ypos+(int)size*height*2-(int)size*2);
		poly.addPoint(xpos, ypos+(int)size*height*2);
		poly.addPoint(xpos + width*3+(int)size*(int)size - height, ypos+(int)size*height*2-(int)size*2);
		poly.addPoint(xpos+(int)size*width, ypos+50+(int)size*height);
		g.drawPolygon(poly);
	}

here is where i do the method

		if(Map.player.detectedPlayer){
			double dx = xpos - Map.player.xpos + 8 - Map.xOffset;
			double dy = ypos - Map.player.ypos - Map.yOffset;
			
			rotation = -Math.atan2(dx, dy);
			
			rotation = Math.toDegrees(rotation) + 180;
		}
		
//		g.rotate(rotation,
//				xpos - Map.xOffset + width / 2 , 
//				ypos - Map.yOffset + height / 3);

		g.rotate(Math.toRadians(rotation), 
				xpos + width / 2 - Map.xOffset, 
				ypos + height / 3 - Map.yOffset);
		//HERE!!!!!!!!!!!!!
		createDetectionRadius(g,
				(int) (xpos - Map.xOffset + width / 2)
				,(int) (ypos - Map.yOffset + height - height / 3), 5, 20, 20);
                //HERE!!!!!!!!!!!!!
		
		g.drawImage(pipe, (int) (xpos - Map.xOffset),(int) (ypos - Map.yOffset), width,height,null);
		
		if(Map.player.detectedPlayer){
			g.setColor(Color.RED);
		}else{			
			g.setColor(Color.WHITE);
		}
		
		g.drawLine(
				(int) (xpos + width / 2 - Map.xOffset),
				(int) (ypos - Map.yOffset), 
				
				(int) (xpos + width / 2 - Map.xOffset), 
				(int) (ypos + detectionSize+width*4+20 - Map.yOffset));
		
		g.rotate(-Math.toRadians(rotation), 
				xpos + width / 2 - Map.xOffset,
				ypos + height / 3 - Map.yOffset);

when the polygons are draw thay do rotate and all, its just that the collision does not work, the collision box does not rotate with the drawn polygons!!

		if(poly != null){
			if(poly.intersects(Map.player.getBounds())){
				Map.player.isDetected(true);
			}else{
				Map.player.isDetected(false);
			}
		}

the collision does not move or rotate so it wont work

tell me if you want better explanation on what all variables are!

here are 2 images of what is happening

http://s30.postimg.org/4cbq8hqzl/image.png

the red lines are where the collision is so the collision polygons are not rotating for some reason!

here is me standing the the box // thats why the colors changed!

http://s30.postimg.org/j9k79i481/image.png

so basically the collision works but the collision polygons wont rotate with the drawn polygons!

You have to rotate the polygon points when testing collision. when you rotate Graphics is only effects the render orientation.

^ This right here. :wink:

You’re basically saying “I want you to render a polygon with these points in this specific location, now I want you to take what you’re rendering and rotate the entire render job by N degrees” (because you’re rotating Graphics2D itself, not the Polygon), so what you’re doing is changing where the polygon is being rendered, but not actually moving it. What you’ll have to do is move the entire Polygon and not touch Graphics2D.rotate().

and how would i rotate that then?

Please stop putting tons of “!!!” into the titles of your topics.

this should do it:


float angle = Math.toRadians(degrees);
float newX = originX + (x - originX ) * Math.cos(angle) - (y - originY) * Math.sin(angle);
float newY = originY + (x - originX ) * Math.sin(angle) + (y - originY) * Math.cos(angle);

If you’re using Java2D might as well use AffineTransform if you please.

Ok i will!!!

  • sy xD