# Make object move with another object

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

## Recommended Posts

I'm working on a 3d space game and currently I'm trying to get mounted equipment on the player ship.

To test this I made 2 hardpoints on the ship model (basically untextured boxes, to get the coordinates.

Now, I can get the 2 testwise equipped weapons to move with the ship in terms of position, but it's not clear to me how to rotate the with the ship when turning.

Both the ship and the weapons are represented as entities in an ECS, with position and rotation components (among others), with the weapon entities also having an Offset component which basically represents the position of its hardpoint in model space.

Rotatiing an object around its own origin is pretty easy, but how do I rotate the weapons with the ship while keeping their position relative to it, so that they stay at their hardpoints?

##### Share on other sites
This is usually done with a parent-child hierarchy in games and 3d modeling. The child object will have a transform relative to it's parent (the offset you mentioned above which you calculated from your hardpoint dummies) and then you just need to apply the parent's transformation on top of that.

I'm really rusty on my 3d math, but you're basically going to multiply the child transformation matrix (which is relative to the parent, not the world) and the parent transformation matrix together, and use the resulting matrix on the child. Repeat the multiplication for each level of parent your child has if you end up with more complex scenes. Edited by SmkViper

##### Share on other sites

Rotate the object (with respect to its own origin) by the same rotation as the ship. Rotate the "hardpoints" coordinates (your "offset"?) with respect to the ship's origin by the same rotation as the ship. If your "offset" is, indeed, already relative to the ship, just rotate the coords. Position the rotated object at rotated-hardpoint + ship-position.

##### Share on other sites

As SmkViper pointed out, that's why hierarchies exist and you probably won't find any scene graph that is completely flat instead of a tree.

Not only is it more complicated to wrap your head around, but it's also a complete waste to constantly "start from scratch" with your transformation matrices. If you already modeled all the equipment in the same orientation (preferably the one matching the ships default orientation), just use the ships transformation, add the hard points position as translation and use that for your equipment. Job done, no headache involved.

If different ships do require different orientation of the equipment, place that required transformation in the hard points, apply it to the ships transformation. Nothing more to it and at no point should you ever care about the ships own rotation or position.

##### Share on other sites

I did it =) I was pretty close but your posts pushed me in the right direction, thanks guys.

By throwing the offset value and the ships rotation value in the Vector3.Transform method I get the correct Vector3 to add to the ship position

1. 1
Rutin
68
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 21
• 10
• 33
• ### Forum Statistics

• Total Topics
633438
• Total Posts
3011881
• ### Who's Online (See full list)

There are no registered users currently online

×