# How to get smaller shapes to follow/be attached to a larger shape.

This topic is 972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Basically, I'm trying to make a game where I have a boat that has a number of guns on it's left and right sides that fire projectiles. The number of guns, and their sizes, will vary to an extent. Obviously, once that position is set, it should remain there as the boat moves around. What I'm having difficulty figuring out how to do is handle when the boat rotates. the boat should be able to rotate in a circle, and if the boat rotates say, 90 degrees, the guns should all stay in the same spot on the boat. I have included a rough picture of what I would hope the final result looks like, with three guns on each side.

Does anyone have any idea how I would do this? I have no problem rotating a single shape, but I've never had to deal with a problem like this before.

##### Share on other sites

I have little experience in 3d programming, but to me, the solution is to position the guns relative to the origin of the boat. You rotate the boat and all guns around that same point, and everything will stay where it is supposed to be.

##### Share on other sites

One way is to use transformation graph. Sort of scene graph, just limited to transformation relations.

Or think of it as of skeleton in skeletal animation.

You will have nodes, attached to other nodes and adding some local transforms (rotate/move relative to parent node).

Once per frame you will evaluate that graph, calculating each node's final transformation by multiplying parent node's transformation by node's local transformation, starting from root node.

Local transforms may be simple location/rotation/scale, or complex - calculate rotation to point at some target, or even to some other node within graph.

Though it might be redundant for your needs, since probably you will have predefined set of locations, and probably every attached gun/turret will be connected only to base, but not to some other movable part. So you will have that graph degraded to list of items, having location/rotation source data (relative to base). You may evaluate global transforms for each item (base's transform multiplied by item's transform) to draw items separately. Or you can calculate only local transform matrix (when source data altered) to manually transform items' geometry if you batch it to draw in single draw call.

##### Share on other sites

vstrakh's method is the most flexible way, but probably overkill if all you want is those guns to rotate with the sprite.  Let's say a gun is originally offset from the boat's rotation origin by <dx,dy>.  If you are rotating the boat's position by theta, just set the gun's position to:

gun.x = boat.x + dx*cos(theta) - dy*sin(theta)
gun.y = boat.y + dx*sin(theta) + dy*cos(theta)

##### Share on other sites

It's a method of Matrix Multiplication when it comes to transforms.

I went ahead and found you a video that will explain this, and even give you a great example on how to program this.

If you don't understand homogeneous coordinates, he covers that as well. This will work for any arbitrary position of the gun, ship, etc.

Edited by Tangletail

##### Share on other sites

Thanks for all the responses. I'm going to try what is explained in the video Tangletail posted.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634113
• Total Posts
3015568
×