4

I'm designing a 3D game, in which I have a camera behind a spaceship to move around. I have added some asteroids in the game as well, and I want to add bullets. I used quaternions to control the path of the spaceship and the camera behind it. Nevertheless, when I use the same quaternions (the rotation in specific so that i can calculate the path of the bullet) on the bullet, I get a really funny effect and the paths are totally wrong. Any ideas as to why this happens?

public void Update(GameTime gameTime)

    {
        lazerRotation = spaceship.spaceShipRotation;

        Vector3 rotVector = Vector3.Transform(new Vector3(0, 0, -1), lazerRotation); 


        Position +=  rotVector* 10 * (float)gameTime.ElapsedGameTime.TotalSeconds;
        //

    }
    //spaceShipRotation = Quaternion.Identity;
    //Quaternion additionalRot = Quaternion.CreateFromAxisAngle(new Vector3(0, -1, 0), leftRightRot) * Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), upDownRot);
    //spaceShipRotation *= additionalRot;
    //Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -1), spaceShipRotation);
    //    futurePosition += addVector * movement * velocity;


    public void Draw(Matrix view, Matrix projection)
    {


       // //Quaternion xaxis = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1), spaceship.leftrightrot);
       // //Quaternion yaxis = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), spaceship.updownrot);
       // //Quaternion rot = xaxis * yaxis;
       //Matrix x =  Matrix.CreateRotationX(spaceship.leftrightrot);
       //Matrix y = Matrix.CreateRotationY(spaceship.updownrot);
       //Matrix rot = x * y;

        Matrix[] transforms = new Matrix[Model.Bones.Count];
        Model.CopyAbsoluteBoneTransformsTo(transforms);
        Matrix worldMatrix = Matrix.CreateFromQuaternion(lazerRotation) * Matrix.CreateTranslation(Position);



        foreach (ModelMesh mesh in Model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {


                effect.World = worldMatrix * transforms[mesh.ParentBone.Index];  //position
                effect.View = view; //camera
                effect.Projection = projection; //2d to 3d

                effect.EnableDefaultLighting();
                effect.PreferPerPixelLighting = true;
            }
            mesh.Draw();
        }
    }

Have in mind that my spaceship is a field of the bullet object and that is how I get the spaceship. Rotation quaternion. Thanks in advance.

Tom H
  • 46,766
  • 14
  • 87
  • 128
Martinos
  • 167
  • 1
  • 2
  • 9

2 Answers2

2

You should concatenate the effect.World matrix opposite of the order you are currently doing it. This shouldn't affect the shape of the object but will solve other problems. It should be like this:

effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;

XNA is a row major framework, the concatenation is left to right. Your code (right to left) is how you would do it for OpenGL, a column major framework.

Steve H
  • 5,479
  • 4
  • 20
  • 26
1

It is my understanding that, based upon your code, your projectiles will change direction after being fired if your ship changes direction.

You need to create a projectile object and store the direction of each individual projectile each projectile's object. When the projectile is instantiated, you will set its direction to the same as the ship (as you have done above). However, for the entire rest of the life of that projectile, you need to not change its direction.

Example: Bullet 1 is fired from POINT A towards POINT B, then the ship turns and fires Bullet 2 from POINT C to POINT D.

Bullet 1 and 2 will each have unique vectors upon which they travel.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • yes the code above was just to test that.. i have done that and i got the same result.. soo that is not the problem something else is.. :S – Martinos Apr 29 '11 at 20:13
  • Can you write some debug code to confirm that the direction vector of your individual projectiles does not change between frames? – Brian Webster Apr 29 '11 at 21:21
  • Also, do your projectiles change directions after they are fired, or are they just being fired in weird directions? – Brian Webster Apr 29 '11 at 21:21
  • the thing is its not the direction that i am having problem this. the object is changing shape in the draw method! i dont know why!:S i also have with the directions but that is not so major. – Martinos Apr 29 '11 at 22:02
  • Ok, so your bullets are changing shape? This is pretty simple to fix if that's the case – Brian Webster Apr 30 '11 at 03:16