Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am having a problem in my 3D jet fighter game using XNA. I have a player jet and a few enemy drones built from a separate class. The problem is that when I set player position and a drone's position to a height of 10f in y-direction, they aren't at the same height, but if I move a drone's position up 500f in the y-direction, then it is pretty close to the player. Relatively they are supposedly at the same height but with different position values. Can anyone help, please?

Here is the code of Enemy Class:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace _3D_Game_Project
{
class Enemy
{
    SpriteFont Font;
    Model Drone;
    bool CanMoveForward;
    bool FlyRightNow;
    bool FlyLeftNow;
    bool TurnAroundNow;
    bool BusyInOperation;
    public bool CanCheckForCollision;
    public Matrix DroneFanRot = Matrix.Identity;
    public Matrix EnemyWorld = Matrix.Identity;
    public Vector3 Position;
    public Quaternion Rotation = Quaternion.Identity;
    public BoundingSphere DroneBounds;
    public BoundingBox LimitBox;
    public BoundingBox[] BuildingBoxes;
    public void Initialize(ContentManager Content, BoundingBox Limit, BoundingBox[] BuildingBox, Vector3 Pos)
    {
        LimitBox = Limit;
        CanCheckForCollision = true;
        BuildingBoxes = new BoundingBox[BuildingBox.Length];
        Font = Content.Load<SpriteFont>("Calibri");
        Drone = Content.Load<Model>("Drone");
        Position = Pos;
        FlyLeftNow = false;
        FlyRightNow = false;
        TurnAroundNow = false;
        BusyInOperation = false;
        CanMoveForward = true;
        //new Vector3(Position.X / 500, Position.Y / 500, Position.Z / 500)
        DroneBounds = new BoundingSphere(Position, 0.04f);
        EnemyWorld = Matrix.CreateFromQuaternion(Rotation) * Matrix.CreateTranslation(Position);
    }

    float RotatedAround = 0;
    public void TurnAround()
    {
        if (RotatedAround >= 180)
        {
            BusyInOperation = false;
            TurnAroundNow = false;
            RotatedAround = 0;
            CanMoveForward = true;
        }
        else
        {
            Rotation *= Quaternion.CreateFromYawPitchRoll(MathHelper.Pi / 60, 0, 0);
            RotatedAround += MathHelper.Pi / 60;
        }
    }

    float RotatedRight = 0;
    public void FlyRight()
    {
        if (RotatedRight >= 90)
        {
            BusyInOperation = false;
            RotatedRight = 0;
            FlyRightNow = false;
            CanMoveForward = true;
        }
        else
        {
            Rotation *= Quaternion.CreateFromYawPitchRoll(MathHelper.PiOver2/60, 0, 0);
            RotatedRight += MathHelper.PiOver2 / 60;
        }
    }
    float RotatedLeft = 0;
    public void FlyLeft()
    {
        if (RotatedLeft >= 90)
        {
            BusyInOperation = false;
            RotatedLeft = 0;
            FlyLeftNow = false;
            CanMoveForward = true;
        }
        else
        {
            Rotation *= Quaternion.CreateFromYawPitchRoll(-MathHelper.PiOver2/60, 0, 0);
            RotatedLeft += (MathHelper.PiOver2 / 60);
        }
    }
    Random Turn = new Random();
    public void SignalOperation(bool TurnDrone)
    {
        if (TurnDrone)
        {
            if (CanCheckForCollision == true)
            {
                if (BusyInOperation == false)
                {
                    BusyInOperation = true;
                    CanCheckForCollision = false;
                    if (Turn.Next(1, 150) < 50)
                    {
                        FlyLeftNow = true;
                    }
                    else if (Turn.Next(1, 150) > 50 || Turn.Next(1, 150) < 100)
                    {
                        FlyRightNow = true;
                    }
                    else if (Turn.Next(1, 150) > 100)
                    {
                        TurnAroundNow = true;
                    }
                }
            }
        }
    }
    float DelayCollision = 3;
    public void Update(Vector3 PositionOfPlayer, Quaternion RotationOfPlayer, GameTime gameTime, List<Enemy> Drones)
    {                   
        if (CanMoveForward)
        {
            MoveForward(ref Position, Rotation, 1.5f);
            DelayCollision -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (DelayCollision <= 0)
            {
                CanCheckForCollision = true;
                DelayCollision = 3;
            }

        }
        if (FlyLeftNow)
        {
            CanMoveForward = false;
            FlyLeft();
        }
        else if (FlyRightNow)
        {
            CanMoveForward = false;
            FlyRight();
        }
        else if (TurnAroundNow)
        {
            CanMoveForward = false;
            TurnAround();
        }
        EnemyWorld = Matrix.CreateFromQuaternion(Rotation) * Matrix.CreateTranslation(Position);
        DroneFanRot *= Matrix.CreateRotationZ(MathHelper.PiOver4);

        DroneBounds = new BoundingSphere(Position, 0.04f);
    }

    private void MoveForward(ref Vector3 position, Quaternion rotationQuat, float speed)
    {
        Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -3), rotationQuat);
        position += addVector * speed;
    }
    public void Draw(SpriteBatch spriteBatch, Matrix ViewMatrix, Matrix ProjectionMatrix)
    {
        Matrix[] EnemyTransforms = new Matrix[Drone.Bones.Count];
        Drone.CopyAbsoluteBoneTransformsTo(EnemyTransforms);
        foreach (ModelMesh Meshes in Drone.Meshes)
        {
            if (Meshes == Drone.Meshes["Propeller"])
            {
                foreach (BasicEffect effect in Meshes.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.Projection = ProjectionMatrix;
                    effect.View = ViewMatrix;
                    effect.World = EnemyTransforms[Meshes.ParentBone.Index] * EnemyWorld * Matrix.CreateScale(0.005f, 0.005f, 0.005f) * DroneFanRot;
                }
            }
            else
            {
                foreach (BasicEffect effect in Meshes.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.Projection = ProjectionMatrix;
                    effect.View = ViewMatrix;
                    effect.World = EnemyTransforms[Meshes.ParentBone.Index] * EnemyWorld * Matrix.CreateScale(0.005f, 0.005f, 0.005f);
                }
            }

            Meshes.Draw();
        }
    }
}

}

