Click here to Skip to main content
15,892,298 members
Articles / Multimedia / DirectX

Game Engine

Rate me:
Please Sign up or sign in to vote.
3.78/5 (6 votes)
20 Nov 2009CPL2 min read 47.5K   1.5K   34  
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;

using MTV3D65;
using IrrKlang; 

namespace gefx
{
    public  class XTModelActor
    {

        public TVActor UnitActor;
        public string UnitActorName;
        public string UnitStatus;
        public enum ActorType
        {

            Enemy,
            Friend,
            Free


        };

        public ActorType UnitType = ActorType.Free ;
        public void SetActorType(ActorType Type)
        {

            UnitType = Type;


        }

        public ActorType GetActorType()
        {

            return UnitType;

        }


        TVTextureFactory TextureFactory;
        public enum ModelActorType
        {

            AnimatedX,

            TVA

        };
        public TVScreen2DImmediate Screen2DImmediate;
        string material_file = "no file include";
        string shader_file = "no file include";
        string GPathToModel = "no path";

        public CONST_TV_PHYSICSBODY_BOUNDING PhysicObjectType;
        public float ModelMass;

        public int RigidBodyIndex;
        public bool IsSelected;
        public bool IsMoving=false ;
        public bool IsUsed= true ;
        public int Team = 0;
        public TV_3DVECTOR UnitPath;


        public XTModelActor()
        {
            UnitActor = new TVActor();
            TextureFactory = new TVTextureFactory();

        }
        public void AddMaterialToModel(string FName, string ModelName, ref TVMaterialFactory mat, ref TVGlobals glob)
        {
            string[] para = new string[19];
            string line;
            int counter = 0;
            TextReader tr = new StreamReader(FName);
            while ((line = tr.ReadLine()) != null)
            {
                para[counter] = line;
                counter++;
            }
            string[] D = para[0].Split(',');
            string[] A = para[1].Split(',');
            string[] S = para[2].Split(',');
            string[] P = para[3].Split(',');
            string[] E = para[4].Split(',');

            tr.Close();

                    string Name = System.IO.Path.GetFileName(FName);
                    mat.CreateMaterial("Mat" + Name);
                    mat.SetAmbient(glob.GetMat("Mat" + Name),
                        (float)System.Convert.ToSingle(A[1]),
                        (float)System.Convert.ToSingle(A[2]),
                        (float)System.Convert.ToSingle(A[3]),
                        1);
                    mat.SetDiffuse(glob.GetMat("Mat" + Name),
                        (float)System.Convert.ToSingle(D[1]),
                        (float)System.Convert.ToSingle(D[2]),
                        (float)System.Convert.ToSingle(D[3]),
                        1);
                    mat.SetSpecular(glob.GetMat("Mat" + Name),
                        (float)System.Convert.ToSingle(S[1]),
                        (float)System.Convert.ToSingle(S[2]),
                        (float)System.Convert.ToSingle(S[3]),
                        1);
                    mat.SetEmissive(glob.GetMat("Mat" + Name),
                        (float)System.Convert.ToSingle(E[1]),
                        (float)System.Convert.ToSingle(E[2]),
                        (float)System.Convert.ToSingle(E[3]),
                        1);
                    mat.SetPower(glob.GetMat("Mat" + Name), (float)System.Convert.ToSingle(P[1]));

                    UnitActor.SetMaterial(glob.GetMat("Mat" + Name));


                



        }
        public XTModelActor(ref TVScene Scene, ref TVGlobals Globals, string Name, string PathToModel, ModelActorType Type)
        {
            TextureFactory = new TVTextureFactory();
            if (Type == ModelActorType.TVA)
            {
                UnitActor = Scene.CreateActor(Name);
                UnitActor.LoadTVA(PathToModel, true, true);
                UnitActor.RotateY(90);            //We rotate the actor 180 degrees, so it's facing us.
                UnitActor.SetAnimationID(1);       //We want to play animation 1.
                UnitActor.SetAnimationLoop(true);
                UnitActor.PlayAnimation(1);
                UnitActor.SetMaterial(Globals.GetMat("Matte"));
                UnitActor.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                UnitActor.SetShadowCast(true, true);
                UnitActor.SetName(Name);
            }
            if (Type == ModelActorType.AnimatedX)
            {

                UnitActor = Scene.CreateActor(Name);
                UnitActor.LoadXFile(PathToModel, true, true);
                UnitActor.RotateY(90);            //We rotate the actor 180 degrees, so it's facing us.
                UnitActor.SetAnimationID(1);       //We want to play animation 1.
                UnitActor.SetAnimationLoop(true);
                UnitActor.PlayAnimation(1);
                UnitActor.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE, 0, 1);
                UnitActor.SetName(Name);
            }
            
            UnitActorName = Name;
            GPathToModel = PathToModel;
            InitSferaGledanja(ref Scene, 20);
        }
        public void CrtajJedinicu()
        {
            if (UnitActor != null)
            {
                UnitActor.Render();
                //SferaGledanja.Render();

            }

        }
        public void MakniJedinicu(ref TVScene Scene)
        {
            if (UnitActor != null)
            {
                UnitActor.Destroy();
                SferaGledanja.Destroy();
                

            }

        }

