Hey all,
I'm running into a problem, that I'm pretty sure is caused by what I'm doing with my matrices and how. I know that matrix math isn't communicative(I think thats the correct phrase, correct me if I'm wrong), in that A * B != B * A. I figured out that this line:
effect.World = mgBullet.mgBulletWorld *
Matrix.CreateTranslation(mgBullet.mgBulletPosition);
has something to do with my problem. I have an AI ship that is supposed to always face the player, move towards it, while firing a bullet. When it is a set distance from the player it is supposed to stop moving and just face the player and fire. It can only fire when the distance between it and the player is less than or equal to a distance I've set. The problem I've got is that when I fly around my ship, the place the bullets spawn is no longer from the ship, and aren't being fire even remotely close to the player. If I remove Matrix.CreateTranslation(mgBullet.mgBulletPosition), then everything works perfectly except for that my bullets don't actually move. I have tried switching the order of the multiplication, etc, but that just breaks it even worse.
| //This code is what determines the rotation of the AI ship, which is |
| //what we set the world matrix of the bullet to when we create it. |
| public void FollowTarget(Vector3 targetPos) |
| { |
| shipWorld = Matrix.Identity; |
| shipWorld.Forward = Vector3.Normalize(targetPos - myShipPos); |
| shipWorld.Right = Vector3.Normalize(Vector3.Cross(shipWorld.Forward, Vector3.Up)); |
| shipWorld.Up = Vector3.Cross(shipWorld.Right, shipWorld.Forward); |
| shipWorld.Translation = myShipPos; |
| } |
| |
| //This is the code that creates the bullet being fired, it is |
| //located in the main game update function |
| if (enemyClip >= 0 && !enemyMGBClip[enemyClip].isAlive) //&& (enemyFireDelay >= .1f)) |
| { |
| soundBank.PlayCue("Machine_Gun3"); |
| enemyMGBClip[enemyClip].isAlive = true; |
| enemyMGBClip[enemyClip].vel += 10; |
| enemyMGBClip[enemyClip].mgBulletPosition = enemy.myShipPos; |
| enemyMGBClip[enemyClip].mgBulletWorld = enemy.shipWorld; |
| enemyClip -= 1; |
| enemy.isFiring = false; |
| enemyFireDelay = 0; |
| if (enemyClip <= 0) |
| enemyClip = 49; |
| } |
| void DrawMGBullet(MGBullet mgBullet) |
| { |
| foreach (ModelMesh mesh in mgBullet.mgBullet.Meshes) |
| { |
| foreach (BasicEffect effect in mesh.Effects) |
| { |
| effect.EnableDefaultLighting(); |
| effect.PreferPerPixelLighting = true; |
| effect.World = mgBullet.mgBulletWorld * Matrix.CreateTranslation(mgBullet.mgBulletPosition); |
| effect.Projection = cameraProjectionMatrix; |
| effect.View = cameraViewMatrix; |
| } |
| mesh.Draw(); |
| } |
| } |
|
Here is most of the relevant code. I've check and recheck and re-rechecked everything and I cant for the life of me figure out why its doing what its doing. Please let me know if you need more of the code!
Thanks in Advance!
-Merc