My brain hurts.
So here’s my progress: I’ve managed to draw what I want on the screen using a global AffineTransform (i named it identity in the code below) that holds the default transform so that the screen doesn’t go all out of whack, but the collisions that occur behind the scenes don’t match what I’ve drawn. For reference, another screenshot here where the red rectangle surrounding the bullet is what I want to check collisions with, while the blue rectangle is what collisions are actually checked with.
https://lh3.googleusercontent.com/-ERY0EqPmpS8/TsQfbJWyMVI/AAAAAAAAADU/7z3lBSEGWD8/s640/stickGameScreenShot2.png
Here’s some of the code for drawing what I want:
AffineTransform transform = new AffineTransform();
Polygon poly;
for(int i = bullets.size() - 1; i >= 0; i--){
if (bullets.get(i).getX() < 0 || bullets.get(i).getX() > screenWidth ||
bullets.get(i).getY() < -30 || bullets.get(i).getY() > screenHeight ||
!bullets.get(i).isAlive())
{
bullets.remove(i);
}else{
bullets.get(i).update(elapsedTime);
int[] xpoints = {(int)bullets.get(i).getX(), (int)bullets.get(i).getX() + 30, (int)bullets.get(i).getX() + 30, (int)bullets.get(i).getX()};
int[] ypoints = {(int)bullets.get(i).getY(), (int)bullets.get(i).getY(), (int)bullets.get(i).getY() + 16, (int)bullets.get(i).getY() + 16};
poly = new Polygon(xpoints, ypoints, 4);
transform.setToTranslation(bullets.get(i).getX(),
bullets.get(i).getY());
transform.rotate(bullets.get(i).theta());
g2d.setTransform(identity);
g2d.drawImage(bullets.get(i).getImage(), transform, null);
g2d.setColor(Color.RED);
g2d.rotate(bullets.get(i).theta(), bullets.get(i).getX(), bullets.get(i).getY());
g2d.draw(poly);
}
}
And here’s the code I’m using for collision detection:
public void checkCollisions(){
for(int i = 0; i < zombies.size(); i++){
//check collision with bullet
Rectangle zombie = new Rectangle((int)zombies.get(i).getX(), (int)zombies.get(i).getY(),
zombies.get(i).getWidth(), zombies.get(i).getHeight());
for(int j = 0; j < bullets.size(); j++){
Rectangle bullet = new Rectangle((int)bullets.get(j).getX(), (int)bullets.get(j).getY(),
bullets.get(j).getWidth(), bullets.get(j).getHeight());
if(bullets.get(j).isAlive() && zombie.intersects(bullet)){
bullets.get(j).setAlive(false);
zombies.get(i).setAlive(false);
}
}
if(zombie.intersects(new Rectangle((int)player.getX(), (int)player.getY(), player.getWidth(), player.getHeight()))){
//do something to the player
}
}
}
I’m not sure how to go forward because i’ve only transformed the image; i think the polygon needs to have those dimensions itself instead of being transformed.