Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » DirectX » Games » Downloads
 
Add your own
alternative version

Game Engine

, 20 Nov 2009 CPL
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MTV3D65;

namespace gefx
{
    public  class XCamera
    {
        TV_2DVECTOR _curRotation, _destRotation;

        public TVCamera _camera;
        public  TVInputEngine _inputs;
        TV_3DVECTOR CamDirection = new TV_3DVECTOR(0, 0, 0);

        public int tmpMouseX, tmpMouseY;
        public bool tmpMouseB1, tmpMouseB2, tmpMouseB3, tmpMouseB4;
        public int tmpMouseX1, tmpMouseY1;
        public bool tmpMouseB11, tmpMouseB21, tmpMouseB31, tmpMouseB41;
        public int tmpMouseScrollOld, tmpMouseScrollNew;


        private TVMathLibrary Math = new TVMathLibrary();
        float fars = 2000.0f;
        float nears = 1.0f;
        float degs = 0.0f;
        float Z = 1;
        float UZ;


        float sngPositionX;
        float sngPositionY;
        float sngPositionZ = -20;
        float sngLookAtX;
        float sngLookAtY;
        float sngLookAtZ;

        float sngLookatX;
        float sngLookatY;
        float sngLookatZ;

        float sngWalk;
        float sngStrafe;
        float sngAngleX, sngAngleY;

        public XCamera() 
        {
            Initialize();
        }

        public  void Initialize() {
            _curRotation = _destRotation = new TV_2DVECTOR(0f, 0f);
            
            _inputs = new TVInputEngine();
            _camera = new TVCamera();
            _inputs.Initialize(true, true);
        }

        public  void Update(float ftime) {
            int mx = 0, my = 0;
            float speed;

            bool isgo = false;


            if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_SPACE))
            {
                isgo = true;
            }
            else
            {

                isgo = false;

            }
            //isgo = true;