        public void MakniJedinicu(ref TVScene Scene, ref TVPhysics PHys)
        {
            if (UnitActor != null)
            {


                PHys.DestroyBody(RigidBodyIndex);
                UnitActor.Destroy();
                SferaGledanja.Destroy();
                UnitActor = null;

            }

        }

        public void MakniJedinicu(ref TVScene Scene, ref XTMannagerStatic GE)
        {
            foreach (BoneStruct b in UnitBoneListName)
            {

                GE.RemoveStaticUnit(b.NAme, ref Scene);

            }
            UnitBoneListName.Clear();

            if (UnitActor != null)
            {
                UnitActor.Destroy();
                SferaGledanja.Destroy();


            }

        }

        public void MakniJedinicu(ref TVScene Scene, ref TVPhysics PHys, ref XTMannagerStatic GE)
        {
            if (UnitActor != null)
            {
                foreach (BoneStruct b in UnitBoneListName)
                {

                    GE.RemoveStaticUnit(b.NAme, ref Scene, ref PHys);

                }

                PHys.DestroyBody(RigidBodyIndex);
                UnitActor.Destroy();
                UnitBoneListName.Clear();
                UnitActor = null;
                SferaGledanja.Destroy();

            }

        }

        #region Pozicija jedinice
        public void SetUnitPosition(TV_3DVECTOR V)
        {
            UnitActor.SetPosition(V.x, V.y, V.z);

        }
        public void SetUnitPosition(float X, float Y, float Z)
        {
            UnitActor.SetPosition(X, Y, Z);

        }

        public TV_3DVECTOR GetUnitPosition()
        {
            TV_3DVECTOR r = new TV_3DVECTOR(0, 0, 0);

            if (UnitActor != null)
            {
                r = UnitActor.GetPosition();
            }

            return r;

        }
        public void SetUnitRotation(TV_3DVECTOR r, float time)
        {
            if (UnitActor != null)
            {
                UnitActor.SetRotation(r.x * time, r.y * time, r.z * time);

            }

        }

        public void SetUnitRotation(float x, float y, float z, float time)
        {
            if (UnitActor != null)
            {
                UnitActor.SetRotation(x * time, y * time, z * time);

            }

        }


        public void SetUnitRotation(TV_3DVECTOR r)
        {
            if (UnitActor != null)
            {
                UnitActor.SetRotation(r.x, r.y, r.z);

            }

        }

        public void SetUnitRotation(float x, float y, float z)
        {
            if (UnitActor != null)
            {
                UnitActor.SetRotation(x, y, z);

            }

        }

        #endregion
        public void SetUnitScale(float x, float y, float z)
        {
            if (UnitActor != null)
            {

                UnitActor.SetScale(x, y, z);

            }

        }
        public void SetUnitScale(TV_3DVECTOR s)
        {
            if (UnitActor != null)
            {

                UnitActor.SetScale(s.x, s.y, s.z);

            }

        }


        

        public TV_3DVECTOR GetUnitScale()
        {

            TV_3DVECTOR r = new TV_3DVECTOR(0, 0, 0);

            if (UnitActor != null)
            {
                r = UnitActor.GetScale();

            }

            return r;

        }
        public int GetUnitID()
        {
            int r = 0;

            if (UnitActor != null)
            {

                r = UnitActor.GetIndex();
            }
            return r;

        }
        public void SetUnitTexture(string TexturePack, ref TVGlobals Globals)
        {
            if (UnitActor != null)
            {

                TextureFactory.LoadTexture(TexturePack, "Texture" + UnitActor.GetName().ToString(), -1, -1, CONST_TV_COLORKEY.TV_COLORKEY_NO, true);
                UnitActor.SetTexture(Globals.GetTex("Texture" + UnitActor.GetName().ToString()), -1);


            }

        }
        public void AddMaterialUnitFromFile(string FName,  ref TVMaterialFactory mat, ref TVGlobals glob)
        {
            string[] para = new string[19];
            string line;
            int counter = 0;
            TextReader tr = new StreamReader(FName);
            material_file = FName;
            while ((line = tr.ReadLine()) != null)
            {
                para[counter] = line;
                counter++;
            }
            string[] D = para[0].Split(',');
            string[] A = para[1].Split(',');
            string[] S = para[2].Split(',');
            string[] P = para[3].Split(',');
            string[] E = para[4].Split(',');
            tr.Close();
            int i = 0;
            string Name = System.IO.Path.GetFileName(FName);
            mat.CreateMaterial("Mat" + Name);
            mat.SetAmbient(glob.GetMat("Mat" + Name + UnitActor.GetName ()),
                (float)System.Convert.ToSingle(A[1]),
                (float)System.Convert.ToSingle(A[2]),
                (float)System.Convert.ToSingle(A[3]),
                1);
            mat.SetDiffuse(glob.GetMat("Mat" + Name + UnitActor.GetName()),
                (float)System.Convert.ToSingle(D[1]),
                (float)System.Convert.ToSingle(D[2]),
                (float)System.Convert.ToSingle(D[3]),
                1);
            mat.SetSpecular(glob.GetMat("Mat" + Name + UnitActor.GetName()),
                (float)System.Convert.ToSingle(S[1]),
                (float)System.Convert.ToSingle(S[2]),
                (float)System.Convert.ToSingle(S[3]),
                1);
            mat.SetEmissive(glob.GetMat("Mat" + Name + UnitActor.GetName()),
                (float)System.Convert.ToSingle(E[1]),
                (float)System.Convert.ToSingle(E[2]),
                (float)System.Convert.ToSingle(E[3]),
                1);
            mat.SetPower(glob.GetMat("Mat" + Name + UnitActor.GetName()), (float)System.Convert.ToSingle(P[1]));

            UnitActor.SetMaterial(glob.GetMat("Mat" + Name + UnitActor.GetName()));
        }
        public void ShowUnitBoundingBox(bool isShow)
        {

            if (UnitActor != null)
            {

                if (isShow)
                {

                    UnitActor.ShowBoundingBox(true);

                }
                else
                {
                    UnitActor.ShowBoundingBox(false);

                }

            }

        }
        public bool CheckMousePick(int Mx, int My, ref TVScene Scene)
        {
            TVCollisionResult CollisionResult;
            bool Ret = false;

            CollisionResult = Scene.MousePick(Mx, My,
                (int)CONST_TV_OBJECT_TYPE.TV_OBJECT_ACTOR ,
                CONST_TV_TESTTYPE.TV_TESTTYPE_BOUNDINGBOX);
            // Check if we did have a collision with a tank
            if (CollisionResult.IsCollision())
            {



                TVActor  colMesh;
                TV_3DVECTOR Tankpos;
                TV_3DVECTOR ColMeshpos;

                colMesh = CollisionResult.GetCollisionActor();
                Tankpos = UnitActor.GetPosition();
                ColMeshpos = colMesh.GetPosition();

                if ((Tankpos.x == ColMeshpos.x) && (Tankpos.z == ColMeshpos.z))
                {
                    Ret = true;
                    IsSelected = true;

                }







            }


            return Ret;

        }
        public void PlayAnimation(string Animation)
        {

            UnitActor.SetAnimationID(UnitActor.GetAnimationIDByName(Animation));

     

        }
        public void ChangeAnimLoop( bool f)
        {

            UnitActor.SetAnimationLoop(f);
         
        }
        public string[] GetListAnimation()
        {
            string[] r = new string[0];
                    r = new string[UnitActor.GetAnimationCount()];
                    int x = 0;
                    for (x = 0; x < UnitActor.GetAnimationCount() - 1; x++)
                    {
                        r[x] = UnitActor.GetAnimationName(x);
                    }
            return r;
        }


        public int GetBoneCount()
        {

            return UnitActor.GetBoneCount();
        }

        public string[] GetBones()
        {

            string[] r = new string[0];
            r = new string[UnitActor.GetBoneCount()];
            int xy = new int();

            int i = UnitActor.GetBoneCount();

            for (xy = 0; xy < UnitActor.GetBoneCount() - 1; xy++)
            {
                
                r[xy] = UnitActor.GetBoneName(xy).ToString();

            }

            return r;

        }

       public  void SaveUnitsToFile(string FName)
        {
            //try
            //{
                //pick whatever filename with .xml extension
                string filename = FName + ".xml";
                string RelativeToP;

                RelativeToP = Directory.GetCurrentDirectory();

                XmlDocument xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.Load(filename);
                    //xmlDoc.CreateComment(filename);
                }
                catch (System.IO.FileNotFoundException)
                {
                    //if file is not found, create a new xml file
                    XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                    xmlWriter.WriteStartElement("Root");
                    xmlWriter.Close();
                    xmlDoc.Load(filename);
                }
                XmlNode root = xmlDoc.DocumentElement;
                XmlElement childNode = xmlDoc.CreateElement("Unit");
                XmlElement childNode2 = xmlDoc.CreateElement("Paths");
                XmlElement childNode3 = xmlDoc.CreateElement("Position");
                XmlElement childNode4 = xmlDoc.CreateElement("Scale");
                XmlElement childNode5 = xmlDoc.CreateElement("Rotation");
                XmlElement childNode6 = xmlDoc.CreateElement("Materijal");
                XmlElement childNode7 = xmlDoc.CreateElement("Shader");


                //XmlText textNode = xmlDoc.CreateTextNode("hello");
                //textNode.Value = "hello, world";

                root.AppendChild(childNode);
                childNode.AppendChild(childNode2);
                childNode2.SetAttribute("NAME", UnitActorName );
                //ToRelative(RelativeToP, GPathToModel)
                childNode2.SetAttribute("MODEL", ToRelative(RelativeToP, GPathToModel));

