So in my game I am detecting the future position of a mob to shoot at it. I have all the maths worked out, but I have reached a problem. I need to find the theta and I am not sure which angle I should calculate… here’s the math I have so far:
if (inGame) {
if ((xBullet >= targetX - 4 && xBullet <= targetX + 4) && (yBullet <= targetY + 4 && yBullet >= targetY - 4)) {
inGame = false;
shotMob.freeze();
xBullet = 900;
yBullet = 900;
}
int rotation = 0;
Graphics2D g2d = (Graphics2D) g.create();
if (rotation >= 360) {
rotation = 0;
} else {
rotation++;
}
targetX = shotMob.x + shotMob.width / 6;
targetY = shotMob.y + shotMob.height / 6;
int theta = ??????;
distance = Math.sqrt(Math.pow(targetX - xBullet, 2) + Math.pow(targetY - yBullet, 2));
time = distance / shotMob.MOVESPEED;
newPos = new Point((int) (targetX + (shotMob.MOVESPEED * time) * Math.cos(theta)), (int) (yBullet + (shotMob.MOVESPEED * time * Math.sin(theta))));
distanceX = (int) ((double) newPos.x - xBullet);
distanceY = (int) ((double) newPos.y - yBullet);
sep = Math.sqrt((distanceX * distanceX) + (distanceY * distanceY));
scale = 3;
xBullet += (distanceX / sep) * scale;
yBullet += (distanceY / sep) * scale;
Screen.room.block[y][x].drawImage = false;
g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(rotation), xBullet + Screen.room.blockSize / 2, yBullet + Screen.room.blockSize / 2));
g2d.drawImage(Screen.tileset_air[Value.missile], (int) xBullet, (int) yBullet, Screen.room.blockSize, Screen.room.blockSize, null);
g2d.dispose();
}
any help is appreciated -cMp