예제 #1
0
        private void PositionObstacle(PhysicsSprite obstacle)
        {
            float newX, newY;


            int minX = Convert.ToInt32(_body.Position.X - 350);
            int maxX = Convert.ToInt32(_body.Position.X + 400);
            int minY = Convert.ToInt32(_body.Position.Y + 200);
            int maxY = Convert.ToInt32(_body.Position.Y + 2000);

            newX = _rand.Next(minX, maxX);
            newY = _rand.Next(minY, maxY);

            Vector2 newPos = new Vector2(newX, newY);

            // make sure we're not too close to another obstacle
            bool bTooClose = false;

            foreach (PhysicsSprite obstacleCheck in _obstacles)
            {
                if (obstacleCheck != obstacle && DistanceBetweenPoints(newPos, obstacleCheck.BodyObject.Position) < 200)
                {
                    bTooClose = true;
                    break;
                }
            }

            if (!bTooClose)
            {
                obstacle.BodyObject.Position = newPos;
            }
        }
예제 #2
0
        void dialog_Closed(object sender, EventArgs e)
        {
            _lostTheBall = null;
            PhysicsSprite ball = _physicsController.PhysicsObjects["ellBall"];

            ball.BodyObject.Position = new Vector2(460, 430);
        }
예제 #3
0
        private void LayoutRootManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            BehaviorCollection behaviorCollection = Interaction.GetBehaviors(_finger);

            if (behaviorCollection.Count == 0)
            {
                behaviorCollection.Add(new PhysicsObjectBehavior
                {
                    BoundaryElement = "finger", IsStatic = true, IsSensor = true
                });
                _physicsController.AddPhysicsBody(
                    _finger.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);
            }


            PhysicsSprite finger = _physicsController.PhysicsObjects["finger"];


            finger.Collision += FingerCollision;
            finger.BodyObject.OnSeparation += BodyObject_OnSeparation;

            //finger phys object already created
            if (finger != null)
            {
                finger.Position = new Vector2((float)(e.ManipulationOrigin.X),
                                              (float)(e.ManipulationOrigin.Y));
            }



            e.Handled = true;
        }
예제 #4
0
        private void SetupDistanceJoint(object sender)
        {
            //remove old joints
            if (_joints.Count > 0)
            {
                foreach (DistanceJoint jnt in _joints)
                {
                    _physicsController.DeleteObject(jnt);
                }
            }

            const float breakpoint = 1000f;

            //translate coordinates relative to screen
            var touchedObject = sender as PhysicsSprite;
            //var list = BoundaryHelper.GetPointsForElement(touchedObject.uiElement, e.ManipulationContainer, false);

            //get reference to player
            PhysicsSprite player = _physicsController.PhysicsObjects["player"];


            //create distance joint between the two
            DistanceJoint joint = JointFactory.CreateDistanceJoint(_physicsController.Simulator, player.BodyObject,
                                                                   touchedObject.BodyObject, Vector2.Zero, Vector2.Zero);

            joint.Frequency        = 4.0f;
            joint.DampingRatio     = .5f;
            joint.Breakpoint       = breakpoint;
            joint.CollideConnected = true;
            joint.Broke           += joint_Broke;
            _joints.Add(joint);

            //create tounge


            //timer
            var timer = new DispatcherTimer {
                Interval = TimeSpan.FromMilliseconds(33)
            };

            timer.Tick += delegate
            {
                //joint broke
                if (!joint.Enabled)
                {
                    timer.Stop();
                    _physicsController.DeleteObject(joint);
                    return;
                }

                //reduce distance
                if (joint.Length <= 0f)
                {
                    timer.Stop();
                    _physicsController.DeleteObject(joint);
                }
                joint.Length -= .1f;
            };
            timer.Start();
        }
예제 #5
0
        private void PhysicsControllerInitialized(object source)
        {
            _player = _physicsController.PhysicsObjects["player"];
            _physicsController.TimerLoop += PhysicsControllerTimerLoop;



            //var branch = _physicsController.PhysicsObjects["branch"];
            //branch.DoubleTap += BranchDoubleTap;
        }
