Attaching a actor + rotation

Gashking

  • Posts: 111
hey guys.. having trouble figuring this one out..

My games top down 2d space focused, What im trying to do is with my ship i can attach turrets/lasers or w/e onto the ship and as the ship moves around it stays in a certain position ontop of it..

I've got the positioning and attachment sorted.. the problem is when i turn/rotate the ship the 'turret' stays in the same position... how would i update it so it follows the rotation also?

thanks for your help!

froz

  • Posts: 250
You need to calculate turret positions based on the ship direction. Actually it's not that hard, but you need to use some trigonometry.

How? You can calculate a point in the circle with a simple formula. x position of the point = cos (angle to the point) * radius (distance to the point). y position of the point = sin (same angle) * radius (same distance).

So, you need to know angle to the point and distance to the point. Distance should be the same as the original distance and you can calculate original distance by using a2 + b2 = c2. Meaning - distance on x axis2 + distance on y axis 2 = real distance. In stencyl it looks like this:
sqrt[x_distance^2 + y_distance^2]

How to calculate the angle? You need to calculate original angle then add ship's direction to it. To find original angle use atan2 block (atan2[y_distance, x_distance]). When you add direction, remember to transform direction to radians (use block "as radians"), because sin/cos blocks takes radians, not degrees and also atan2 block gives radians, not degrees, however direction block gives degrees, not radians.

x_distance and y_ distance should be x and y distance from the ship center to the turret center. All those calculations will give you x_center and y_center of the turret, so you need to diminish half-width to x and half_height to y to get x/y position.

Gashking

  • Posts: 111
you're a legend! thanks for such a great and informative response! unfortunately i'm hopeless at trigonometry... but ill give it a try soon..

thanks again!