Firstly, I had posted this in the Java2D section, but came to think it was more of a n00b-question than a Java2D issue.
I made this little video to better explain what I’m dealing with.
TXrJjKuAQHI
I want the projectiles to spawn at the end of the gun-barrel while the arm is waving and flailing about. Each gun has a different muzzle-offset. How would I go about figuring out the new muzzle-offset for the projectiles after I rotate the arms? Can I somehow rotate a point using the same transform-matrix as I do for the arm (which is calculated every update() so far; I will eventually change that, so it doesn’t run unnecessarily)?
I’ve been pulling hair out of my already balding head for days pondering over this. I mean, I’ve tried circle-math and stuff, but I’m just too confused about which points are important in this endeavour, and which steps to take. I finally got the arms to rotate properly, though.
Note: This is using nothing but Java2D. I know… For my next project I’ll use LWJGL
Here’s some code:
Method in Main-class for shooting projectiles (yeah yeah, it’s a temporary placement):
public void shootProjectile() {
// 35 and 40 are the currently fixed muzzle-offsets, from player-position (at his feet, in the middle)
double muzzleX = player.getPosX()+35, muzzleY = player.getPosY()-40;
double angleInRad = Math.atan2(mouseController.getMouseY()-(player.getPosY()-40), mouseController.getMouseX()-(player.getPosX()+35));
double angle = (double) angleInRad*180/Math.PI;
playerProjectiles.add(creator.spawnProjectile("9mm", muzzleX, muzzleY, angle));
}
Creator-class code for spawning projectiles into the world:
public Projectile spawnProjectile(String projectileName, double originX, double originY, double angle){
// clone the projectile from HashMap, using ID
Projectile p = Game.projectileMap.get(projectileName).cloneProjectile();
// clone Animation from HashMap, using ID in Projectile
Animation a = Game.projectileAnimations.get(projectileName).cloneAnimation();
// set Animation on new Projectile object
p.setAnim(a);
// set init position and angle
p.setInitX(originX-(a.getImage().getWidth()/2));
p.setInitY(originY-(a.getImage().getHeight()/2));
p.setPosX(originX-(a.getImage().getWidth()/2));
p.setPosY(originY-(a.getImage().getHeight()/2));
p.setAngle(angle);
p.setTimeLived(0);
// create the collison rectangle
p.createMainCollisionRectangle();
// return Projectile
return p;
}
Code for rendering projectiles:
for (int i = 0; i < playerProjectiles.size(); i++) {
AffineTransform transform = new AffineTransform();
Projectile p = playerProjectiles.get(i);
Point center = p.getImageCenter();
// translate and rotate the sprite
transform.setToTranslation(p.getPosX(), p.getPosY());
transform.rotate(p.getAngle() * Math.PI / 180.0, center.x, center.y);
// draw it
g.drawImage(playerProjectiles.get(i).getAnim().getImage(), transform, null);
}
Rotating the arm:
// Transform the arm, using the x,y offsets for the initial placement of the image.
armTransform.setToTranslation(player.getPosX()+player.getArmOffsetX(), player.getPosY()-player.getArmOffsetY());
// Factoring in the shoulder-offset of the arm-image (4 x, -10 y)
armTransform.rotate(Math.atan2(mouseController.getMouseY()-(player.getPosY()-player.getArmOffsetY()+10), mouseController.getMouseX()-(player.getPosX()+4+player.getArmOffsetX())), 4, 10);