예제 #6
0
        void _physicsController_TimerLoop(object source)
        {
            // here you can handle logic per "frame"

            // NOTE that you can get a reference to the Fluid Container like so:
            FluidContainerMain fluidContainerMain = rectWater.GetValue(FluidContainerMain.FluidControllerProperty) as FluidContainerMain;

            // NOTE that you can get references to "sprite" objects in the simulation like so:
            PhysicsSprite astronaut      = _physicsController.PhysicsObjects["spaceSuit"];
            float         mass           = astronaut.BodyObject.Mass;
            int           collisionGroup = astronaut.GeometryObject.CollisionGroup;
        }
예제 #7
0
        private void FingerCollision(PhysicsSprite source, string collidedWith)
        {
            Debug.WriteLine(collidedWith);
            if (collidedWith != "player")
            {
                return;
            }
            _touchingPlayer = true;
            //Debug.WriteLine("touchingPlayer");


            target.Stroke = new SolidColorBrush(Colors.Red);
            target.SetValue(Canvas.TopProperty, _player.Position.Y - (target.Height / 2));
            target.SetValue(Canvas.LeftProperty, _player.Position.X - (target.Width / 2));
        }
예제 #8
0
        private void physicsController1_Initialized(object source)
        {
            _body = physicsController1.PhysicsObjects["body"].BodyObject;

            // create a bunch of obstacles
            for (int i = 0; i < _numObstacles; i++)
            {
                ucObstacle obstacle = new ucObstacle();
                LayoutRoot.Children.Add(obstacle);
                physicsController1.AddPhysicsBody(obstacle);

                string        newObstacleName = "ball_" + (i + 1).ToString();
                PhysicsSprite newObstacle     = physicsController1.PhysicsObjects[newObstacleName];

                _obstacles.Add(newObstacle);
            }
        }
예제 #9
0
        private void LayoutRootManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            PhysicsSprite finger = _physicsController.PhysicsObjects["finger"];


            double x = finger.Position.X + e.DeltaManipulation.Translation.X;
            double y = finger.Position.Y + e.DeltaManipulation.Translation.Y;

            finger.Position = new Vector2((float)x,
                                          (float)y);
            _startDrag = finger.Position;


            if (_dragLine == null)
            {
                _dragLine = new Line
                {
                    Stroke          = new SolidColorBrush(Color.FromArgb(255, 254, 192, 200)),
                    StrokeThickness = 4,
                };
            }

            //set new line properties
            _dragLine.StrokeThickness = 2;
            _dragLine.X1   = _startDrag.X;
            _dragLine.X2   = _startDrag.X - e.DeltaManipulation.Translation.X;
            _dragLine.Y1   = _startDrag.Y;
            _dragLine.Y2   = _startDrag.Y - e.DeltaManipulation.Translation.Y;
            _dragLine.Name = "line";

            if (!LayoutRoot.Children.Contains(_dragLine))
            {
                LayoutRoot.Children.Add(_dragLine);
            }



            e.Handled = true;
        }
예제 #10
0
        protected override void LoadContent()
        {
            IsMouseVisible = true;
            spriteBatch    = new SpriteBatch(GraphicsDevice);
            testSprite     = new Sprite(Content.Load <Texture2D>("WhiteCircle100x100"), new Vector2(100, 100), Color.White);
            coords         = new TextSprite(Content.Load <SpriteFont>("DebugFont"), "", new Vector2(0, 0), Color.Red);
            text           = new TextSprite(Content.Load <SpriteFont>("DebugFont"), "Tasdasd", new Vector2(200, 200), Color.White);
            speed          = new Vector2(5, 5);
            testSprite.SetOriginToCenter();

            world = new World(new Vector2(0, 9.8f));

            circle = new PhysicsSprite(Content.Load <Texture2D>("WhiteCircle100x100"), Vector2.Zero, Color.Red, world);
            circle.Body.BodyType     = BodyType.Dynamic;
            circle.PixelTexture      = Content.Load <Texture2D>("pixel");
            circle.Body.OnCollision += Body_OnCollision;

            platform1 = new PhysicsSprite(Content.Load <Texture2D>("WhiteBar700x15"), new Vector2(0, 400), Color.White, world);
            platform1.Body.BodyType     = BodyType.Static;
            platform1.Body.CollidesWith = Category.Cat1;

            //text.Scale = new Vector2(2, 2);
        }
