Issues with bullet entry points for “shoulder mounted” guns
I'm making a SHMUP game that has a space ship. That space ship currently fires a main cannon from its center point. The sprite that represents the ship has a center based registration point. 0,0 is center of the ship. When I fire the main cannon i make a bullet and assign make its x & y coordinates match the avatar and add it to the display list. This works fine. I then made two new functions called fireLeftCannon, fireRightCannon. These create a bullet and add it to the display list but the x, y values are this.y + 15 and this.y +(-) 10. This creates a sort of triangle of bullet entry points. Similar to this: ▲ ▲ ▲ the game tick function will adjust the avatar's rotation to always point at the cursor. This is my aiming method. When I shoot straight up all 3 bullets fire up in the expected pattern. However when i rotate and face the right the entry points do not rotate. This is not an issue for the center point main cannon. My question is how do i use the current center position ( this.x, this.y ) and adjust them based on my current rotation to place a new bullet so that it is angled correctly. Thanks a lot in advance. Tyler EDIT OK i tried your solution and it didn't work. Here is my bullet move code: var pi:Number = Math.PI var _xSpeed:Number = Math.cos((_rotation - 90) * (pi/180) ); var _ySpeed:Number = Math.sin((_rotation - 90) * (pi / 180) ); this.x += (_xSpeed * _bulletSpeed ); this.y += (_ySpeed * _bulletSpeed ); And i tried adding your code to the left shoulder cannon: _bullet.x = this.x + Math.cos( StaticMath.ToRad(this.rotation) ) * ( this.x - 10 ) - Math.sin( StaticMath.ToRad(this.rotation)) * ( this.x - 10 ); _bullet.y = this.y + Math.sin( StaticMath.ToRad(this.rotation)) * ( this.y + 15 ) + Math.cos( StaticMath.ToRad(this.rotation)) * ( this.y + 15 ); This is placing the shots a good deal away from the ship and sometimes off screen. How am i messing up the translation code?
What you need to start with is, to be precise, the coordinates of your cannons in the ship's coordinate system (or “frame of reference”). This is like what you have now but starting from 0, not the ship's position, so they would be something like: (0, 0) -- center (10, 15) -- left shoulder (-10, 15) -- right shoulder Then what you need to do is transform those coordinates into the coordinate system of the world/scene; this is the same kind of thing your graphics library is doing to draw the sprite. In your particular case, the intervening transformations are world ←translation→ ship position ←rotation→ ship positioned and rotated So given that you have coordinates in the third frame (how the ship's sprite is drawn), you need to apply the rotation, and then apply the translation, at which point you're in the first frame. There are two approaches to this: one is matrix arithmetic, and the other is performing the transformations individually. For this case, it is simpler to skip the matrices unless you already have a matrix library handy already, in which case you should use it — calculate "ship's coordinate transformation matrix" once per frame and then use it for all bullets etc. I'll now explain doing it directly. The general method of applying a rotation to coordinates (in two dimensions) is this (where (x1,y1) is the original point and (x2,y2) is the new point): x2 = cos(angle)*x1 - sin(angle)*y1 y2 = sin(angle)*x1 + cos(angle)*y1 Whether this is a clockwise or counterclockwise rotation will depend on the “handedness” of your coordinate system; just try it both ways (+angle and -angle) until you have the right result. Don't forget to use the appropriate units (radians or degrees, but most likely radians) for your angles given the trig functions you have. Now, you need to apply the translation. I'll continue using the same names, so (x3,y3) is the rotated-and-translated point. (dx,dy) is what we're translating by. x3 = dx + x2 y3 = dy + x2 As you can see, that's very simple; you could easily combine it with the rotation formulas. I have described transformations in general. In the particular case of the ship bullets, it works out to this in particular: bulletX = shipPosX + cos(shipAngle)*gunX - sin(shipAngle)*gunY bulletY = shipPosY + sin(shipAngle)*gunX + cos(shipAngle)*gunY If your bullets are turning the wrong direction, negate the angle. If you want to establish a direction-dependent initial velocity for your bullets (e.g. always-firing-forward guns) then you just apply the rotation but not the translation to the velocity (gunVelX, gunVelY). bulletVelX = cos(shipAngle)*gunVelX - sin(shipAngle)*gunVelY bulletVelY = sin(shipAngle)*gunVelX + cos(shipAngle)*gunVelY If you were to use vector and matrix math, you would be doing all the same calculations as here, but they would be bundled up in single objects rather than pairs of x's and y's and four trig functions. It can greatly simplify your code: shipTransform = translate(shipX, shipY)*rotate(shipAngle) bulletPos = shipTransform*gunPos I've given the explicit formulas because knowing how the bare arithmetic works is useful to the conceptual understanding. Response to edit: In the code you edited into your question, you are adding what I assume is the ship position into the coordinates you multiply by sin/cos. Don't do that — just multiply the offset of the gun position from the ship center by sin/cos and only then add that to the ship position. Also, you are using x x; y y on the two lines, where you should be using x y; x y. Here is your code edited to fix those two things: _bullet.x = this.x + Math.cos( StaticMath.ToRad(this.rotation)) * (-10) - Math.sin( StaticMath.ToRad(this.rotation)) * (+15); _bullet.y = this.y + Math.sin( StaticMath.ToRad(this.rotation)) * (-10) + Math.cos( StaticMath.ToRad(this.rotation)) * (+15); This is the code for a gun at offset (-10, 15).
Inverse-Kinematics: How to calculate angles for servos of a robotic arm to reach all possible points in a canvas?
How to calculate Heading using Gyro and magnetometer
Integral of a sign function proof
Best way working with trigonometric functions
Calculate place of point C to be equal distance away from points A and B
How to compute sum of angles from sine and cossine?
Counting trig ops as measure() function to fu() in sympy
Find a coordinate along a path
Angle calculation resulting in strange behavior
Shift angle orientation using atan2
estimate angle between two lines y = 1000x and y = 999x
Need help revising a simple formula to make it universal
SWIFT trig functions
2 Different Radians constant calcuations whats the difference?
Trigonometry & Sunset