Jump to content
  • Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!