예제 #11
0
파일: MainScreen.cs 프로젝트: bsimser/cdx
        private void InitializeCrate()
        {
            // Create the crate as a sprite for display
            var sprite = new PhysicsSprite(ScreenManager.ContentManager.Load <Texture2D>("Sprites/crate"))
            {
                X      = _random.Next(-400, 400),
                Y      = -512,
                Width  = _random.Next(40, 80),
                Height = _random.Next(40, 80),
                Layer  = 1,
            };

            // Create a crate to fall
            var crate = BodyFactory.CreateRectangle(World, ConvertUnits.ToSimUnits(sprite.Width),
                                                    ConvertUnits.ToSimUnits(sprite.Height), 0.8f);

            // Give it a little spin
            crate.Rotation = MathHelper.ToRadians(_random.Next(0, 45));

            // And make it a little rough
            crate.Friction = 0.3f;

            // Add some bounce
            crate.Restitution = 0.6f;

            // Make it non-static so it interacts with other objects
            crate.BodyType = BodyType.Dynamic;

            // Drop the crate from somewhere above the screen
            crate.Position = new Vector2(ConvertUnits.ToSimUnits(_random.Next(-ScreenManager.GraphicsDevice.Viewport.Width / 2, ScreenManager.GraphicsDevice.Viewport.Width / 2)),
                                         ConvertUnits.ToSimUnits(-ScreenManager.GraphicsDevice.Viewport.Height));

            // Finally attach it to our sprite
            sprite.AttachBody(crate);

            _sprites.Add(sprite);
        }
예제 #12
0
        private void SetupReleaseTarget(object sender, PhysicsControllerMain physMain, ManipulationCompletedEventArgs e)
        {
            //remove old joints
            if (_joints.Count > 0)
            {
                foreach (DistanceJoint jnt in _joints)
                {
                    physMain.DeleteObject(jnt);
                }
            }

            const float breakpoint = 1000f;

            //translate coordinates relative to screen
            var touchedObject = sender as PhysicsSprite;
            //var list = BoundaryHelper.GetPointsForElement(touchedObject.uiElement, e.ManipulationContainer, false);

            //get reference to player
            PhysicsSprite player = physMain.PhysicsObjects["player"];


            var letgopoint = new Point((int)e.ManipulationOrigin.X + (int)e.TotalManipulation.Translation.X,
                                       (int)e.ManipulationOrigin.Y + (int)e.TotalManipulation.Translation.Y);

            Body testBody = BodyFactory.CreateCircle(physMain.Simulator, 25, 1, physMain.ScreenToWorld(letgopoint));

            //create distance joint between the two
            DistanceJoint joint = JointFactory.CreateDistanceJoint(physMain.Simulator, player.BodyObject,
                                                                   testBody, Vector2.Zero, Vector2.Zero);

            joint.Frequency        = 4.0f;
            joint.DampingRatio     = .5f;
            joint.Breakpoint       = breakpoint;
            joint.CollideConnected = true;
            joint.Broke           += joint_Broke;
            _joints.Add(joint);

            //create tounge


            //timer
            var timer = new DispatcherTimer {
                Interval = TimeSpan.FromMilliseconds(33)
            };

            timer.Tick += delegate
            {
                //joint broke
                if (!joint.Enabled)
                {
                    timer.Stop();
                    physMain.DeleteObject(joint);
                    return;
                }

                //reduce distance
                if (joint.Length <= 0f)
                {
                    timer.Stop();
                }
                joint.Length -= .1f;
            };
            timer.Start();
        }