share|improve this question
    
Positions relative to what? Can you clarify with some code? –  Marcks Thomas Dec 16 '12 at 17:22
    
Position Of Enemy Drone Relative To Player. Sure I'll Edit Post With The Whole Code Of Enemy Class. But Some Of The Code Is Not Being Used –  CoOlDud3 Dec 16 '12 at 17:24
    
I'm reading it as: the height of the enemy drone in relation to the player is supposed to be the same as the height of the player in relation to the player. That seems obfuscated. Is your question: 'why do I have to set different position values to make the player and enemy appear at the same height'? Also, I suggest you omit parts of your code that are not essential to answering this question. People dislike scrolling through 224 lines and if they don't have to, you'll get better answers. –  Marcks Thomas Dec 16 '12 at 18:05
    
Yes I understand. My question really is hard to understand. Okay I'll refine it. For Player And Enemy Drone To Be On The Same Position. I use vector3(8,1,-3) for player and vector3(8*500, 1*500, -3*500) for the drone. And I Don't get why. I'll remove the unnecessary code. Thanks for your kind help sir. –  CoOlDud3 Dec 16 '12 at 18:10
    
Even though its commented out in Initialize, you might have more instances of this in the code: //new Vector3(Position.X / 500, Position.Y / 500, Position.Z / 500). For starters search for "500" in your code, and/or for "0.002" –  Maik Semder Dec 16 '12 at 20:48
show 2 more comments

1 Answer

up vote 0 down vote accepted

I finally figured out the answer to my question. It had something to do with the order of multiplication of matrices in the Draw Method.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.