コード例 #1
0
 protected override int Read(ref JoystickState state)
 {
   return state.GetForceSliders()[base.Index];
 }
コード例 #2
0
    /// <summary>
    /// Update state of joystick
    /// </summary>
    public void Update()
    {
        //Get the state of the joystick
        JoystickState state = joystick.GetCurrentState();

        //Save prev values for autodetection of axis
        Array.Copy(axis, axisPrev, axis.Length);

        //Update axes values
        axis[0]  = state.AccelerationX;        //Ax
        axis[1]  = state.AccelerationY;        //Ay
        axis[2]  = state.AccelerationZ;        //Az
        axis[3]  = state.AngularAccelerationX; //aAx
        axis[4]  = state.AngularAccelerationY; //aAy
        axis[5]  = state.AngularAccelerationZ; //aAz
        axis[6]  = state.AngularVelocityX;     //aVx
        axis[7]  = state.AngularVelocityY;     //aVy
        axis[8]  = state.AngularVelocityZ;     //aVz
        axis[9]  = state.ForceX;               //Fx
        axis[10] = state.ForceY;               //Fy
        axis[11] = state.ForceZ;               //Fz
        axis[12] = state.RotationX;            //Rx
        axis[13] = state.RotationY;            //Ry
        axis[14] = state.RotationZ;            //Rz
        axis[15] = state.TorqueX;              //Tx
        axis[16] = state.TorqueY;              //Ty
        axis[17] = state.TorqueZ;              //Tz
        axis[18] = state.VelocityX;            //Vx
        axis[19] = state.VelocityY;            //Vy
        axis[20] = state.VelocityZ;            //Vz
        axis[21] = state.X;                    //x
        axis[22] = state.Y;                    //y
        axis[23] = state.Z;                    //z

        int asl = state.GetAccelerationSliders().Length;
        int fsl = state.GetForceSliders().Length;
        int vsl = state.GetVelocitySliders().Length;
        int sl  = state.GetSliders().Length;

        Array.Copy(state.GetAccelerationSliders(), 0, axis, 24, asl);
        Array.Copy(state.GetForceSliders(), 0, axis, 24 + asl, fsl);
        Array.Copy(state.GetVelocitySliders(), 0, axis, 24 + asl + fsl, vsl);
        Array.Copy(state.GetForceSliders(), 0, axis, 24 + asl + fsl + vsl, sl);

        //Save previous
        Array.Copy(button, buttonPrev, button.Length);



        //Update button values

        //first, convert powController into boolean array to represent them as buttons
        int[] pov  = state.GetPointOfViewControllers();
        int   povl = pov.Length;

        bool[] povButton = new bool[povl * 4];
        for (int i = 0; i < povl; i += 4)
        {
            int povValue = pov[i / 4];
            if (povValue == -1)
            {
                povButton[i + 0] = false;
                povButton[i + 1] = false;
                povButton[i + 2] = false;
                povButton[i + 3] = false;
                continue;
            }

            //north
            if (povValue >= 31500 || povValue <= 4500)
            {
                povButton[i + 0] = true;
            }

            //east
            if (povValue >= 4500 && povValue <= 13500)
            {
                povButton[i + 1] = true;
            }

            //south
            if (povValue >= 13500 && povValue <= 22500)
            {
                povButton[i + 2] = true;
            }

            //west
            if (povValue >= 22500 && povValue <= 31500)
            {
                povButton[i + 3] = true;
            }
        }

        int bl = state.GetButtons().Length;

        Array.Copy(state.GetButtons(), 0, button, 0, bl);
        Array.Copy(povButton, 0, button, bl, povButton.Length);



        //TEMP for debugging of joystick input

        /*
         * if (povPrev == null)
         * {
         *      aslidersPrev = state.GetAccelerationSliders();
         *      fslidersPrev = state.GetForceSliders();
         *      povPrev = state.GetPointOfViewControllers();
         *      slidersPrev = state.GetSliders();
         *      vslidersPrev = state.GetVelocitySliders();
         * }
         *
         * int[] asliders = state.GetAccelerationSliders();
         * int[] fsliders = state.GetForceSliders();
         * int[] povC = state.GetPointOfViewControllers();
         * int[] sliders = state.GetSliders();
         * int[] vsliders = state.GetVelocitySliders();
         *
         * for(int i = 0; i < asliders.Length; i++)
         * {
         *      if (asliders[i] != aslidersPrev[i])
         *              Console.WriteLine(asliders[i]);
         * }
         * aslidersPrev = asliders;
         *
         * for (int i = 0; i < fsliders.Length; i++)
         * {
         *      if (fsliders[i] != fslidersPrev[i])
         *              Console.WriteLine(fsliders[i]);
         * }
         * fslidersPrev = fsliders;
         *
         * for (int i = 0; i < pov.Length; i++)
         * {
         *      if (povC[i] != povPrev[i])
         *              Console.WriteLine(povC[i]);
         * }
         * povPrev = povC;
         *
         * for (int i = 0; i < sliders.Length; i++)
         * {
         *      if (sliders[i] != slidersPrev[i])
         *              Console.WriteLine(sliders[i]);
         * }
         * slidersPrev = sliders;
         *
         * for (int i = 0; i < vsliders.Length; i++)
         * {
         *      if (vsliders[i] != vslidersPrev[i])
         *              Console.WriteLine(vsliders[i]);
         * }
         * vslidersPrev = vsliders;
         */
    }
