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
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using MTV3D65;
using IrrKlang; 

namespace gefx
{
    public  class EXVehicle
    {

        int matLand;
        int matVehicleBody;
        int matWindow;
        int matWheels;

        
        XTModel Xm_fl;
        XTModel Xm_fr;
        XTModel Xm_rl;
        XTModel Xm_rr;

       
       public  TVMesh m_fl;
       public  TVMesh m_fr;
       public  TVMesh m_rl;
       public  TVMesh m_rr;
       public float susheight = 0.5f; //distance from chassis to wheel 0.5f
       public float susplen = 10f; // 10
       public float susshock = 40f; //Springiness of suspension 10
       public float susspring = 300f; //Stiffness of suspension 400

       
        int flw;
        int frw;
        int rlw;
        int rrw;
        int car_ID;
        float steerAngle;

        public string VehicaleName;

        public EXVehicle()
        {


        }
        public EXVehicle(ref gefxs GE, string modelPath, string name, XTModel.ModelType MType, float Mass,TV_3DVECTOR Position)
        {

            AddVehicaleBody(ref GE, modelPath, name, MType, Mass, Position);

        }

        public void CreateVehicale(ref gefxs GE)
        {




        }

    

        public void AddVehicaleBody(ref gefxs GE, string modelPath, string name, XTModel.ModelType MType, float Mass,TV_3DVECTOR Position)
        {
            GE.XModel.AddStaticUnit(name, modelPath ,ref GE ,MType);
            GE.XModel.SetPhysics(
                ref GE.Fizika.PhysX,
                name,
                CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_CONVEXHULL,
                Mass);


            GE.Fizika.PhysX.SetAutoFreeze(GE.XModel.GetPhysxBodyIndex(name), false);
            GE.Fizika.PhysX.SetBodyPosition(GE.XModel.GetPhysxBodyIndex(name), Position.x, Position.y, Position.z );
            GE.Fizika.PhysX.SetBodyRotation(GE.XModel.GetPhysxBodyIndex(name), 0f, 0f, 0f);
            car_ID = GE.Fizika.PhysX.CreateVehicle(GE.XModel.GetPhysxBodyIndex(name));
            GE.Fizika.PhysX.SetBodyCenterOfMass(car_ID, new TV_3DVECTOR(0, -1.0f, 10f));

            VehicaleName = name;

        }

        public void AddCarWheels(ref gefxs GE, string modelPath,  XTModel.ModelType MType, float Mass, float scale)
        {
            //GE.XModel.AddStaticUnit(name, ref GE,MType  );
            //GE.XModel.SetPhysics(
            //    ref GE.Fizika.PhysX,
            //    name, 
            //    CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_CYLINDER, 
            //    Mass);


            m_fl = GE.Scene.CreateMeshBuilder("mfl");
            m_fl.LoadTVM(modelPath, true, true);
            m_fl.SetScale(scale, scale, scale);
            m_fl.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_fl.SetMaterial(matWheels);
            m_fl.SetTexture(GE.Globals.GetTex("Wheel"));
            m_fl.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_fl.SetShadowCast(true, true);

            //Front Right Wheel
            m_rl = GE.Scene.CreateMeshBuilder("mrl");
            m_rl.LoadTVM(modelPath, true, true);
            m_rl.SetScale(scale, scale, scale);
            m_rl.SetMaterial(matWheels);
            m_rl.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_rl.SetTexture(GE.Globals.GetTex("Wheel"));
            m_rl.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_rl.SetShadowCast(true, false);
            m_rl.SetShadowCast(true, true);

            //Rear Left Wheel
            m_fr = GE.Scene.CreateMeshBuilder("mfr");
            m_fr.LoadTVM(modelPath, true, true);
            m_fr.SetScale(scale, scale, scale);
            m_fr.SetMaterial(matWheels);
            m_fr.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_fr.SetTexture(GE.Globals.GetTex("Wheel"));
            m_fr.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_fr.SetShadowCast(true, false);

            //Rear Right Wheel
            m_rr = GE.Scene.CreateMeshBuilder("mrr");
            m_rr.LoadTVM(modelPath, true, true);
            m_rr.SetScale(scale, scale, scale);
            m_rr.SetMaterial(matWheels);
            m_rr.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_rr.SetTexture(GE.Globals.GetTex("Wheel"));
            m_rr.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_rr.SetShadowCast(true, false);
            m_rr.SetShadowCast(true, true);

            flw = GE.Fizika.PhysX.AddVehicleWheelEx(car_ID, Mass, 0.5f * scale, 0.372f * scale, new TV_3DVECTOR(1, 0, 0), -0.8f * scale, -susheight * scale, 1.25f * scale, 1, 0, 0, susplen, susshock, susspring, m_fl); //fl
            frw = GE.Fizika.PhysX.AddVehicleWheelEx(car_ID, Mass, 0.5f * scale, 0.372f * scale, new TV_3DVECTOR(1, 0, 0), 0.8f * scale, -susheight * scale, 1.25f * scale, 1, 0, 0, susplen, susshock, susspring, m_fr); //fr
            rlw = GE.Fizika.PhysX.AddVehicleWheelEx(car_ID, Mass, 0.5f * scale, 0.372f * scale, new TV_3DVECTOR(1, 0, 0), -0.8f * scale, -susheight * scale, -1.425f * scale, 1, 0, 0, susplen, susshock, susspring, m_rl); //rl
            rrw = GE.Fizika.PhysX.AddVehicleWheelEx(car_ID, Mass, 0.5f * scale, 0.372f * scale, new TV_3DVECTOR(1, 0, 0), 0.8f * scale, -susheight * scale, -1.425f * scale, 1, 0, 0, susplen, susshock, susspring, m_rr); //rr





        }


        public void CheckInput(ref  gefxs GE)
        {
            float CarPower = 3000; //3000

              

            if (GE.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_UPARROW)) //Accellerate
            {
                GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, rlw, CarPower, -1000);
                GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, rrw, CarPower, -1000);
            }
            else
            {
                if (GE.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWNARROW))
                {
                    GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, rlw, -CarPower, -1000);
                    GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, rrw, -CarPower, -1000);
                }
                else
                {
                    GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, flw, 0, -10000);
                    GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, frw, 0, -10000);
                    GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, rlw, 0, -10000);
                    GE.Fizika.PhysX.SetVehicleWheelTorque(car_ID, rrw, 0, -10000);
                }
            }

            //Steering
            if (GE.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFTARROW))
            {
                steerAngle -= 5;
                if (steerAngle < -45)
                    steerAngle = -45;

                GE.Fizika.PhysX.SetVehicleWheelSteering(car_ID, flw, steerAngle);
                GE.Fizika.PhysX.SetVehicleWheelSteering(car_ID, frw, steerAngle);
            }
            else
            {
                if (GE.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHTARROW))
                {
                    steerAngle += 5;
                    if (steerAngle > 45)
                        steerAngle = 45;

                    GE.Fizika.PhysX.SetVehicleWheelSteering(car_ID, flw, steerAngle);
                    GE.Fizika.PhysX.SetVehicleWheelSteering(car_ID, frw, steerAngle);
                }
                else
                {
                    steerAngle = 0;

                    GE.Fizika.PhysX.SetVehicleWheelSteering(car_ID, flw, steerAngle);
                    GE.Fizika.PhysX.SetVehicleWheelSteering(car_ID, frw, steerAngle);
                }
            }

            //Car Handbrake
            if (GE.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_X))
            {
                GE.Fizika.PhysX.VehicleWheelHandBrake(car_ID, rlw, 1, 2000);
                GE.Fizika.PhysX.VehicleWheelHandBrake(car_ID, rrw, 1, 2000);
            }

            //if (GE.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_P))
            //{
            //    GE.Fizika.PhysX.VehicleReset(car_ID);
            //    GE.Fizika.PhysX.SetBodyRotation(pbi_chassis, 0, 0, 0);
            //    GE.Fizika.PhysX.SetBodyPosition(pbi_chassis, GE.Fizika.PhysX.GetBodyPosition(pbi_chassis).x, GE.Fizika.PhysX.GetBodyPosition(pbi_chassis).y + 0.2f, GE.Fizika.PhysX.GetBodyPosition(pbi_chassis).z);
            //}

            //if (GE.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_O))
            //{
            //    GE.Fizika.PhysX.VehicleReset(car_ID);
            //    GE.Fizika.PhysX.SetBodyPosition(pbi_chassis, 0f, 5, 0f);
            //    GE.Fizika.PhysX.SetBodyRotation(pbi_chassis, 0f, 0f, 0f);
            //}


        }


        public void CheckInput( TVInputEngine input, ref TVPhysics phy)
        {
            float CarPower = 3000; //3000



            if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_UPARROW)) //Accellerate
            {
                phy.SetVehicleWheelTorque(car_ID, rlw, CarPower, -1000);
                phy.SetVehicleWheelTorque(car_ID, rrw, CarPower, -1000);
            }
            else
            {
                if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWNARROW))
                {
                    phy.SetVehicleWheelTorque(car_ID, rlw, -CarPower, -1000);
                    phy.SetVehicleWheelTorque(car_ID, rrw, -CarPower, -1000);
                }
                else
                {
                    phy.SetVehicleWheelTorque(car_ID, flw, 0, -10000);
                    phy.SetVehicleWheelTorque(car_ID, frw, 0, -10000);
                    phy.SetVehicleWheelTorque(car_ID, rlw, 0, -10000);
                    phy.SetVehicleWheelTorque(car_ID, rrw, 0, -10000);
                }
            }

            //Steering
            if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFTARROW))
            {
                steerAngle -= 5;
                if (steerAngle < -45)
                    steerAngle = -45;

                phy.SetVehicleWheelSteering(car_ID, flw, steerAngle);
                phy.SetVehicleWheelSteering(car_ID, frw, steerAngle);
            }
            else
            {
                if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHTARROW))
                {
                    steerAngle += 5;
                    if (steerAngle > 45)
                        steerAngle = 45;

                    phy.SetVehicleWheelSteering(car_ID, flw, steerAngle);
                    phy.SetVehicleWheelSteering(car_ID, frw, steerAngle);
                }
                else
                {
                    steerAngle = 0;

                    phy.SetVehicleWheelSteering(car_ID, flw, steerAngle);
                    phy.SetVehicleWheelSteering(car_ID, frw, steerAngle);
                }
            }

            //Car Handbrake
            if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHTCONTROL))
            {
                phy.VehicleWheelHandBrake(car_ID, rlw, 1, 2000);
                phy.VehicleWheelHandBrake(car_ID, rrw, 1, 2000);
            }

            //if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_P))
            //{
            //    phy.VehicleReset(car_ID);
            //    phy.SetBodyRotation(pbi_chassis, 0, 0, 0);
            //    phy.SetBodyPosition(pbi_chassis,
            //        phy.GetBodyPosition(pbi_chassis).x,
            //        phy.GetBodyPosition(pbi_chassis).y + 0.2f,
            //        phy.GetBodyPosition(pbi_chassis).z);
            //}

            //if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_O))
            //{
            //    phy.VehicleReset(car_ID);
            //    phy.SetBodyPosition(pbi_chassis, 0f, 5, 0f);
            //    phy.SetBodyRotation(pbi_chassis, 0f, 0f, 0f);
            //}


        }

        public void DrawVehicle()
        {


            m_fl.Render();
            m_fr.Render();
            m_rl.Render();
            m_rr.Render();

        }


        public void SaveUnit()
        {


        }

    }
}

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 | Mobile
Web02 | 2.8.140821.2 | Last Updated 20 Nov 2009
Article Copyright 2009 by Member 3873633
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid