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

# Implement a Body.TravelTo(Point) method?

 Wiki Link: [discussion:73253]
 rhythm86 Oct 27, 2009 at 7:14 PM Edited Oct 27, 2009 at 7:21 PM First of all Farseer rocks, big thanks to everyone who's worked on it. I'm trying to use it with WPF to create applications for the MS Surface. I'm trying to manipulate objects in a nice fashion using contacts from the surface, but it's still a bit shaky. Clearly setting the Body's position directly doesn't work, as none of the other properties are updated, and bodies get set on top of one another and all sorts of trouble ensues. It occurred to me that it could be useful to have something like a Body.TravelTo(Vector2 location) method, where you would tell the engine where you'd like the body to get to, and the body would take the fastest path to get there, updating it's velocity and colliding properly. I think basically you would need to figure its velocity based on the position change and update it, as opposed to trying to figure a force to apply to get your body there (my first attempt) . What I'm not sure of is how this would work with the other forces that may be happening. Could I just figure the velocity based on the position delta and set it there? Or would I need to add it to the Body's existing velocity? Would this depend on the situation? It seems to me like this method would be useful in a lot of applications, especially with mutli-touch, and would save the need for creating a lot of different custom springs to achieve the same effect. What do you guys think? I wanted to make sure this makes sense, and if so I'll try and implement it. pnikosis Oct 28, 2009 at 11:19 AM Edited Oct 28, 2009 at 3:44 PM Couold be something like this (may or not compile):   `public bool MoveToPoint(Vector2 pos){ float motionSpeed = 10f; Vector2 currentPos = _body.Position; Vector2 direction = (pos - currentPos); if(direction.Lenght() <= 1) { _body.LinearVelocity = 0; return true; } _body.LinearVelocity = motionSpeed * Vector2.Normalize(direction); return false;}`   Then in your main code:   ```if(MoveToPoint(point)) { // Here is the code when it arrives to destination. } ``` rhythm86 Oct 28, 2009 at 2:38 PM Edited Oct 28, 2009 at 2:47 PM hm, that would take the body there with a constant speed, and it would continue past it once it reached it. i guess that method would rely on you continuously calling it. i was hoping to have the method take care of the process of accelerating and slowing the object down as it nears its destination. i think it would need to store the location it's traveling towards and adjust it's path accordingly at every update. so there would be something like      ```public bool IsTraveling{ get; set; } private Vector2 _travelingTo public Vector2 TravelingTo { get; set { _travelingTo = value; IsTraveling = true; } ``` `}` ```float TravelFactor = somethingf, TravelTolerance = somethingf; // then in Update()... if(IsTraveling) { // adjust velocity to head towards the point, maybe make velocity // proportional to the distance from TravelingTo  ``` ` float distanceSquared = Vector2.DistanceSquared(Position, TravelTo);` ` this.LinearVelocity = (distanceSquared * TravelFactor) * normalizedDirection;` ``` // once we've reached our destination (within a tolerance) we are done IsTraveling = distance.Length <= TravelTolerance; }``` pnikosis Oct 28, 2009 at 3:43 PM Yep, you have to continously call it (in the update method, for example), when it reaches to a distance below 1, it resets the linear velocity. If what you want is to slow down the speed when reaching the objective, just change the motionspeed value acordingly to the distance from the objective (the direction.lenght() value). Typocolder Nov 6, 2009 at 7:13 AM Same here! I encountered the same problem in my project. It would be very useful if such method exists. danthekilla Nov 6, 2009 at 8:28 AM Yer this is the kind of function that would be useful for something like a mouse joint too... Anyone know how i could make an object just follow the mouse (or a vector2d) with no delay while still having collisions? Typocolder Nov 7, 2009 at 2:48 AM Edited Nov 16, 2009 at 12:05 PM Here is my workaround with silverlight and physics helper:   `private void PhysicsControllerInitialized(object source){ var mousePosition = new Vector2(); MouseMove += (o, e) => mousePosition = new Vector2((float) e.GetPosition(this).X, (float) e.GetPosition(this).Y); ThreadPool.QueueUserWorkItem( o => { while (true) { Body body = _physicsController.PhysicsObjects["rect"].BodyObject; Vector2 from = body.Position; Vector2 vector = mousePosition - from; if (vector.Length() <= 5) { body.ClearForce(); body.ClearImpulse(); body.ClearTorque(); body.LinearVelocity = Vector2.Zero; } else body.LinearVelocity = vector*20; Thread.Sleep(5); } } );}`     Hope it helps.