Dragging objects in farseer using WP7 swiping

Topics: User Forum
Jun 3, 2012 at 1:42 AM

Hey, so I am trying to this project working that requires swiping the screen to move a farseer body.

Basically the speed of the object must match the swipe speed of the finger. I have read over the Gestures in the TouchPanel and tried to use them but it doesn't seem to work properly. Here's how I am trying to do it:

while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();

                switch (gesture.GestureType)
                {
                    case GestureType.FreeDrag:
                        {
                            m_isDragging = true;
                            break;
                        }
                    case GestureType.DragComplete:
                        {
                            m_isDragging = false;
                            break;
                        }
                }
                HandleDrag(gesture);
            }

public void HandleDrag(GestureSample gesture, float frameTime)
        {
            if (gesture.GestureType == GestureType.FreeDrag)
            {
                if (m_firstGesture == null)
                {
                    m_firstGesture = gesture;
                }

                m_currentGesture = gesture;
                Vector2 delta = m_currentGesture.Value.Position - m_firstGesture.Value.Position;

                if (m_prevGesture != null)
                {
                    Vector2 currentMinusPrev = m_currentGesture.Value.Position - m_prevGesture.Value.Position;
                    if ((delta.X > 0 && currentMinusPrev.X < 0) || (delta.X < 0 && currentMinusPrev.X > 0))
                    {
                        delta.X = 0;
                        m_firstGesture = m_currentGesture;
                    }
                }
//Update velocity of object, but how?
                m_prevGesture = m_currentGesture;
            }
            else if (gesture.GestureType == GestureType.DragComplete)
            {
                ResetDrag();
            }
        }

Jun 6, 2012 at 2:11 PM

What specifically do you mean by it does work properly? What is or isn't happening. 

Not sure this is really a Farseer question as there is no Farseer related code in your listing, maybe try AppHub or other Windows Phone forums if this is purely a gesture related problem.

Jun 11, 2012 at 1:08 AM

Well the body being dragged is a farseer body, so I decided to post here.

What I have is the following:

1) A ball with physics enabled.

2) A polyline level

3) Physics/Collision detection works fine between those

What I want:

Swiping the ball by X amount in the X direction would move the ball by X amount in the X direction WHILE maintaining Y physics and collision physics. Which means that if you swipe the ball right 100 pixels, but after 50 pixels there is a wall, then the ball would just move 50 pixels to the right.

Another example: Say you want the ball to go up a hill to the right, you would swipe to the right and the ball's change in X position would equal to the finger's delta X. So the ball is going up the hill in X direction and also in Y due to the ball's reaction to the slope of the hill (3. physics/collision detection)

I have recently become aware of the TouchPanel drag gesture.Delta() function and making use of it but it doesn't feel right.