How do i make a sprite move with another sprites point

hi all

im making a game using a customizable tank as my player character. The problem im having is, i have drawn the base of the tank and the turret of the tank as 2 separate sprites because i want the turret to be able to move independently. The base of the tank has 8 directional movements but the turret has no automation, i have tried to do it using points where point tank of turret should move with point turret of tank, but just cant get it to work here is what im doing but i know how to implement this bit i just cant get the sprite to stay in a fixed position on the tank, you can move the tank all over the screen hopefully the turret will be moving with it but then using z and x keyboard press to rotate the turret around point tank of turret if the player then changes direction the turret slowly rotates back to the front of the tank. here is what i have for it to follow the tank that doesnt work,

no condition
action - put turret around Tank.PointX(Turret);Tank.PointY(Turret) with an angle and distance of 0

can any1 be kind enough to tell me what im doing wrong

There is an action to set the position of an object (which is a way better than the action to put an object around something).

i will have a play around nw, iv also noticed that when i give the base of the tank a custom size it is opposite in the preview window e.g the custom size of x is actually resizing y in the preview window and i have conditions or actions for this its messing my head up lol

now im using position of an object object turret =x: Tank.PointX(Turret) =y: Tank.PointY(Turret) this still isnt working it doesnt move at all when im moving tank base

First, you’re using the action “put object around…” and that don’t makes sense here. “Put object around…” is useful, for example, when you want to put a sprite of the Moon to orbit a sprite of the Earth. I bet you don’t want to put your turret in orbit with your tank; insted you want to make the turret rotate on its own center…

Now let’s talk about bounding boxes…

Although there are not refered as it in GD, a bounding box is the rectangled (or squared) shape that delimits a sprite picture and it matches the real size of your sprite picture, including all tranparent pixels in it.

Now, let’s talk about object points…

Object points are just vectors or pairs of coordinates (x,y) you can set in objects for different purposses BUT there are two very important object points present in any object: ORIGIN and CENTER.

What’s origin point? Well it is the reference point to set the position of an object and is set by default to x=0,y=0… that’s the left upper corner of the bounding box of the object (even if there is nothing more than a transparent pixel). When you positionate an object in a x,y position on the game canvas, that means this x,y position in the canvas will match the origin point of the object.

What’s center point? Well, center point is used as the pivot when you rotate an object.

In other words, let’s imagine you pinned a squared piece of paper to the wall by its left upper corner, you can rotate it with your hand but the squared paper will rotate around its own corner, where it’s pinned. If you pinned the same piece of paper by its center you can make it rotate perfectly centered and that’s what you want to do with the tank turret.

What do you need to do?

  • Edit the center point of your turret sprite to move it to a proper pivot position
  • Now, edit the origin point of your turret sprite to match its center point (origin point must be equal to center point)
  • In the events, as Victor say to you, use a simple position action (NOT the “put object around bla bla” action) to always set the position of the turret on the turret-point of your tank (this point have to be proper centered in the base of the turret, in your tank sprite)
  • Besides that, use another action to rotate the turret when its needed

That should do the trick! :wink:

EDIT: some corrections suggested by Lizard-13.



thats what i got dunno why it isnt moving

The pivot point is not the Origin but the Centre. You can put the Centre anywhere, but you have to put the Origin point in the position of the Centre (to get the common effect with child objects) :slight_smile:
These “orbit” effect could be the turret positioned with the Origin point, but rotating (relative to its parent) over the Centre :neutral_face:

You are using PointX() twice, instead PointX(); PointY() :wink:

ok cheers ill change the points coordinates n thats cos i was lazy n copied n pasted it into the y axis n fogot to change it lol

still nothing do i have to add an automation to the turret or something so it can move or should it work now iv changed the center coordinates and the code error

You’re absolutely right. Thank you. I’ll fix and update my explanation.

Yes, but why use more process-expensive actions when you can do it more efficiently, right?

Show us your project (project file + all its resources, in a zip), to take a look.

i can only make a rar file
Tank Commander.rar (1.49 MB)

look what it does aswell i think it is because i rotated the file then reput the tank in the game but i can sort that problem i just need to get the turret to move with the tank smoothly i know how to implement the rest i think lol

You also have a group with the same name as the Tank, it might the cause of the problem…

didnt think about that i only created the group to try move it all as a group but didnt know how to do it

Groups are here not to move objects together but to use different objects with the same name under events (to generalize some event as instance).

Ok! Let’s start!

First, some minor fixes I suggest regarding your sprites:

  • Don’t let different sized large margins of transparent pixels around the main picture of your sprites if it isn’t for a good reason. That’s unnecesary use of disk space (even if you have a 4 terabytes hard disk, it’s just not elegant) and only hinders the proper laying of objects.
  • You don’t need to use a 1000x1000 pixels 1,4MB picture for your background. You can achive the same effect with the tile object and a small sprite.

Now, problems I find in your project:

  • You haven’t make the origin point of the turret sprite equal to its center (pivot) point as Lizard-13 told you.
  • There’s no actions at all to properly rotate the turret and that’s why the turret is not rotating.

Attached to this post, in a compressed file, you’ll find a functional tank-and-turret movement project sporting (fixed versions of) your sprites.

There are two different versions of the project inside the compressed file:

  • Turret movement by keyboard: this project controls the turrent movement using the Z and X keys (as you wanted), slowly turning the turret 45° in one direction each time. Problem with this version is the angular movement of turret feels absolutely disembodied from the tank body due to the turret position fixed in very specific angles (controlled by the Z and X keys) even when the whole tank is turning around (further development on this issue requires some calculations to find the current angle of the turret relative to the rotation angle of the whole tank).
  • Turret movement by cursor: this project controls the turrent movement using the mouse, slowly turning the turret towards the cursor position. It feels like a more natural movement and it’s the most commonly used technique in this kind of games. Also, you will see it can be implemented with faaar fewer actions.

In both projects the whole tank movement is controlled with the arrows (provided by default by the top-down automatism).

tank_turret_movement.7z (68.2 KB)

So, hope it helps. Good luck.

ok but the reason why i havnt put that in is because i cant get the turret to move with the tank and im making the images smaller anyway but thanks

thankyou for that though i see how it works now :wink:

Then, I missunderstood the nature of your problem.

You’re welcome :wink:

Thanks for your help this is what i have now and its working perfect thanks
Tank Commander.rar (24.9 KB)