Ejemplo n.º 1
0
        override protected void OnUpdateFrame(FrameEventArgs e)
        {
            KeyboardState currentKeyboardState = OpenTK.Input.Keyboard.GetState();

            if (currentKeyboardState[Key.Escape])
            {
                Exit();
            }

            MouseState currentMouseState = OpenTK.Input.Mouse.GetState();
            int        deltaX, deltaY, deltaZ;

            if (currentMouseState != lastMouseState)
            {
                deltaX         = currentMouseState.X - lastMouseState.X;
                deltaY         = currentMouseState.Y - lastMouseState.Y;
                deltaZ         = currentMouseState.Wheel - lastMouseState.Wheel;
                lastMouseState = currentMouseState;
            }
            else
            {
                deltaX = 0;
                deltaY = 0;
                deltaZ = 0;
            }

            float
                xFactor = (float)Math.Sin(-2 * Math.PI / SIZE_X * currentMouseState.X),
                zFactor = (float)Math.Cos(-2 * Math.PI / SIZE_X * currentMouseState.X);

            if (currentKeyboardState[Key.W])
            {
                basePosition.X -= WORLD_MOVE_FRAME_DELTA * xFactor;
                basePosition.Z -= WORLD_MOVE_FRAME_DELTA * zFactor;
            }
            if (currentKeyboardState[Key.S])
            {
                basePosition.X += WORLD_MOVE_FRAME_DELTA * xFactor;
                basePosition.Z += WORLD_MOVE_FRAME_DELTA * zFactor;
            }
            if (currentKeyboardState[Key.A])
            {
                basePosition.X -= WORLD_MOVE_FRAME_DELTA * zFactor;
                basePosition.Z += WORLD_MOVE_FRAME_DELTA * xFactor;
            }
            if (currentKeyboardState[Key.D])
            {
                basePosition.X += WORLD_MOVE_FRAME_DELTA * zFactor;
                basePosition.Z -= WORLD_MOVE_FRAME_DELTA * xFactor;
            }
            if (currentKeyboardState[Key.LShift])
            {
                basePosition.Y += WORLD_MOVE_FRAME_DELTA;
            }
            if (currentKeyboardState[Key.LControl])
            {
                basePosition.Y -= WORLD_MOVE_FRAME_DELTA;
            }

            if (currentKeyboardState[Key.G] && !lastKeyboardState[Key.G])
            {
                bodyManager.Gravity.invert();
            }
            if (currentKeyboardState[Key.R] && !lastKeyboardState[Key.R])
            {
                bodyManager.Gravity.rotate(1);
            }
            if (currentKeyboardState[Key.BackSlash] && !lastKeyboardState[Key.BackSlash])
            {
                squirrelBody.Restitution = 1 - squirrelBody.Restitution;
                dilloBody.Restitution    = 1 - dilloBody.Restitution;
                turtleBody.Restitution   = 1 - turtleBody.Restitution;
            }

            if (currentKeyboardState[Key.Number1])
            {
                //squirrelBody.applyImpulse( Vector3f.getRandom( -10, 10 ) );
                squirrelBody.Velocity.add(Vector3f.getRandom(-10, 10));
            }
            if (currentKeyboardState[Key.Number2])
            {
                //dilloBody.applyImpulse( Vector3f.getRandom( -10, 10 ) );
                dilloBody.Velocity.add(Vector3f.getRandom(-10, 10));
            }
            if (currentKeyboardState[Key.Number3])
            {
                //turtleBody.applyImpulse( Vector3f.getRandom( -10, 10 ) );
                turtleBody.Velocity.add(Vector3f.getRandom(-10, 10));
            }

            if (currentKeyboardState[Key.Enter])
            {
                squirrelBody.Transform.setTranslation(45, BALL_RADIUS, 45);
                squirrelBody.Velocity.set(-15, 0, -15);
            }

            lastKeyboardState = currentKeyboardState;

            time.Value   = (DateTime.Now.Ticks % (100L * 1000 * 1000 * 1000)) / 1E7f;
            random.Value = MathUtils.nextFloat();
            //Console.WriteLine( time[0] );

            if (cameraOnFrame != null)
            {
                cameraOnFrame(modelviewMatrix);
            }
            else
            {
                float radius = currentMouseState.Wheel + MIN_RADIUS;

                Vector3 pos = new Vector3(
                    basePosition.X + xFactor * radius,
                    basePosition.Y + BASE_VIEW_HEIGHT,
                    basePosition.Z + zFactor * radius
                    );
                //Console.WriteLine( pos );

                modelviewMatrix.set(Matrix4.LookAt(pos,
                                                   new Vector3(basePosition.X, BASE_VIEW_HEIGHT + basePosition.Y - radius * currentMouseState.Y / SIZE_Y, basePosition.Z),
                                                   new Vector3(0, 1, 0)));

                //modelviewMatrix.setTranslation( deltaX, deltaY, deltaZ );
            }

            foreach (Mesh m in meshes)
            {
                m.update();
            }

            if (!currentKeyboardState.IsKeyDown(Key.LAlt))
            {
                float TIME_THRESHOLD = 1f / 60f, stepTime = (float)e.Time;
                while (stepTime > TIME_THRESHOLD)
                {
                    bodyManager.update(TIME_THRESHOLD);
                    stepTime -= TIME_THRESHOLD;
                }
                bodyManager.update(stepTime);
            }

            /*
             * ambientColor.Y = (float) Math.Sin( time.Value );
             * Console.WriteLine( ambientColor.Y );
             */
        }