コード例 #3
0
 protected override int Read(ref JoystickState state)
 {
     return(state.GetForceSliders()[base.Index]);
 }
コード例 #4
0
ファイル: Game1.cs プロジェクト: mbravod/aeros
        void updateCamera(GameTime gameTime)
        {
            colision_map.load_map();


            JoystickState state = new JoystickState();

            bool[]        buttonData;
            int[]         hats, uv;
            SlimDX.Result r;


            try
            {
                if ((r = joystick.Acquire()).IsFailure)
                {
                    throw new DirectInputException(r);
                }

                if ((joystick.Capabilities.Flags & DeviceFlags.PolledDevice) != 0)
                {
                    if ((r = joystick.Poll()).IsFailure)
                    {
                        throw new DirectInputException(r);
                    }
                }


                state = joystick.GetCurrentState();
                //    state = joystick.GetCurrentState();
                if (SlimDX.Result.Last.IsFailure)
                {
                    throw new DirectInputException(SlimDX.Result.Last);
                }

                buttonData = state.GetButtons();
                hats       = state.GetPointOfViewControllers();
                uv         = state.GetForceSliders();
            }
            catch (DirectInputException e)
            {
                int I = 0;
            }
            if (state.X != 0)
            {
                xval = state.X;
            }
            if (state.Y != 0)
            {
                yval = (-1 * (state.Y - 5000));
            }

            if (state.RotationZ < 1)
            {
                bval = -1 * (state.RotationZ);
            }
            float iangle;

            iangle = (((float)xval)) * 1.4f / 5000.0f / 3.1415f;
            float iaccel;

            iaccel = yval / 100.0f + 1.0f;
            iaccel = (float)Math.Log10((double)iaccel) + 1.0f;
            iaccel = (float)Math.Pow(2, (double)iaccel) - 2;
            float daccel;

            daccel = bval / 100.0f + 1.0f;
            daccel = (float)Math.Log10((double)daccel) + 1.0f;
            daccel = (float)Math.Pow(3, (double)daccel) - 3;

            // Get the new keyboard and mouse state
            Microsoft.Xna.Framework.Input.MouseState    mouseState = Microsoft.Xna.Framework.Input.Mouse.GetState();
            Microsoft.Xna.Framework.Input.KeyboardState keyState   = Microsoft.Xna.Framework.Input.Keyboard.GetState();

            // Determine how much the camera should turn
            float deltaX = ((float)lastMouseState.X - (float)mouseState.X) * 0.02f;
            float deltaY = ((float)lastMouseState.Y - (float)mouseState.Y) * 0.02f;

            // Rotate the camera
            /// ((FreeCamera)camera).Rotate(deltaX * .01f, deltaY * .01f);

            double steer = 0;

            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.I))
            {
                driver_pos = 2.42f;
                mode       = 1;
                dis        = 1.36f;
                yalt       = 1.45f;
                angle      = -2.9115f;
                yxalt      = 1.23f;
                rangle     = -1.45f; // -2.34f;
                langle     = 1.45f;  //2.34f;
            }

            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
            {
                Bus_models[0].position = new Microsoft.Xna.Framework.Vector3(1228.729f, 0.2f, -3246.247f);
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.O))
            {
                mode  = 0;
                dis   = 13.5f;
                yalt  = 6.0f;
                angle = 0;
                yxalt = 2.44f;
            }

            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.P))
            {
                mode  = 0;
                dis   = 4.3f;
                yalt  = 1.2f;
                angle = 4.77f;
                yxalt = 1.4f;
            }

            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D7))
            {
                rangle += .010f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D8))
            {
                rangle -= .010f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D9))
            {
                langle += .0100f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D0))
            {
                langle -= .0100f;                                                             //100 km/hr
            }
            if (mode == 0)
            {
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Y))
                {
                    driver_pos += .010f;
                }
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.U))
                {
                    driver_pos -= .010f;
                }
            }
            else
            {
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Y))
                {
                    driver_pos += .0010f;
                }
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.U))
                {
                    driver_pos -= .0010f;
                }
            }

            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D5))
            {
                driver_posx += .0010f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D6))
            {
                driver_posx -= .0010f;
            }


            // Determine in which direction to move the camera
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Z))
            {
                dis += .010f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.X))
            {
                dis -= .010f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.C))
            {
                dis += .0200f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.V))
            {
                dis -= .0200f;                                                            //100 km/hr
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Q))
            {
                yalt += 5.0f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.W))
            {
                yalt += .010f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S))
            {
                yalt -= .010f;
            }
            if (mode == 0)
            {
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A))
                {
                    angle += .02f;
                }
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D))
                {
                    angle -= .02f;
                }
            }
            else
            {
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D))
                {
                    angle += .01f;
                }
                if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A))
                {
                    angle -= .01f;
                }
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.G))
            {
                yxalt -= 0.010f;
            }
            if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.T))
            {
                yxalt += 0.010f;
            }


            Bus_models[0].ProcessKeyboard(gameTime, iangle, iaccel, daccel);
            Bus_models[0].Update(gameTime);
            Bus_models[0].doit();

            if (mode == 1)
            {
                tempbus_Position_I = new Microsoft.Xna.Framework.Vector3((float)(Bus_models[0].Position.X - driver_pos * Math.Sin(Bus_models[0].Rotation.Y) - driver_posx * Math.Cos(Bus_models[0].Rotation.Y)), yalt, (float)(Bus_models[0].Position.Z - driver_pos * Math.Cos(Bus_models[0].Rotation.Y) + driver_posx * Math.Sin(Bus_models[0].Rotation.Y)));
                tempbus_Position_C = new Microsoft.Xna.Framework.Vector3((float)(Bus_models[0].Position.X - driver_pos * Math.Sin(Bus_models[0].Rotation.Y) - driver_posx * Math.Cos(Bus_models[0].Rotation.Y)), yalt, (float)(Bus_models[0].Position.Z - driver_pos * Math.Cos(Bus_models[0].Rotation.Y) + driver_posx * Math.Sin(Bus_models[0].Rotation.Y)));
                tempbus_Position_D = new Microsoft.Xna.Framework.Vector3((float)(Bus_models[0].Position.X - driver_pos * Math.Sin(Bus_models[0].Rotation.Y) - driver_posx * Math.Cos(Bus_models[0].Rotation.Y)), yalt, (float)(Bus_models[0].Position.Z - driver_pos * Math.Cos(Bus_models[0].Rotation.Y) + driver_posx * Math.Sin(Bus_models[0].Rotation.Y)));

                // ((TargetCamera)camera).Position = target;
                //  ((TargetCamera)camera).Target = new Vector3(car_position.X, yxalt, car_position.Z);
                tempbus_target_I = new Microsoft.Xna.Framework.Vector3((float)(tempbus_Position_I.X - dis * (Math.Sin(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle + langle))), yalt - (yalt - yxalt) * .6f, (float)(tempbus_Position_I.Z - dis * (Math.Cos(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle + langle))));
                tempbus_target_C = new Microsoft.Xna.Framework.Vector3((float)(tempbus_Position_I.X - dis * (Math.Sin(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle))), yxalt, (float)(tempbus_Position_I.Z - dis * (Math.Cos(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle))));
                tempbus_target_D = new Microsoft.Xna.Framework.Vector3((float)(tempbus_Position_I.X - dis * (Math.Sin(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle + rangle))), yalt - (yalt - yxalt) * .3f, (float)(tempbus_Position_I.Z - dis * (Math.Cos(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle + rangle))));
            }


            if (mode == 0)
            {
                tempbus_target_I = new Microsoft.Xna.Framework.Vector3((float)(Bus_models[0].Position.X - driver_pos * Math.Sin(Bus_models[0].Rotation.Y) - 0.3 * Math.Cos(Bus_models[0].Rotation.Y)), yxalt, (float)(Bus_models[0].Position.Z - driver_pos * Math.Cos(Bus_models[0].Rotation.Y) + .3f * Math.Sin(Bus_models[0].Rotation.Y)));
                tempbus_target_C = new Microsoft.Xna.Framework.Vector3((float)(Bus_models[0].Position.X - driver_pos * Math.Sin(Bus_models[0].Rotation.Y) - 0.3 * Math.Cos(Bus_models[0].Rotation.Y)), yxalt, (float)(Bus_models[0].Position.Z - driver_pos * Math.Cos(Bus_models[0].Rotation.Y) + .3f * Math.Sin(Bus_models[0].Rotation.Y)));
                tempbus_target_D = new Microsoft.Xna.Framework.Vector3((float)(Bus_models[0].Position.X - driver_pos * Math.Sin(Bus_models[0].Rotation.Y) - 0.3 * Math.Cos(Bus_models[0].Rotation.Y)), yxalt, (float)(Bus_models[0].Position.Z - driver_pos * Math.Cos(Bus_models[0].Rotation.Y) + .3f * Math.Sin(Bus_models[0].Rotation.Y)));

                tempbus_Position_I = new Microsoft.Xna.Framework.Vector3((float)(tempbus_target_I.X - dis * (Math.Sin(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle - 1.57f))), yalt, (float)(tempbus_target_I.Z - dis * (Math.Cos(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle - 1.57f))));
                tempbus_Position_C = new Microsoft.Xna.Framework.Vector3((float)(tempbus_target_C.X - dis * (Math.Sin(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle))), yalt, (float)(tempbus_target_C.Z - dis * (Math.Cos(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle))));
                tempbus_Position_D = new Microsoft.Xna.Framework.Vector3((float)(tempbus_target_D.X - dis * (Math.Sin(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle + 1.57f))), yalt, (float)(tempbus_target_D.Z - dis * (Math.Cos(Bus_models[0].Rotation.Y + MathHelper.ToRadians(180) - angle + 1.57f))));
            }



            double fps = (1000 / gameTime.ElapsedGameTime.TotalMilliseconds);

            fps = Math.Round(fps, 0);

            this.Window.Title = " pos " + roadcode + " th " + BusModel.last_theta + " LF " + maps_loaded + " Distance " + dis + "," + fps.ToString() + " FPS";
            // Update the mouse state
            lastMouseState = mouseState;
        }