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

using

FarseerPhysics;
 

 

using

FarseerPhysics.Dynamics;
 

 

using

XamlPhysics.WPF;
 

 

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))
			return;

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

		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;
		Body.Box.World.AddJoint(MouseJoint);

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

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

		e.Handled = true;
		CaptureTouch(e.TouchDevice);
	}

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

		dynamic Connection = Connections[e.TouchDevice.Id];
		Connections.Remove(e.TouchDevice.Id);

		Connection.Body.Box.World.RemoveJoint(Connection.MouseJoint);
		Connection.Body.Box.World.RemoveJoint(Connection.AngleJoint);

		e.Handled = true;
		ReleaseTouchCapture(e.TouchDevice);
	}

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

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

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

		Debug.WriteLine(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

http://www.developerfusion.com/tools/convert/vb-to-csharp/

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"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:physics="clr-namespace:XamlPhysics.WPF;assembly=XamlPhysics.WPF">
    <Grid>
        <physics:PhysicalBox x:Name="UxBox">
            <physics:PhysicalBox.Clock>
                <physics:GameLoop />
            </physics:PhysicalBox.Clock>
            <physics:TouchDrag Width="1024" Height="768">
                <Rectangle Canvas.Left="80" Canvas.Top="40" Width="100" Height="100" Fill="DarkRed">
                    <physics:PhysicalBox.Body>
                        <physics:RectangleBody />
                    </physics:PhysicalBox.Body>
                </Rectangle>
            </physics:TouchDrag>
        </physics:PhysicalBox>
    </Grid>
</UserControl>

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.