I am writing my first Java game and so far:
I made a rectangle that can walk with WSAD, and it always encounters what the mouse points to. Also, if you click, it will fire into the bullets where your mouse pointed (and the bullets rotate in that direction). I also made enemies that followed you, and they turned to face your character. The problem I ran into is that the detected collision only detects the collision of objects (character, enemies and bullets) before they rotate (using .intersects ()). This means that some parts of their bodies overlap when drawing.
I look around and I have not found solutions that I understand or can apply to my situation. For now, I rotate my Graphics2D grid for each of the objects, so they actually do not rotate, but simply stretch. Is there a way I can rotate my shapes and then use something like .intersects ()?
Any help or suggestions are welcome.
Here is what I use to see if it will collide while moving along the x axis:
public boolean detectCollisionX(int id, double xMove, double rectXco, double rectYco, int width, int height)
{
boolean valid=true;
Rectangle enemyRectangleX=new Rectangle((int)(rectXco+xMove)-enemySpacing,(int)rectYco-enemySpacing,width+enemySpacing*2,height+enemySpacing*2);
if (rectXco+xMove<0 || rectXco+xMove>(areaWidth-width))
{
valid=false;
}
if(enemyNumber>0)
{
for (int x=0; x<=enemyNumber; x++)
{
if (x!=id)
{
if (enemyRectangleX.intersects(collisionObjects[x])==true)
{
valid=false;
}
}
}
}
return valid;
}
source
share