Example #1
0
        private void _tracker_BallUpdate(object sender, BallUpdateEventArgs e)
        {
            if (ObjectMotion == null)
            {
                return;
            }

            foreach (GameObject obj in _objects.Where(x => x.ControlledObject == true).ToList())
            {
                Vector3D motionVector = e.PositionVector - obj.Position;
                if (motionVector.Length < this.ControlDeadZone)
                {
                    continue;
                }

                switch (_game.ControlType)
                {
                case ControlType.Relative:
                    if (motionVector.Length > MaxSpeed)
                    {
                        motionVector = motionVector * (MaxSpeed / motionVector.Length);
                    }

                    obj.Position = obj.Position + motionVector;
                    ObjectMotion(this, new ObjectEventArgs(obj));
                    break;

                default:
                    obj.Position = obj.Position + motionVector;
                    ObjectMotion(this, new ObjectEventArgs(obj));
                    break;
                }
            }

            if (TrackingUpdateReceived != null)
            {
                TrackingUpdateReceived(this, new EventArgs());
            }
        }
Example #2
0
        void _capture_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            if (!lastFrameProcessed)
            {
                return;
            }

            if (!BallTracking(eventArgs.Frame))
            {
                return;
            }

            lastFrameProcessed = false;

            if (_rectangles.Length > 0 && BallUpdate != null)
            {
                Rectangle ballRectangle;
                var       args = new BallUpdateEventArgs();

                if (_rectangles.Length > 1)
                {
                    ballRectangle = FindBall();
                }
                else
                {
                    ballRectangle = _rectangles[0];
                }

                _blobCenter.X = ballRectangle.X + (ballRectangle.Width / 2);
                _blobCenter.Y = ballRectangle.Y + (ballRectangle.Height / 2);

                int halfWidth  = (int)(.5 * _trackingImage.Width);
                int halfHeight = (int)(.5 * _trackingImage.Height);

                int Xpct = (int)(100 * (_blobCenter.X - halfWidth) / halfWidth);
                int Ypct = (int)(100 * (_blobCenter.Y - halfHeight) / halfHeight);

                args.PositionVector = new System.Windows.Media.Media3D.Vector3D()
                {
                    X = Xpct, Y = Ypct, Z = 0
                };

                if (Xpct < 99 && Ypct < 99 && Xpct > -99 && Ypct > -99)
                {// 100% in any direction and we're assuming we've lost the ball
                    if (_lastUpdate != null)
                    {
                        if (Math.Abs((_lastUpdate.PositionVector - args.PositionVector).Length) < OUTLIER_LENGTH)
                        {
                            _lastUpdate = args;
                            BallUpdate(this, args);
                            _outlier = null;
                        }
                        else
                        {
                            if (_outlier == null)
                            {
                                _outlier = args;
                            }
                            else
                            {
                                if (_outlier != null && (args.PositionVector - _outlier.PositionVector).Length < OUTLIER_LENGTH)
                                {
                                    _lastUpdate = args;
                                    BallUpdate(this, args);
                                    _outlier = null;
                                }
                                else
                                {
                                    _outlier = args;
                                }
                            }
                        }
                    }
                    else
                    {
                        _lastUpdate = args;
                        BallUpdate(this, args);
                    }
                }
            }

            lastFrameProcessed = true;
        }