Detect click of body

Topics: Developer Forum
Feb 14, 2013 at 10:51 PM
How can I detect if my body has been clicked. Currently I can move my body around when the user drags the object (in this case a ball) , but It does not matter where I click on screen. I only want the ball to move if it was clicked to start with.
My code to move ball is as follows:
while (TouchPanel.IsGestureAvailable)
                {
                    GestureSample gesture = TouchPanel.ReadGesture();

switch (gesture.GestureType)
                        {
                            case GestureType.FreeDrag:
                                {
                                    oldPos = bouncingBall.farseerRectangle.Position;
                                    newPos = ConvertUnits.ToSimUnits(gesture.Position);
                                    break;
                                }
                            case GestureType.DragComplete:
                                {
                                    Vector2 direction = new Vector2();
                                    direction = newPos - oldPos;
                                    direction.Normalize();
                                    bouncingBall.farseerRectangle.ApplyForce(direction * 200);
                                    break;
                                }
                        }
How can I detect if the ball was clicked and dragged.
Feb 15, 2013 at 3:25 PM
The functionality you are looking for is provided by the mousejoint from the farseer samples project.I think it does all you want to do.Just download and programm away or you could just steal the world query part,that is the part where you query the world for a body at that position,all nice and simple.Have fun.

P.S Its generally a bad idea to set the position of the body explictly in a physics engine you should use forces etc to move it around.See how in the mousejoint.
Feb 19, 2013 at 8:47 AM
Thanks for the reply, i will take a look at this.