Esempio n. 1
0
        public unsafe P5State GetState()
        {
            P5State state = new P5State(); ;

            NativeMethods.GetP5State(p5Handle.DangerousGetHandle(), &state);

            return state;
        }
Esempio n. 2
0
 public unsafe P5State GetState()
 {
     P5State state = new P5State(); ;
     try
     {
         NativeMethods.GetP5State(p5Handle.DangerousGetHandle(), &state);
     }
     catch (Exception e)
     {
         //this will most likely throw when no hand device connected
     }
     return state;
 }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override unsafe void Draw(GameTime gameTime)
        {
            projMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                (float)GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height, 1.0f, 1000.0f);

            //cameraPosition = Vector3.Transform(new Vector3(0, 0, -350), Matrix.CreateRotationY(gState.X / 1000f * (float)Math.PI));
            cameraPosition = new Vector3(0, 0, -350);
            cameraUp = new Vector3(0, 1, 0);
            cameraTarget = new Vector3(0, 0, 0);

            viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, cameraUp);
            vpMatrix = (Matrix.Identity * viewMatrix) * projMatrix;


            GraphicsDevice.Clear(Color.Black);

            DrawSphere(spherePosition);

            DrawParticles();

            lastEnergyComputed += gameTime.ElapsedGameTime;

            if (lastEnergyComputed >= TimeSpan.FromSeconds(1.0))
            {
                totalEnergy = 0f;
                for (int i = 0; i < particles.Count; ++i)
                {
                    totalEnergy += particles[i].Mass * (particles[i].Velocity.LengthSquared()) / 2;
                }
                lastEnergyComputed = TimeSpan.Zero;
                if (useGlove)
                    gloveSecState = glove.GetState();
            }


            var vp = GraphicsDevice.Viewport;
            var gs = gloveSecState;

            spriteBatch.Begin();
            spriteBatch.DrawString(font, "Liczba cz¹stek: " + particles.Count.ToString(), new Vector2(5, 5), Color.WhiteSmoke);
            spriteBatch.DrawString(font, string.Format("Energia: {0,8:#0.00} J", totalEnergy), new Vector2(5, 26), totalEnergy > 60000f ? Color.Red : Color.CornflowerBlue);

            if (showHelp)
            {
                spriteBatch.DrawString(font, @"Poruszaj rêkawic¹ aby przemieœciæ manipulator tachionowy
Zaciœnij piêœæ aby wprowadziæ manipulator tachionowy w tryb implozji
Wyprostuj palce aby manipulator tachionowy przeszed³ w tryb eksplozji
F - pe³en ekran
R - reset wszechœwiata
Przycisk A - reset manipulatora tachionowego
Przycisk B - reset wszechœwiata", new Vector2(5, vp.Height - 140), Color.WhiteSmoke);
            }
            else
            {
                spriteBatch.DrawString(font, "[F1] - Pomoc", new Vector2(5, vp.Height - 20), Color.WhiteSmoke);
            }

            //spriteBatch.DrawString(font, vp.Width + " x " + vp.Height, new Vector2(5, 47), Color.WhiteSmoke);

            //if (useGlove)
            //{
            //    spriteBatch.DrawString(font, string.Format("{0,4:#0} {1,4:#0} {2,4:#0}", gs.X, gs.Y, gs.Z), new Vector2(5, 68), Color.WhiteSmoke);
            //    spriteBatch.DrawString(font, string.Format("{0,4:#0} {1,4:#0} {2,4:#0}", gs.Yaw, gs.Pitch, gs.Roll), new Vector2(5, 89), Color.WhiteSmoke);
            //    spriteBatch.DrawString(font, string.Format("{0,4}", gs.BendSensorData[1]), new Vector2(5, 110), Color.WhiteSmoke);
            //}

            spriteBatch.End();

            base.Draw(gameTime);
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override unsafe void Update(GameTime gameTime)
        {
            prevKeys = keys;
            prevMouse = mouse;
            mouse = Mouse.GetState();
            keys = Keyboard.GetState();

            P5State gState = new P5State();

            // Allows the game to exit
            if (keys.IsKeyDown(Keys.Escape))
                this.Exit();

            if (WasKeyPressed(Keys.R))
            {
                if (useGlove)
                    glove.ResetSensors();

                ResetParticles();

            }

            if (WasKeyPressed(Keys.F1))
                showHelp = !showHelp;

            if (WasKeyPressed(Keys.F))
            {
                if (graphics.IsFullScreen)
                {
                    graphics.PreferredBackBufferHeight = 600;
                    graphics.PreferredBackBufferWidth = 800;
                }
                else
                {
                    graphics.PreferredBackBufferWidth = graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Width;
                    graphics.PreferredBackBufferHeight = graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Height;
                }

                graphics.ToggleFullScreen();
            }

            int sensorStrength;
            Vector3 newSpherePosition;

            if (useGlove)
            {
                gState = glove.GetState();

                if (gState.ButtonsData[0] > 0)
                {
                    glove.ResetSensors();
                }

                if (gState.ButtonsData[1] > 0)
                {
                    ResetParticles();
                }

                sensorStrength = 0;
                for (int i = 1; i < 5; ++i)
                    sensorStrength += gState.BendSensorData[i];

                sphereSign = -MathHelper.Clamp(((float)sensorStrength - (32f * 4f)) / (32f * 3), -1f, 1f);
                newSpherePosition = Vector3.Lerp(spherePosition, new Vector3(gState.X / 5f, gState.Y / 5f, gState.Z / 5f), 0.1f);
                yprQ = Quaternion.CreateFromYawPitchRoll(gState.Yaw, gState.Pitch, gState.Roll);
            }
            else
            {
                sphereSign = 0f;
                int div = keys.IsKeyDown(Keys.LeftShift) ? 2 : 1;
                if (mouse.RightButton == ButtonState.Pressed)
                    sphereSign = 1f / div;
                else if (mouse.LeftButton == ButtonState.Pressed)
                    sphereSign = -1f / div;

                int xMouse = mouse.X - prevMouse.X;
                int yMouse = mouse.Y - prevMouse.Y;

                newSpherePosition = new Vector3(spherePosition.X + xMouse / 2, spherePosition.Y - yMouse / 2, spherePosition.Z);

                float yaw = keys.IsKeyDown(Keys.T) ? 1 : 0;
                float pitch = keys.IsKeyDown(Keys.Y) ? 1 : 0;
                float roll = keys.IsKeyDown(Keys.U) ? 1 : 0;

                yprQ = Quaternion.CreateFromYawPitchRoll(yaw, pitch, roll);
            }

            deltayprQ = yprQ - yprQPrev;
            yprQPrev = yprQ;

            sphereVelocity = newSpherePosition - spherePosition;
            spherePosition = newSpherePosition;

            stepAggr += gameTime.ElapsedGameTime;

            while (stepAggr > physicsStep)
            {
                for (int i = 0; i < stepsPerCycle; ++i)
                    UpdatePhysics((float)physicsStep.TotalSeconds);
                stepAggr -= physicsStep;

                if (stepAggr > TimeSpan.FromSeconds(2.0))
                    stepAggr = TimeSpan.Zero;
            }

            base.Update(gameTime);
        }
	    private HandData GetHand()
        {
            float posScale = 0.001f;


	        
	        if(Globals.useHandDevice)
            {
                state = p5.GetState();
                byte[] fings = new byte[10];

                unsafe
                {
                    fixed (byte* pointer = state.BendSensorData)
                    {
                        Marshal.Copy(new IntPtr(pointer), fings, 0, 5);
                    }
                }

                Console.Out.WriteLine(fings[0] + " " + fings[1] + " " + fings[2] + " "+ fings[3] + " " + fings[4]); 

                return new HandData(
                            -state.Z * posScale, state.Y * posScale, -state.X * posScale, //pos
                            -state.Yaw, -state.Roll, -state.Pitch,//rot
                            1 - (fings[4] / 64.0f),
                            1 - (fings[3] / 64.0f),
                            1 - (fings[2] / 64.0f),
                            1 - (fings[1] / 64.0f),
                            1 - (fings[0] / 64.0f));
            }
            else
            {
                float[] fings = new float[] { 0, 0, 0, 0, 0 };

                MouseState mouseState = Mouse.GetState();

                if (mouseState.LeftButton == ButtonState.Pressed) fings = new float[] { 1, 1, 1, 1, 1 };

                //fingers injection
                foreach (var pressedKey in Keyboard.GetState().GetPressedKeys())
                {
                    if (pressedKey == Keys.F1) fings = new float[] { 0, 0, 0, 0, 0 };
                    if (pressedKey == Keys.F2) fings = new float[] { 0, 0, 0, 0, 0 };
                    if (pressedKey == Keys.F3) fings = new float[] { 0, 0, 0, 0, 0 };
                    if (pressedKey == Keys.F4) fings = new float[] { 0, 0, 0, 0, 0 };
                }

                return new HandData(
                    (mouseState.X - Globals.screenWidth * 0.5f) * 0.2f * posScale, -(mouseState.Y - Globals.screenHeight * 0.5f) * 0.2f * posScale, -state.Z * posScale, //pos
                    -state.Yaw, -state.Roll, -state.Pitch,//rot
                    fings[0], fings[1], fings[2], fings[3], fings[4]);
            }

            



        }
 internal static unsafe extern void GetP5State(IntPtr p5, P5State* state);
        private HandData GetHand()
        {
            state = p5.GetState();
            return new HandData(
                            -state.Z, state.Y, -state.X, //pos
                            -state.Yaw, -state.Roll, -state.Pitch,//rot
                            0, 0, 0, 0, 0);



        }