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();
}
}
}
}
//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