Пример #1
0
        public Character(DiscreteDynamicsWorld world, Vehiculo vehiculo, TGCVector3 position, float rotation, GameModel gameModel)
        {
            //Cargar sonido
            turboSound = new TgcStaticSound();
            turboSound.loadSound(Game.Default.MediaDirectory + Game.Default.FXDirectory + "turbo.wav", gameModel.DirectSound.DsDevice);

            this.vehiculo = vehiculo;

            var loader = new TgcSceneLoader();

            this.mesh  = loader.loadSceneFromFile(vehiculo.ChassisXmlPath).Meshes[0];
            this.wheel = loader.loadSceneFromFile(vehiculo.WheelsXmlPath).Meshes[0];

            Vehiculo.ChangeTextureColor(this.mesh, vehiculo.Color);

            this.mesh.AutoTransform  = false;
            this.wheel.AutoTransform = false;

            maxHitPoints         = float.Parse(mesh.UserProperties["maxHitPoints"], CultureInfo.InvariantCulture);
            engineForce          = -float.Parse(mesh.UserProperties["engineForce"], CultureInfo.InvariantCulture);
            brakeForce           = float.Parse(mesh.UserProperties["brakeForce"], CultureInfo.InvariantCulture);
            steeringAngle        = -float.Parse(mesh.UserProperties["steeringAngle"], CultureInfo.InvariantCulture);
            turboImpulse         = float.Parse(mesh.UserProperties["turboImpulse"], CultureInfo.InvariantCulture);
            frictionSlip         = float.Parse(mesh.UserProperties["frictionSlip"], CultureInfo.InvariantCulture);
            rollInfluence        = float.Parse(mesh.UserProperties["rollInfluence"], CultureInfo.InvariantCulture);
            rearWheelsHeight     = float.Parse(mesh.UserProperties["rearWheelsHeight"], CultureInfo.InvariantCulture);
            frontWheelsHeight    = float.Parse(mesh.UserProperties["frontWheelsHeight"], CultureInfo.InvariantCulture);
            suspensionRestLength = float.Parse(mesh.UserProperties["suspensionRestLength"], CultureInfo.InvariantCulture);
            suspensionStiffness  = float.Parse(mesh.UserProperties["suspensionStiffness"], CultureInfo.InvariantCulture);
            dampingCompression   = float.Parse(mesh.UserProperties["dampingCompression"], CultureInfo.InvariantCulture);
            dampingRelaxation    = float.Parse(mesh.UserProperties["dampingRelaxation"], CultureInfo.InvariantCulture);

            meshAxisRadius = this.mesh.BoundingBox.calculateAxisRadius().ToBsVector;
            var wheelRadius = this.wheel.BoundingBox.calculateAxisRadius().Y;

            //The btBoxShape is centered at the origin
            CollisionShape chassisShape = new BoxShape(meshAxisRadius.X, meshRealHeight, meshAxisRadius.Z);

            //A compound shape is used so we can easily shift the center of gravity of our vehicle to its bottom
            //This is needed to make our vehicle more stable
            CompoundShape compound = new CompoundShape();

            //The center of gravity of the compound shape is the origin. When we add a rigidbody to the compound shape
            //it's center of gravity does not change. This way we can add the chassis rigidbody one unit above our center of gravity
            //keeping it under our chassis, and not in the middle of it
            var localTransform = Matrix.Translation(0, (meshAxisRadius.Y * 1.75f) - (meshRealHeight / 2f), 0);

            compound.AddChildShape(localTransform, chassisShape);
            //Creates a rigid body
            this.rigidBody = CreateChassisRigidBodyFromShape(compound, position, rotation);

            //Adds the vehicle chassis to the world
            world.AddRigidBody(this.rigidBody);
            worldID = world.CollisionObjectArray.IndexOf(this.rigidBody);

            //RaycastVehicle
            DefaultVehicleRaycaster vehicleRayCaster = new DefaultVehicleRaycaster(world);
            VehicleTuning           tuning           = new VehicleTuning();

            //Creates a new instance of the raycast vehicle
            vehicle = new RaycastVehicle(tuning, this.rigidBody, vehicleRayCaster);

            //Never deactivate the vehicle
            this.rigidBody.ActivationState = ActivationState.DisableDeactivation;

            //Adds the vehicle to the world
            world.AddAction(vehicle);

            //Adds the wheels to the vehicle
            AddWheels(meshAxisRadius, vehicle, tuning, wheelRadius);

            //Inicializo puntos
            hitPoints       = maxHitPoints;
            specialPoints   = maxSpecialPoints;
            timerMachineGun = 0f;
        }