Click here to Skip to main content
15,884,936 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.4K   1.5K   34  
A game engine first prototype
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MTV3D65;

namespace gefx
{
   public  class EXDecals
    {
       ArrayList arLand = new ArrayList();



       public struct DecalStruct
       {

           public TVMesh decal;
           public string WhoCreate;
           public float Time;
           public float CTime;
       };

       public List<DecalStruct> DecalList = new List<DecalStruct>();
       public TVMathLibrary Math;
       public float GetLandscapeAltitude(float fx, float fz,TVLandscape Land)
       {
           //TV_COLLISIONRESULT eCollision = new TV_COLLISIONRESULT();
           TVCollisionResult eCollision = new TVCollisionResult();

           TV_3DVECTOR vStart = new TV_3DVECTOR(fx, 1024, fz);
           TV_3DVECTOR vEnd = new TV_3DVECTOR(fx, -10, fz);

           //int nHits = 1;

           TVLandscape lnd = Land;

           //Scene.AdvancedCollision(vStart, vEnd, ref eCollision, (int)MTV3D65.CONST_TV_OBJECT_TYPE.TV_OBJECT_ALL, MTV3D65.CONST_TV_TESTTYPE.TV_TESTTYPE_ACCURATETESTING, ref nHits);
           eCollision = lnd.AdvancedCollide(vStart, vEnd);
           //lnd.Destroy();

           if (eCollision.IsCollision())
               return eCollision.GetCollisionImpact().y;
           else
               return 0;
       }
       public TV_3DVECTOR GetLandscapeNormal(Single nx, Single nz,TVLandscape Land)
       {
           TVLandscape lnd = Land;
           return lnd.GetNormal(nx, nz);
       }
       public TV_3DVECTOR GetNormal(Single fx, Single fz,TVLandscape Land)
       {
           //TV_COLLISIONRESULT eCollision = new TV_COLLISIONRESULT();
           TV_3DVECTOR vNormal = new TV_3DVECTOR();
           Math = new TVMathLibrary();
           //TV_3DVECTOR vStart = new TV_3DVECTOR(fx, 1024, fz);
           //TV_3DVECTOR vEnd = new TV_3DVECTOR(fx, -10, fz);

           //int nHits = 1;
           //Scene.AdvancedCollision(vStart, vEnd, ref eCollision, (int)MTV3D65.CONST_TV_OBJECT_TYPE.TV_OBJECT_ALL, MTV3D65.CONST_TV_TESTTYPE.TV_TESTTYPE_ACCURATETESTING,ref nHits);

           //if (eCollision.bHasCollided)
           //{
           //if (eCollision.eCollidedObjectType == CONST_TV_OBJECT_TYPE.TV_OBJECT_LANDSCAPE)
           vNormal = GetLandscapeNormal(fx, fz, Land);
           //else
           //  vNormal = eCollision.vCollisionNormal;

           //}

           return Math.VNormalize(vNormal);
       }

       public void AlignAndDrop(ref TVMesh mesh, TVLandscape Land)
       {
           mesh.SetCollisionEnable(false);

           TV_3DVECTOR vPosition = mesh.GetPosition();
           TV_3DMATRIX mModel = mesh.GetRotationMatrix();

           //vPosition.y = GetAltitude(vPosition.x, vPosition.z);
           vPosition.y = GetLandscapeAltitude(vPosition.x, vPosition.z, Land);

           mesh.SetPosition(vPosition.x, vPosition.y + 0.01f, vPosition.z);

           TV_3DVECTOR vUp = GetNormal(vPosition.x, vPosition.z,Land );

           TV_3DVECTOR fForward = new TV_3DVECTOR(mModel.m31, mModel.m32, mModel.m33);

           TV_3DVECTOR vRight = Math.VCrossProduct(vUp, fForward);
           TV_3DVECTOR vFront = Math.VCrossProduct(vRight, vUp);
           vRight = Math.VNormalize(vRight);
           vFront = Math.VNormalize(vFront);

           mModel.m11 = vRight.x;
           mModel.m12 = vRight.y;
           mModel.m13 = vRight.z;
           mModel.m21 = vUp.x;
           mModel.m22 = vUp.y;
           mModel.m23 = vUp.z;
           mModel.m31 = vFront.x;
           mModel.m32 = vFront.y;
           mModel.m33 = vFront.z;

           TV_3DMATRIX MatRot = new TV_3DMATRIX();
           Math.TVMatrixRotationX(ref MatRot, 90);
           Math.TVMatrixMultiply(ref mModel, MatRot, mModel);
           mesh.SetRotationMatrix(mModel);

           mesh.SetCollisionEnable(true);
       }

       public void AddDecalTexture(string TPath, ref gefxs _core)
       {


           TVTextureFactory TF = new TVTextureFactory();

           //decaltex = _core.Tex.LoadTexture(TPath);

           decaltex = _core.Tex.LoadTexture(TPath, "texture", 10, 10, CONST_TV_COLORKEY.TV_COLORKEY_NO , true);

       }
       int decaltex;
       public void CreateDecal( TV_3DVECTOR position, TV_3DVECTOR normal, ref gefxs _core,float LiveTime,TVEngine TV)
       {
           TVMesh decal;



           decal = _core.Scene.CreateBillboard(decaltex, position.x, position.y + 0.01f, position.z, 1f, 1f, "Decal1", true);
           decal.SetBillboardType(CONST_TV_BILLBOARDTYPE.TV_BILLBOARD_NOROTATION);
           AlignAndDrop(ref decal, _core.Teren.Land );
           //decal.SetRotation(90, 0, 0);
           DecalStruct s = new DecalStruct();
           s.decal = decal;
           s.Time = LiveTime;
           s.CTime = TV.TimeElapsed();

           DecalList.Add(s);
       }

       public void DrawDecals()
       {

           foreach (DecalStruct m in DecalList)
           {

               m.decal.Render();

           }


       }

       public void RemoveDecals(TVEngine TV)
       {


           int idx=1;

           foreach (DecalStruct d in DecalList)
           {

               if (  (TV.TimeElapsed() - d.CTime ) > d.Time )
               {
                   d.decal.Destroy();
                   
                  

               }

           }

           //if (DecalList.Count > 0)
           //{

           //    DecalList.RemoveAt(idx);
           //}

       }



    }
}

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