            if (isgo)
            {

                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFTSHIFT))
                {
                    speed = 0.1f * ftime;
                }
                else
                {
                    speed = 0.05f * ftime;
                }

                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_W))
                {
                    _camera.MoveRelative(speed, 0f, 0f, false);
                }
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_A))
                {
                    _camera.MoveRelative(0f, 0f, -speed, false);
                }
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_S))
                {
                    _camera.MoveRelative(-speed, 0f, 0f, false);
                }
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_D))
                {
                    _camera.MoveRelative(0f, 0f, speed, false);
                }

                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_UP))
                {
                    _camera.MoveRelative(speed, 0f, 0f, false);
                }
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFT))
                {
                    _camera.MoveRelative(0f, 0f, -speed, false);
                }
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWN))
                {
                    _camera.MoveRelative(-speed, 0f, 0f, false);
                }
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHT))
                {
                    _camera.MoveRelative(0f, 0f, speed, false);
                }

                speed = ftime / 10f;

                _inputs.GetMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2, ref tmpMouseB3, ref tmpMouseB4, ref tmpMouseScrollNew);

                
                _destRotation.x += (float)tmpMouseY * speed;
                _destRotation.y += (float)tmpMouseX * speed;

               
                if (!_curRotation.Equals(_destRotation))
                {
                      _curRotation = TVVec2CurveAdjust(_curRotation, _destRotation, 0.25f);
                }

              

                    _camera.SetRotation(_curRotation.x, _curRotation.y, 0f);
                }

            _inputs.GetAbsMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2);

            
            
        }


       
        public int ValueScrool = 0;
        public void UpdateTerrainCamera(ref TVEngine TV, TVLandscape Land)
        {
            Application.DoEvents();
            bool Enable = false;

            if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_SPACE) || tmpMouseB3 )
            {
                Enable = true;

            }
           

                Application.DoEvents();

                tmpMouseScrollOld = tmpMouseScrollNew;

                // Get the movement of the mouse.
                _inputs.GetAbsMouseState(ref tmpMouseX1, ref tmpMouseY1, ref tmpMouseB11, ref tmpMouseB21);

                _inputs.GetMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2, ref tmpMouseB3, ref tmpMouseB4, ref tmpMouseScrollNew);

                if (Enable)
                {
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_UP))
                    sngWalk = 1;

                                // If we are not walking forward, maybe we are walking backward
                // by using the DOWN arrow? If so, set walk speed to negative.
                else if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWN))
                    sngWalk = -1;

                // Check if we pressed the LEFT arrow key, if so, then strafe
                // on the left.
                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFT))
                    sngStrafe = 1;

                // If we are not strafing left, maybe we want to strafe to the
                // right, using the RIGHT arrow? If so, set strafe to negative.
                else if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHT))
                    sngStrafe = -1;

                // Actual value to old mouse scroller value.
                // Update the camera angles.
                sngAngleX = sngAngleX - ((float)tmpMouseY / 100);
                sngAngleY = sngAngleY - ((float)tmpMouseX / 100);

                if (sngAngleX > 1.3)
                    sngAngleX = 1.3F;
                if (sngAngleX < -1.3)
                    sngAngleX = -1.3F;
                // Okay, now for the smothing of the movement... Update
                // the forward and backward (walk) movement.
                if (sngWalk > 0)
                {
                    sngWalk = sngWalk - 0.005F * (float)TV.TimeElapsed();
                    if (sngWalk < 0)
                        sngWalk = 0;
                }
                else
                {
                    sngWalk = sngWalk + 0.005F * (float)TV.TimeElapsed();
                    if (sngWalk > 0)
                        sngWalk = 0;
                }

                // Now, we update the left and right (strafe) movement.
                if (sngStrafe > 0)
                {
                    sngStrafe = sngStrafe - 0.005F * (float)TV.TimeElapsed();
                    if (sngStrafe < 0)
                        sngStrafe = 0;
                }
                else
                {
                    sngStrafe = sngStrafe + 0.005F * (float)TV.TimeElapsed();
                    if (sngStrafe > 0)
                        sngStrafe = 0;
                }

                // Update the vectors using the angles and positions.
                sngPositionX = sngPositionX + (float)(System.Math.Cos((double)sngAngleY) * sngWalk / 5 * TV.TimeElapsed()) + (float)(System.Math.Cos((double)sngAngleY + 3.141596 / 2) * sngStrafe / 5 * TV.TimeElapsed());
                sngPositionZ = sngPositionZ + (float)(System.Math.Sin((double)sngAngleY) * sngWalk / 5 * TV.TimeElapsed()) + (float)(System.Math.Sin((double)sngAngleY + 3.141596 / 2) * sngStrafe / 5 * TV.TimeElapsed());

            
                if(_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_PAGEUP ) )
                {

                    ValueScrool += 1;
                }

                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_PAGEDOWN ))
                {

                    ValueScrool -= 1;
                }

                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_W ))
                {

                    ValueScrool += 1;
                }

                if (_inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_S))
                {

                    ValueScrool -= 1;
                }
                
                if (ValueScrool <= 2)
                {
                    ValueScrool = 2;

                }

                sngPositionY = Land.GetHeight(sngPositionX, sngPositionZ) + ValueScrool;

                // We update the look at position.
                sngLookatX = sngPositionX + (float)System.Math.Cos((double)sngAngleY);
                sngLookatY = sngPositionY + (float)System.Math.Tan((double)sngAngleX);
                sngLookatZ = sngPositionZ + (float)System.Math.Sin((double)sngAngleY);


                // With the new values of the camera vectors (position and
                // look at), we update the scene's camera.
                TV_3DVECTOR v;
                v.x = sngPositionX;
                v.y = sngPositionY;
                v.z = sngPositionZ;
               
                _camera.SetPosition(sngPositionX, sngPositionY, sngPositionZ);

                _camera.SetLookAt(sngLookatX, sngLookatY, sngLookatZ);

               

                }
        }


        /// <summary>
        /// Because I suck too much to make Hermite interpolation work.
        /// </summary>
        TV_2DVECTOR TVVec2CurveAdjust(TV_2DVECTOR current, TV_2DVECTOR destination, float speed)
        {
            current.x += (destination.x - current.x) * speed;
            current.y += (destination.y - current.y) * speed;

            return current;
        }

        public void SetUpCameraPosition(TV_3DVECTOR CameraPosition)
        {

            _camera.SetPosition(CameraPosition.x, CameraPosition.y, CameraPosition.z);
            _camera.SetViewFrustum(90, 2000);
        }

        public TV_3DVECTOR GetCameraPosition()
        {
            return _camera.GetPosition();

        }

        public void SetUpCameraDirection(float x, float y, float z)
        {
            CamDirection.x = x;
            CamDirection.y = y;
            CamDirection.z = z;
            _camera.SetLookAt(x, y, z);

        }

        public TV_3DVECTOR GetCameraDirection()
        {

            return CamDirection;
        }


        public void GetMSpacePos(float Mx, float My, ref TV_3DVECTOR rV)
        {
            float NearPl = new float();
            float FarPl = new float();
            float degres = new float();
            float Z = new float();

            _camera.GetViewFrustum(ref  degres, ref FarPl, ref  NearPl);
            float UZ = new float();
            //Z = 2;
            //UZ = (FarPl / (FarPl - NearPl)) + ((NearPl * FarPl) / (FarPl - NearPl)) * (1 / Z);

            //Math.Project2DPointTo3D(Mx, My, UZ, ref  rV);

            TV_3DVECTOR NearPos = new TV_3DVECTOR(0, 0, 0);
            TV_3DVECTOR FarPos = new TV_3DVECTOR(0, 0, 0);

            Math.GetMousePickVectors(Mx, My, ref NearPos, ref FarPos);
            float sngDistFromNearPlane = 2000;

            sngDistFromNearPlane = NearPl / FarPl;

            rV = NearPos + ((FarPos - NearPos) * ((sngDistFromNearPlane - NearPl) / (FarPl - NearPl)));

            //kamerMouseSpacePos = rV;



        }

        public void UpdateCamera()
        {

            _inputs.ForceUpdate();
            

        }


    }
}

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)

Share

About the Author

Member 3873633
Software Developer
Croatia Croatia
Programer

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Member 3873633
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid