# How can I measure the velocity of a drag gesture?

I want to measure the velocity of a drag(FreeDrag, VerticalDrag, HorizontalDrag) gesture on my Windows Phone. For example, if the measured velocity of a drag is 40 km/h, the player will shoot a bullet in the dragging direction that flies with 40 km/h.

How can I measure the velocity of a drag gesture on my Windows Phone?

Update: I don't know how to measure the time. Is it possible to do it without touchCollection or should I use touchCollection to determine the StartTime and the EndTime?

I'm not sure if I need the touchCollection block. In addition, I'm not sure if I can use two different types, TimeSpan for StartTime and DateTime for EndTime.

What is the best way to measure the time?

TimeSpan StartTime;
DateTime EndTime;
TouchPanel.EnabledGestures = GestureType.FreeDrag;
protected override void Update(GameTime gameTime)
{
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
if ((tl.State == TouchLocationState.Pressed))
{
StartTime = gs.Timestamp;
}
if ((tl.State == TouchLocationState.Released))
{
EndTime = DateTime.Now;
}
}

while (TouchPanel.IsGestureAvailable)
{
switch (gs.GestureType)
{
case GestureType.FreeDrag:
StartTime = gs.Timestamp;
break;
}
}
base.Update(gameTime);
}


Using the information from your previous question, you know what the delta of the drag was. This gives you distance. The only other thing you need to get velocity is a time. So, record the time when the touch even started, and when it finished. The difference in those times is the time it took for the gesture to complete.

Now you have distance and time. So the velocity is equal to distance/time.

Even though this will work in theory, I think you'll need to test for how well it works in practice. I think you'll find that a 1:1 ratio for swipe speed to bullet speed will be a little hard to control, especially if the user also needs to define a direction to shoot.

• What is the best way to record the time when the touch started and when it finished? I don't know how to do that. I found "Timestamp", it holds the starting time for this touch gesture sample, but not the ending time. GestureSample gs = TouchPanel.ReadGesture(); gs.Timestamp Dec 27 '13 at 10:50
• The end time can be found with DateTime.Now. Get it when you're processing the event.
– House
Dec 27 '13 at 15:15
• I don't know how to measure the time. I updated my question with my code and some informations. Dec 27 '13 at 21:06
• The time the event took is EndTime - StartTime. That'll create a time span, convert that into whatever time units you want.
– House
Dec 27 '13 at 21:20
• Is it possible to get the EndTime and StartTime in the second loop(while loop) or should I use the foreach loop to get the EndTime and StartTime? Dec 27 '13 at 23:26

The quickest/simplest approach is to get the position and time of the touch and then get the position and time of the release. Then dividing the position difference (which is a vector) by the time difference you have an approximation to the velocity.

However, there are more complicated ways to do this which can be desirable in some cases. For example, if you want to make your velocity measurement resistant to jittering/variations in the final position of the pointer, then you can sample the positions of the pointer between the touch and release at regular intervals and then do a linear fit to the pointer position (each component) vs. time. This helps correct for the natural tendency for the hand to create an arch when moving the pointer as well as jittering.

• Note that this is a touch screen, there's no mouse.
– House
Dec 27 '13 at 15:12
• I realized that after posting but the idea is identical; I'll update the answer. Dec 27 '13 at 17:16