                childNode2.AppendChild(childNode3);
                 
                childNode3.SetAttribute("X", UnitActor.GetPosition().x.ToString());
                childNode3.SetAttribute("Y", UnitActor.GetPosition().y.ToString());
                childNode3.SetAttribute("Z", UnitActor.GetPosition().z.ToString());

                childNode2.AppendChild(childNode4);

                childNode4.SetAttribute("X", UnitActor.GetScale().x.ToString());
                childNode4.SetAttribute("Y", UnitActor.GetScale().x.ToString());
                childNode4.SetAttribute("Z", UnitActor.GetScale().x.ToString());


                childNode2.AppendChild(childNode5);

                childNode5.SetAttribute("X", UnitActor.GetRotation().x.ToString());
                childNode5.SetAttribute("Y", UnitActor.GetRotation().x.ToString());
                childNode5.SetAttribute("Z", UnitActor.GetRotation().x.ToString());


                XmlElement childNode8 = xmlDoc.CreateElement("RigidBodyMesh");
                XmlElement[] chRigid = new XmlElement[UnitBoneListName.Count + 1];
                childNode2.AppendChild(childNode8);
                int i=0;
                foreach (BoneStruct b in UnitBoneListName)
                {
                    chRigid[i] = xmlDoc.CreateElement("BONERGD");
                    childNode2.AppendChild(chRigid[i]);
                    chRigid[i].SetAttribute(b.NAme, b.bOneId.ToString());
                    i++;
                }

