This project has moved and is read-only. For the latest updates, please go here.

Im driving crazy trying make work in C# with touch.

Jan 18, 2012 at 12:01 PM


Please liserdart, can post some example for make a simple object can be moved with touch event.
I trying move object with touch events directly thinking physics is applied but no ; )
How can move a simple object with touch events?

Im learning about multi-touch.

Bets regards.

Jan 18, 2012 at 12:12 PM
Edited Jan 18, 2012 at 12:15 PM










public class TouchDrag : Canvas

	private struct Connection
		public PhysicalBody Body;
		public FarseerPhysics.Dynamics.Joints.FixedMouseJoint MouseJoint;
		public FarseerPhysics.Dynamics.Joints.FixedAngleJoint AngleJoint;

	/// <summary>
	/// The maximum constraint force that can be exerted to move the candidate body.
	/// </summary>
	public double MaxForce { get; set; }

	/// <summary>
	/// The response speed.
	/// </summary>
	public double Frequency { get; set; }

	/// <summary>
	/// The damping ratio. 0 = no damping, 1 = critical damping.
	/// </summary>
	public double DampingRatio { get; set; }

	public float LinearBreakpoint { get; set; }

	public float TorqueBreakpoint { get; set; }

	/// <summary>
	/// If true, a FixedAngleJoint will be created to stop rotation of the touched body
	/// </summary>
	public bool StopRotaion { get; set; }

	Dictionary<int, Connection> Connections = new Dictionary<int, Connection>();
	private void TouchDrag_Down(object sender, Windows.Input.TouchEventArgs e)
		if (Connections.ContainsKey(e.TouchDevice.Id))

		dynamic Body = PhysicalBox.GetBody(e.OriginalSource);
		if (Body == null)
		if (Body.Body == null)

		dynamic TouchPoint = e.TouchDevice.GetTouchPoint(Body.Box);

		FarseerPhysics.Dynamics.Joints.FixedMouseJoint MouseJoint = new FarseerPhysics.Dynamics.Joints.FixedMouseJoint(Body.Body, Body.Box.PointToMeter(TouchPoint.Position));
		MouseJoint.MaxForce = MaxForce;
		MouseJoint.Frequency = Frequency;
		MouseJoint.Breakpoint = LinearBreakpoint;
		MouseJoint.DampingRatio = DampingRatio;

		FarseerPhysics.Dynamics.Joints.FixedAngleJoint AngleJoint = new FarseerPhysics.Dynamics.Joints.FixedAngleJoint(Body.Body);
		AngleJoint.TargetAngle = Body.Body.Rotation;
		AngleJoint.Breakpoint = TorqueBreakpoint;
		if (StopRotaion) {

		Connection Connection = default(Connection);
		Connection.Body = Body;
		Connection.MouseJoint = MouseJoint;
		Connection.AngleJoint = AngleJoint;
		Connections.Add(e.TouchDevice.Id, Connection);

		e.Handled = true;

	private void TouchDrag_Up(object sender, Windows.Input.TouchEventArgs e)
		if (!Connections.ContainsKey(e.TouchDevice.Id))

		dynamic Connection = Connections[e.TouchDevice.Id];


		e.Handled = true;

	private void TouchDrag_Move(object sender, Windows.Input.TouchEventArgs e)
		if (!Connections.ContainsKey(e.TouchDevice.Id))

		dynamic Connection = Connections[e.TouchDevice.Id];

		dynamic TouchPoint = e.TouchDevice.GetTouchPoint(Connection.Body.Box);
		Connection.MouseJoint.WorldAnchorB = Connection.Body.Box.PointToMeter(TouchPoint.Position);

	public TouchDrag()
		TouchMove += TouchDrag_Move;
		TouchUp += TouchDrag_Up;
		TouchDown += TouchDrag_Down;


Jan 18, 2012 at 12:19 PM
Edited Jan 18, 2012 at 12:22 PM

This line return this errors:

dynamic Body = PhysicalBox.GetBody(e.OriginalSource);

Error 79 The best overloaded method match for 'XamlPhysics.WPF.PhysicalBox.GetBody(System.Windows.UIElement)'

Error 80 Argument 1: cannot convert from 'object' to 'System.Windows.UIElement'

I convert to C# using

Also I have to change LinearBreakpoint and TorqueBreakpoint to float, otherwise I have this error:

Error 36 Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)
Really be great have some simple working example of touch for C# :)

Jan 18, 2012 at 8:31 PM

I don't know why you converted that code to C#. It's already in the library ready to use. Here is a simple example.

<UserControl x:Class="Viewer"
        <physics:PhysicalBox x:Name="UxBox">
                <physics:GameLoop />
            <physics:TouchDrag Width="1024" Height="768">
                <Rectangle Canvas.Left="80" Canvas.Top="40" Width="100" Height="100" Fill="DarkRed">
                        <physics:RectangleBody />

Jan 19, 2012 at 5:34 AM
Edited Jan 19, 2012 at 5:40 AM

Ok thank you for your faster reply.

Also I have now a new question, how can obtain the position of the rectangle when is moving in c# code?

I have a problem with animation run with drops, but is fixed now.

UxBox.Clock.UISteps = 1;

This simple change make animation very smooth and good! : )

I want say thank you again for take time to look in this and for your help.
I are very new coders and all this for me is extreme hard.


Jan 19, 2012 at 6:34 AM
Edited Jan 19, 2012 at 11:45 AM

Feb 9, 2012 at 4:31 PM

I have an updated touch-drag class coming... this handles multitouch on the same object better. Also I'm finally able to test the TagDrag class for surface :)

Regarding the position, I usually just call the actual position from the UI element.