1
\$\begingroup\$

I'm having a little conundrum that may just result from a lack of knowledge of Gamemaker's functionality. I've attached two images to aid explanation.

enter image description here

enter image description here

I have a sprite of a turret (with a gun barrel), attached to a turret object, and at certain points in gameplay this object will spawn another object on top of it (let's call it the 'bullet object').

I would like the bullet object to spawn at the x and y coordinates at the end of the gun barrel. This would be easy to find as a pair of coordinates if the sprite was always stationary, in this configuration. Alas, it is not. It rotates like billy-oh. This means that the x and y coordinates at the end of the gun barrel are constantly different.

How do I find this constantly changing x and y coordinate? I imagine (though am most likely wrong) that there the initial x and y coordinates of the sprite are saved and can be found even if rotated; is there a function that does this? Or do I need to write a script and then call it every time I want to spawn the bullet object?

Thanks for your help.

\$\endgroup\$
  • \$\begingroup\$ stackoverflow.com/a/26311662/1821428 \$\endgroup\$ – Dmi7ry Oct 29 '15 at 20:28
  • \$\begingroup\$ Thanks, this worked very well. I have another related problem, but I'll post another question about that. \$\endgroup\$ – Bonfire Dog Oct 30 '15 at 14:06
1
\$\begingroup\$

Thanks, @Dmi7ry, for posting the answer to this question above in the comments. The functions to use here are lengthdir_x and lengthdir_y, as explained in the link to the other question.

For my purposes, I knew that the point on the turret that I want my bullet to appear at was 62 pixels out from the origin. The functions lengthdir_x and lengthdir_yallow you to find a set of coordinates out from an object's x/y origin by specifying how many pixels out from source you want, and in which direction. I knew that I needed to find a point 62 pixels out from origin, and that that point would need to be in whichever direction the turret object (and thus its barrel) was pointing, determined by the image_angle. Therefore:

instance_create(x + lengthdir_x(62, image_angle), y + lengthdir_y(62, image_angle), oBullet)
\$\endgroup\$
  • 1
    \$\begingroup\$ This answer would be improved for future users if you explained how those functions should be used and how they apply to the original problem statement. \$\endgroup\$ – user1430 Oct 30 '15 at 16:07
  • \$\begingroup\$ Excellent point, have just done so. \$\endgroup\$ – Bonfire Dog Oct 31 '15 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.