                //textNode.Value = "replacing hello world";
                xmlDoc.Save(filename);
            //}
            //catch (Exception ex)
            //{
            //    //throw new System.ArgumentException("GRESKA tip : global u spremanju jedinice u file : " + ex.Message.ToString());
            //} 
        }

       private string ToRelative(string absolutePath, string relativeTo)
       {
           StringBuilder relativePath = new StringBuilder();


           try
           {

               string[] absoluteDirectories = absolutePath.Split('\\');
               string[] relativeDirectories = relativeTo.Split('\\');

               //Get the shortest of the two paths
               int length = absoluteDirectories.Length < relativeDirectories.Length ? absoluteDirectories.Length : relativeDirectories.Length;

               //Use to determine where in the loop we exited
               int lastCommonRoot = -1;
               int index;

               //Find common root
               for (index = 0; index < length; index++)
                   if (absoluteDirectories[index] == relativeDirectories[index])
                       lastCommonRoot = index;
                   else
                       break;

               //If we didn't find a common prefix then throw
               if (lastCommonRoot == -1)
                   throw new ArgumentException("Paths do not have a common base");

               //Build up the relative path


               //Add on the ..
               for (index = lastCommonRoot + 1; index < absoluteDirectories.Length; index++)
                   if (absoluteDirectories[index].Length > 0)
                       relativePath.Append("..\\");

               //Add on the folders
               for (index = lastCommonRoot + 1; index < relativeDirectories.Length - 1; index++)
                   relativePath.Append(relativeDirectories[index] + "\\");
               relativePath.Append(relativeDirectories[relativeDirectories.Length - 1]);
           }
           catch (Exception e)
           {

               return relativeTo;


           }

           return relativePath.ToString();
       }


       public void StopPlayingAnimation()
       {
           UnitActor.PauseAnimation();

       }

       public void ResumePlayAnimation()
       {

           UnitActor.PlayAnimation();

       }

       public void SetUnitMatrix(TV_3DMATRIX Matrix)
       {

           UnitActor.SetMatrix(Matrix);

       }

      

       public TV_3DVECTOR GetUnitRotate()
       {

           return UnitActor.GetRotation();
       }
       public TV_3DMATRIX GetUnitRotateMatrix()
       {

           return UnitActor.GetRotationMatrix();

       }

       public TV_3DMATRIX GetMatrix()
       {

           return UnitActor.GetMatrix();
       }

       public void DrawCoordinateSystem(float Length)
       {
           Screen2DImmediate = new TVScreen2DImmediate();
           TV_3DMATRIX mtxMesh;
           mtxMesh = UnitActor.GetMatrix();
           TV_3DVECTOR vecLookAt = new TV_3DVECTOR(mtxMesh.m31, mtxMesh.m32, mtxMesh.m33);

           TVMathLibrary Math = new TVMathLibrary();


           vecLookAt.x = vecLookAt.x + Math.ACos(UnitActor.GetRotation().y) * Length;
           vecLookAt.y = vecLookAt.y + Math.ASin(UnitActor.GetRotation().x) * Length;
           vecLookAt.z = vecLookAt.z + Math.ASin(UnitActor.GetRotation().y) * Length;

           Screen2DImmediate.Draw_Line3D(UnitActor.GetPosition().x, UnitActor.GetPosition().y,
               UnitActor.GetPosition().z, vecLookAt.x, vecLookAt.y, vecLookAt.z, 1, 1);





       }


       public void SetUnitPosition(TV_3DVECTOR V, ref TVPhysics TVP)
       {
           UnitActor.SetPosition(V.x, V.y, V.z);
           TVP.SetBodyPosition(RigidBodyIndex, V.x, V.y, V.z);

       }
       public void SetUnitPosition(float X, float Y, float Z, ref TVPhysics TVP)
       {
           UnitActor.SetPosition(X, Y, Z);
           TVP.SetBodyPosition(RigidBodyIndex, X, Y, Z);


       }


       public void SetUnitPosition(TV_3DVECTOR V, ref TVPhysics TVP,ref gefxs GE)
       {
           UnitActor.SetPosition(V.x, V.y, V.z);
           TVP.SetBodyPosition(RigidBodyIndex, V.x, V.y, V.z);
           MoveBoneBBWithActor(ref GE);


       }
       public void SetUnitPosition(float X, float Y, float Z, ref TVPhysics TVP, ref gefxs GE)
       {
           UnitActor.SetPosition(X, Y, Z);
           TVP.SetBodyPosition(RigidBodyIndex, X, Y, Z);
           MoveBoneBBWithActor(ref GE);



       }

       //public TV_3DVECTOR GetUnitPosition()
       //{
       //    TV_3DVECTOR r = new TV_3DVECTOR(0, 0, 0);

       //    if (UnitActor != null)
       //    {
       //        r = UnitActor.GetPosition();
       //    }

       //    return r;

       //}
       public void SetUnitRotation(TV_3DVECTOR r, float time, ref TVPhysics TVP)
       {
           if (UnitActor != null)
           {
               UnitActor.SetRotation(r.x * time, r.y * time, r.z * time);
               TVP.SetBodyRotation(RigidBodyIndex, r.x * time, r.y * time, r.z * time);

           }

       }

       public void SetUnitRotation(float x, float y, float z, float time, ref TVPhysics TVP)
       {
           if (UnitActor != null)
           {
               UnitActor.SetRotation(x * time, y * time, z * time);
               TVP.SetBodyRotation(RigidBodyIndex, x * time, y * time, z * time);


           }

       }


       public void SetUnitRotation(TV_3DVECTOR r, ref TVPhysics TVP)
       {
           if (UnitActor != null)
           {
               UnitActor.SetRotation(r.x, r.y, r.z);
               TVP.SetBodyRotation(RigidBodyIndex, r.x, r.y, r.z);


           }

       }

       public void SetUnitRotation(float x, float y, float z, ref TVPhysics TVP)
       {
           if (UnitActor != null)
           {
               UnitActor.SetRotation(x, y, z);
               TVP.SetBodyRotation(RigidBodyIndex, x, y, z);


           }

       }


       public void UpdateActor()
       {
           UnitActor.Update();


       }
       public void UpdateActor(ref gefxs GE,bool see)
       {
           UnitActor.Update();
           if (see)
           {
               MoveBoneBBWithActor(ref GE);
           }


       }


       public void UpdateActor(ref XTMannagerStatic  GE, bool see)
       {
           UnitActor.Update();
           if (see)
           {
               MoveBoneBBWithActor(ref GE);
           }


       }
 

       public void Walk(TV_3DVECTOR DestinationPoint,  ref TVPhysics TVP, float fTime, float speed, TVLandscape Land)
       {
           TVMathLibrary MathLibrary = new TVMathLibrary();
           TV_3DVECTOR vd = DestinationPoint;
           TV_3DVECTOR Direction = new TV_3DVECTOR(0, 0, 0);
           float TankAngleY = new float();
           


               

                   MathLibrary.TVVec3Subtract(ref vd, DestinationPoint, GetUnitPosition());
                   MathLibrary.TVVec3Normalize(ref vd, vd);
                   Direction = vd;
                   if (Direction.z > 0)
                       TankAngleY = MathLibrary.Rad2Deg(((float)System.Math.Atan((double)Direction.x / Direction.z)));
                   else
                       TankAngleY = MathLibrary.Rad2Deg((float)System.Math.Atan((double)Direction.x / Direction.z)) + 180;

                   SetUnitRotation(0f, TankAngleY, 0f, ref TVP);


                   if ((MathLibrary.GetDistance3D(GetUnitPosition().x, 0, GetUnitPosition().z,
                       DestinationPoint.x, 0, DestinationPoint.z)) > 2)
                   {
                       // Update all the tank's position
                       TV_3DVECTOR dV2 = new TV_3DVECTOR(0, 0, 0);
                       TV_3DVECTOR TankPosition = new TV_3DVECTOR(0, 0, 0);
                       TankPosition = GetUnitPosition();
                       MathLibrary.TVVec3Scale(ref dV2, Direction, ((float)fTime * speed));
                       MathLibrary.TVVec3Add(ref TankPosition, TankPosition, dV2);

                       if (Land == null)
                       {
                           TankPosition.y = 0;
                       }
                       else
                       {

                           TankPosition.y = Land.GetHeight(TankPosition.x, TankPosition.z) + 0;

                       }
                       // Update the tank's mesh position

                       SetUnitPosition(TankPosition.x, TankPosition.y, TankPosition.z, ref TVP);
                       
                   }


                   

               }

       public void Walk(TV_3DVECTOR DestinationPoint, ref TVPhysics TVP, float fTime, float speed, TVLandscape Land,ref gefxs GE)
       {
           TVMathLibrary MathLibrary = new TVMathLibrary();
           TV_3DVECTOR vd = DestinationPoint;
           TV_3DVECTOR Direction = new TV_3DVECTOR(0, 0, 0);
           float TankAngleY = new float();

           MathLibrary.TVVec3Subtract(ref vd, DestinationPoint, GetUnitPosition());
           MathLibrary.TVVec3Normalize(ref vd, vd);
           Direction = vd;
           if (Direction.z > 0)
               TankAngleY = MathLibrary.Rad2Deg(((float)System.Math.Atan((double)Direction.x / Direction.z)));
           else
               TankAngleY = MathLibrary.Rad2Deg((float)System.Math.Atan((double)Direction.x / Direction.z)) + 180;

           SetUnitRotation(0f, TankAngleY, 0f, ref TVP);


           if ((MathLibrary.GetDistance3D(GetUnitPosition().x, 0, GetUnitPosition().z,
               DestinationPoint.x, 0, DestinationPoint.z)) > 2)
           {
               // Update all the tank's position
               TV_3DVECTOR dV2 = new TV_3DVECTOR(0, 0, 0);
               TV_3DVECTOR TankPosition = new TV_3DVECTOR(0, 0, 0);
               TankPosition = GetUnitPosition();
               MathLibrary.TVVec3Scale(ref dV2, Direction, ((float)fTime * speed));
               MathLibrary.TVVec3Add(ref TankPosition, TankPosition, dV2);

               if (Land == null)
               {
                   TankPosition.y = 0;
               }
               else
               {

                   TankPosition.y = Land.GetHeight(TankPosition.x, TankPosition.z) + 0;

               }
               // Update the tank's mesh position

               SetUnitPosition(TankPosition.x, TankPosition.y, TankPosition.z, ref TVP);

               if (GE.Kamera2._camera.GetPosition().y == TankPosition.y)
               {

                   TankPosition.y = Land.GetHeight(TankPosition.x, TankPosition.z) + 1;

               }

               GE.Dekoli.AddDecalTexture("D:\\TV3D\\Primjeri\\physics1\\Physics 1\\Physics 1\\bin\\Debug\\Textures\\BulletHole.dds", ref GE);

               int NumEvents = GE.Fizika.PhysX.PollEvents();
               for (int i = 0; i < NumEvents; i++)
               {
                   TV_EVENT_PHYSICSCOLLISION pevent = GE.Fizika.PhysX.GetEventCollisionDesc(i);
                   
                   GE.Dekoli.GetLandscapeNormal(pevent.vPosition.x, pevent.vPosition.z, GE.Teren.Land);
                   GE.Dekoli.CreateDecal(TankPosition, pevent.vNormal, ref GE, 10f, GE.TV);
                   
               }
           
           }




       }
       private TV_3DVECTOR ReachDestinaton;

       public void Walk(TV_3DVECTOR DestinationPoint, ref TVPhysics TVP, float fTime, float speed, TVLandscape Land, ref gefxs GE,bool GenerateDecals, float DEcalTimeLive)
       {
           TVMathLibrary MathLibrary = new TVMathLibrary();
           TV_3DVECTOR vd = DestinationPoint;
           TV_3DVECTOR Direction = new TV_3DVECTOR(0, 0, 0);
           float TankAngleY = new float();

           MathLibrary.TVVec3Subtract(ref vd, DestinationPoint, GetUnitPosition());
           MathLibrary.TVVec3Normalize(ref vd, vd);
           Direction = vd;
           if (Direction.z > 0)
               TankAngleY = MathLibrary.Rad2Deg(((float)System.Math.Atan((double)Direction.x / Direction.z)));
           else
               TankAngleY = MathLibrary.Rad2Deg((float)System.Math.Atan((double)Direction.x / Direction.z)) + 180;

           SetUnitRotation(0f, TankAngleY, 0f, ref TVP);


           if ((MathLibrary.GetDistance3D(GetUnitPosition().x, 0, GetUnitPosition().z,
               DestinationPoint.x, 0, DestinationPoint.z)) > 2)
           {
               // Update all the tank's position
               TV_3DVECTOR dV2 = new TV_3DVECTOR(0, 0, 0);
               TV_3DVECTOR TankPosition = new TV_3DVECTOR(0, 0, 0);
               TankPosition = GetUnitPosition();
               MathLibrary.TVVec3Scale(ref dV2, Direction, ((float)fTime * speed));
               MathLibrary.TVVec3Add(ref TankPosition, TankPosition, dV2);

               if (Land == null)
               {
                   TankPosition.y = 0;
               }
               else
               {

                   TankPosition.y = Land.GetHeight(TankPosition.x, TankPosition.z) + 0;

               }
               // Update the tank's mesh position

               SetUnitPosition(TankPosition.x, TankPosition.y, TankPosition.z, ref TVP);
               IsUsed = true;
               if (GenerateDecals)
               {

                   GE.Dekoli.AddDecalTexture("D:\\TV3D\\Primjeri\\physics1\\Physics 1\\Physics 1\\bin\\Debug\\Textures\\BulletHole.dds", ref GE);

                   int NumEvents = GE.Fizika.PhysX.PollEvents();
                   for (int i = 0; i < NumEvents; i++)
                   {
                       TV_EVENT_PHYSICSCOLLISION pevent = GE.Fizika.PhysX.GetEventCollisionDesc(i);

                       GE.Dekoli.GetLandscapeNormal(pevent.vPosition.x, pevent.vPosition.z, GE.Teren.Land);
                       GE.Dekoli.CreateDecal(TankPosition, pevent.vNormal, ref GE, DEcalTimeLive, GE.TV);

                   }

               }
               IsMoving = true;

           }
           else
           {

               IsMoving = false;
               IsUsed = true;
              
           }




       }


       public void AddActorPath(TV_3DVECTOR Path)
       {
           ReachDestinaton = Path;
           IsMoving = true;
           IsUsed = true;

       }

       TV_3DVECTOR DSFC;
       public  TV_3DVECTOR LookAtVector;
       public void Walk( float fTime, float speed, ref gefxs GE, bool GenerateDecals, float DEcalTimeLive,string  DecoleTexture)
       {
           TVMathLibrary MathLibrary = new TVMathLibrary();
           TV_3DVECTOR vd = ReachDestinaton;
           TV_3DVECTOR Direction = new TV_3DVECTOR(0, 0, 0);
           float TankAngleY = new float();



           MathLibrary.TVVec3Subtract(ref vd, ReachDestinaton, GetUnitPosition());
               MathLibrary.TVVec3Normalize(ref vd, vd);
               Direction = vd;
               if (Direction.z > 0)
                   TankAngleY = MathLibrary.Rad2Deg(((float)System.Math.Atan((double)Direction.x / Direction.z)));
               else
                   TankAngleY = MathLibrary.Rad2Deg((float)System.Math.Atan((double)Direction.x / Direction.z)) + 180;

               SetUnitRotation(0f, TankAngleY, 0f, ref GE.Fizika.PhysX);


               if ((MathLibrary.GetDistance3D(GetUnitPosition().x, 0, GetUnitPosition().z,
                   ReachDestinaton.x, 0, ReachDestinaton.z)) > 2)
               {
                   // Update all the tank's position
                   TV_3DVECTOR dV2 = new TV_3DVECTOR(0, 0, 0);
                   TV_3DVECTOR TankPosition = new TV_3DVECTOR(0, 0, 0);
                   TankPosition = GetUnitPosition();
                   MathLibrary.TVVec3Scale(ref dV2, Direction, ((float)fTime * speed));
                   
                   MathLibrary.TVVec3Add(ref TankPosition, TankPosition, dV2);
                   LookAtVector = dV2;
                   if (GE.Teren.Land == null)
                   {
                       TankPosition.y = 0;
                   }
                   else
                   {

                       TankPosition.y = GE.Teren.Land.GetHeight(TankPosition.x, TankPosition.z) + 0;

                   }
                   // Update the tank's mesh position

                   SetUnitPosition(TankPosition.x, TankPosition.y, TankPosition.z, ref GE.Fizika.PhysX);
                   IsUsed = true;
                   if (GenerateDecals)
                   {

                       //GE.Dekoli.AddDecalTexture("D:\\TV3D\\Primjeri\\physics1\\Physics 1\\Physics 1\\bin\\Debug\\Textures\\BulletHole.dds", ref GE);
                       GE.Dekoli.AddDecalTexture(DecoleTexture, ref GE);

                       int NumEvents = GE.Fizika.PhysX.PollEvents();
                       for (int i = 0; i < NumEvents; i++)
                       {
                           TV_EVENT_PHYSICSCOLLISION pevent = GE.Fizika.PhysX.GetEventCollisionDesc(i);

                           GE.Dekoli.GetLandscapeNormal(pevent.vPosition.x, pevent.vPosition.z, GE.Teren.Land);
                           GE.Dekoli.CreateDecal(TankPosition, pevent.vNormal, ref GE, DEcalTimeLive, GE.TV);

                       }

                   }
                   IsMoving = true;

               }
               else
               {

                   IsMoving = false;
                   IsUsed = false ;

               }
           



       }

       public void Walk(ref gefxs GE ,TV_3DVECTOR DestinationPoint, ref TVPhysics TVP, float fTime, float speed, TVLandscape Land)
       {
           TVMathLibrary MathLibrary = new TVMathLibrary();
           TV_3DVECTOR vd = DestinationPoint;
           TV_3DVECTOR Direction = new TV_3DVECTOR(0, 0, 0);
           float TankAngleY = new float();





           MathLibrary.TVVec3Subtract(ref vd, DestinationPoint, GetUnitPosition());
           MathLibrary.TVVec3Normalize(ref vd, vd);
           Direction = vd;
           if (Direction.z > 0)
               TankAngleY = MathLibrary.Rad2Deg(((float)System.Math.Atan((double)Direction.x / Direction.z)));
           else
               TankAngleY = MathLibrary.Rad2Deg((float)System.Math.Atan((double)Direction.x / Direction.z)) + 180;

           SetUnitRotation(0f, TankAngleY, 0f, ref TVP);


           if ((MathLibrary.GetDistance3D(GetUnitPosition().x, 0, GetUnitPosition().z,
               DestinationPoint.x, 0, DestinationPoint.z)) > 2)
           {
               // Update all the tank's position
               TV_3DVECTOR dV2 = new TV_3DVECTOR(0, 0, 0);
               TV_3DVECTOR TankPosition = new TV_3DVECTOR(0, 0, 0);
               TankPosition = GetUnitPosition();
               MathLibrary.TVVec3Scale(ref dV2, Direction, ((float)fTime * speed));
               MathLibrary.TVVec3Add(ref TankPosition, TankPosition, dV2);

               if (Land == null)
               {
                   TankPosition.y = 0;
               }
               else
               {

                   TankPosition.y = Land.GetHeight(TankPosition.x, TankPosition.z) + 0;

               }
               // Update the tank's mesh position

               SetUnitPosition(TankPosition.x, TankPosition.y, TankPosition.z, ref TVP, ref GE);
               //GE.Dekoli.CreateDecal("D:\\TV3D\\Primjeri\\physics1\\Physics 1\\Physics 1\\bin\\Debug\\Textures\\BulletHole.dds", TankPosition, new TV_3DVECTOR(0, 1, 0), ref GE, 5);

           }




       }


       int i = 10;
       string b = "";
       int x = new Int32();

       public struct BoneStruct
       {
           public  string NAme;
           public int bOneId;

       };

       public List<BoneStruct> UnitBoneListName = new List<BoneStruct>();


       public void SetActorBoneBB( ref gefxs GE)
       {

           x = UnitActor.GetBoneCount();


           foreach (BoneStruct b in UnitBoneListName)
           {

               GE.XModel.RemoveStaticUnit(b.NAme, ref GE.Scene,ref GE.Fizika.PhysX );

           }

           UnitBoneListName.Clear();
           for (i = 1; i < x - 2; i++)
           {

               GE.XModel.AddStaticUnit("B_" + UnitActorName + UnitActor.GetBoneName(i), ref GE, XTModel.ModelType.TVM);

               GE.XModel.SetUnitMatrix("B_" + UnitActorName + UnitActor.GetBoneName(i), UnitActor.GetBoneMatrix(i));
               BoneStruct str;
               str.bOneId = i;
               str.NAme = "B_" + UnitActorName + UnitActor.GetBoneName(i);
               
               UnitBoneListName.Add(str);


           }



       }

       public void DestroyAttachedBoneMesh(ref XTMannagerStatic XT,ref TVScene scene,ref TVPhysics Ph)
       {

           foreach (BoneStruct b in UnitBoneListName)
           {

               XT.RemoveStaticUnit(b.NAme, ref scene, ref Ph);
               


           }
           UnitBoneListName.Clear();


       }

       public void MoveBoneBBWithActor(ref gefxs GE)
       {

           foreach (BoneStruct  s in UnitBoneListName)
           {

               GE.XModel.SetUnitMatrix(s.NAme, UnitActor.GetBoneMatrix(s.bOneId));


           }


       }

       public void MoveBoneBBWithActor(ref XTMannagerStatic  GE)
       {

           foreach (BoneStruct s in UnitBoneListName)
           {
               

               GE.SetUnitMatrix(s.NAme, UnitActor.GetBoneMatrix(s.bOneId));


           }


       }


       public  TVMesh SferaGledanja ;
       float LookRadius;
       public void InitSferaGledanja(ref TVScene Scene, float Radius)
       {
           SferaGledanja = Scene.CreateMeshBuilder(UnitActorName.ToString() + "_Area");
           SferaGledanja.CreateSphere(Radius);
           LookRadius = Radius;
           float offset = Radius / 2;
           SferaGledanja.SetPosition(UnitActor.GetPosition().x - offset  , UnitActor.GetPosition().y - offset, UnitActor.GetPosition().z );
           SferaGledanja.Enable(false  );
           SferaGledanja.SetCollisionEnable(true);

       }

       public void MoveSferGledanja()
       {

           float offset = LookRadius / 2;


           //SferaGledanja.SetPosition(UnitActor.GetPosition().x , UnitActor.GetPosition().y - offset, UnitActor.GetPosition().z   );
           RotateSferaGledanja();


       }

       public void RotateSferaGledanja()
       {

           

       }

     

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Software Developer
Croatia Croatia
Programer

Comments and Discussions