Hello!
I’ve been working very hard at creating a 2D platform side-scroller where the player can shoot. Everything is going well, except I can’t figure out an answer to this one problem, and I’ve been tearing my brain apart for a week now.
The thing is: right now I have an offset for the shoulder of the player, and at the moment the gun is (quite by chance) almost the same Y-position as the shoulder, so my code below works. It basically calculates the angle of the line between the shoulder and the cursor, and adds the distance from the shoulder to the muzzle to get the spawn-point for the projectile. I thought I had made it so the muzzleoffset could be used to move the spawn-point of the bullets in 2D, but all it does is add the distance from the shoulder to the muzzle to the spawn-point. It doesn’t actually work as a 2D offset.
What I’d like, is to be able to move the spawn-point of the projectiles upwards or downwards or closer/farther from the player, because I’m going to have many different guns with varying muzzle-offsets. I’ve tried a good deal of things to move the spawn-point of the bullets, but I’m having a hard time making it “count” when I move it around in different angles. It’s easy enough to just add +2 to Y, but if it isn’t taken into account in the calculations, it obviously won’t work in all angles.
http://img23.imageshack.us/img23/6899/gungamedebugging.png
So, what can I do to offset the spawn-point of the projectiles by some simple X,Y values, so the offset will be taken into account when I rotate the arm?
int shoulderOffsetX = roundPlayerPosX, shoulderOffsetY = roundPlayerPosY-40;
// Calculate the radians for the angle the bullets should be rotated.
// Ignore PlayerRenderOffsets, as they are only important for the calculation, because it is the offset of
// where the player is rendered on the screen, and it just indicates a point to which everything is
// translated.
// I know, shoulderOffsetX is the same as roundedPlayerPosX, but the position of the shoulder might change,
// when I get some drawings from the artist.
double baseXcalc = control.getMc().getMouseX() - shoulderOffsetX + roundPlayerPosX - playerRenderOffsetX;
double baseYcalc = control.getMc().getMouseY() - shoulderOffsetY + roundPlayerPosY - playerRenderOffsetY;
double angleInRad = Math.atan2(baseYcalc, baseXcalc);
// The above works perfectly to find the correct angle between the shoulder and the mouse, but I would think
// I need to alter the calculations in some way to achieve what I've stated above, but since the guns all turn
// with the arm around the shoulder-point, I should be able to use this angle regardless, right?
// I don't know how I could've thought the method below would do what I wanted...it must've been late.
// I do understand what I've done, but I also see that it can never work the way I intended, as this just
// finds a distance between the two points and adds it to the projectile position. The +2 and +42 are the values
// I want the projectile offset by, so it would spawn 2 pixels lower than the shoulder, and 42 pixels away from it.
// Calculate the starting-positions of the bullets.
Point shoulder = new Point(shoulderOffsetX, shoulderOffsetY);
Point muzzleOffset = new Point(shoulder.x+42, shoulder.y+2);
// Find the distance from the shoulder to the muzzle;
double distanceFromShoulderToMuzzle = shoulder.distance(muzzleOffset);
// Calculate new start-positions for the bullets
double bulletX = Math.cos(angleInRad)*distanceFromShoulderToMuzzle + shoulderOffsetX;
double bulletY = Math.sin(angleInRad)*distanceFromShoulderToMuzzle + shoulderOffsetY;
// Calculate the angle the bullets should be traveling at; is also used to rotate the projectile images
double angle = (double) angleInRad*180/Math.PI;