I have a Model of a platform carring a gun turret on it. The turret has two barrels and I am attemping to shoot bullets from the barrels.
The turret itself is a Mesh contained in the Model. I am able to rotate the turret by manipulating the ModelBone.Transform matrix like so:
turretBone.Transform = Matrix.CreateRotationX(3.14f /2f) * turretMatrix * Matrix.CreateTranslation(turretBone.Transform.Translation);
theModel.CopyAbsoluteBoneTransformsTo(transforms);
where theModel represents the whole model (platform + turret), turretBone is the ModelBone representing the turret and turretMatrix represents a 4x4 XNA matrix that describes the rotation of the turret so that it is facing the player.
This part works perfectly.
Now, I would like to fire a bullet from the tip of the barrels of the turret. For some reason, I cannot determine what is the position of the tips of the barrells so that I use that as the originating position of the bullets.
The direction of fire is ok as I use turretBone.Transform.Down as the direction. (I Know up/down/etc are relative but imagine Down as in a top-view shooter game where the barrel shoots down towards the player, i.e., increasing Y in XNA 2D).
I understand that turretBone.Transform is relative to its parent, correct ? So, if I would like to move 100 units to the right, and 400 units down, what would I have to do ?
I have tried to do something like this:
Position + turretBone.Translation + turretBone.Transform.Right * 900f + turretBone.Transform.Down * 400f
to move 900 units to the right and 400 units down from the turret (where Position is the position of the entire model).
This doesn't work though; it's starts out correctly at the tip of the barrels but as it rotates it doesn't quite follow it).
I thought of another solution to add two small spheres at the tip of the barrels and somehow obtain their coordinates in world space to place the start position of the bullets there. Any idea how to do that ?
Thanks!