Make object move with another object

Started by
3 comments, last by Stefan Fischlschweiger 9 years, 6 months ago

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?

Advertisement
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.

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.

f@dzhttp://festini.device-zero.de

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

This topic is closed to new replies.

Advertisement