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

Moving with constant velocity

May 28, 2007 at 4:46 AM
Hello again. I am working to adjust my movement mechanic to incorporate the Farseer engine and am having a little trouble... My ship moves with a constant velocity, so for each button press event (up, down, left, right) I set the x or y component of the velocity vector accordingly (ie. if up is pressed, velocity.Y = -speed... if right is pressed, velocity.x = speed... etc). This works rather well with the current algorithm I am using, but it doesn't seem as though I can set the individual x and y components of the LinearVelocity property for a RigidBody. I get an error stating:

"Cannot modify the return value of 'FarseerGames.FarseerXNAPhysics.Dynamics.Body.LinearVelocity' because it is not a variable"

In the past when I have received this error, it has been due to the fact that I could not set fields off of a property that returns an object (not sure why however). So is there any way of doing what I am describing above? Thanks for any and all replies!!
May 29, 2007 at 12:25 PM
Bodies are really not meant to have there velocity modifed directly. It is better to simply apply forces to objects in order to move them. To set a constant velocity, adjust your drag coeficent and apply a constant force. Modifying the velocity directly, sort of defeats the purpose of the engine.
May 29, 2007 at 11:09 PM
Yes, I had read that in another post, but could not find any other solution. I tried doing what you're describing and can't seem to get the result I was hoping for. When you say, apply a constant force, do you mean something like:

EntityRigidBody.ApplyForce(new Vector(2000.0f,0.0f);

I currently have the drag coefficient set to 0.0f, so there is no air resistance at all. Using this method, my ship moves, but takes several seconds before getting to full speed (it's slowly accelerating), which is not what I am trying to achieve. Ideally, I would like for the ship to always move at a given velocity and never have to accelerate... does that make sense? So no matter how long the movement keys are pressed, I want the ship to always go the same speed... Can this sort of thing be done by applying force? Thanks again for your time and many congratulations on your new family member!
May 30, 2007 at 5:45 PM
You can make your ship reach a fixed speed (or at least very close to fixed) by getting the right balance between your linear drag coefficient and the force you are applying. The drag force is proportional to the velocity squared of your ship. So at low speeds, the drag will be small but it will quickly ramp up as you ship increases speed. At some point the drag and the thrust force you are applying will balance eachother out and you will be at a constant velocity. It's the same idea as an object falling... at some point it reaches a terminal velocity where the gravity force is countered by the air resistance(drag).

So, play around with the drag and foce values. If you want a quick accelerating ship, you will need to bump up the thrust force and drag force.

Hope this helps.
May 30, 2007 at 6:38 PM
Thanks for the quick reply! I definitely understand what you're saying and think I could achieve this rather easily, but that is not really what I'm going for. Sorry for not being more precise in my previous posts, but what I want is for my ship to always move at the same speed. So in effect, there would be no acceleration acting on this object as it has a constant velocity. However, I only want it to move at this velocity if the movement keys are pressed. If they are not pressed, then the object should immediately stop. So rather than reaching a constant velocity through acceleration, the object should travel at the exact same speed at any point in time (unless acted on by an outside force... like an enemy weapon or environmental affects for example). This is only for movement of the main ship through input... I want the ship to be affected by other forces in the game that could potentially adjust the velocity, but as far as movement goes, I want to always move at the same speed from the instant that the movement key is pressed. Does this make sense? I'm sorry for not being more clear, but I don't know how else to describe what I want to achieve. ( The second video sort of shows what I mean... it may be difficult to tell, but the ship is moving at the same rate - always, without acceleration... this is ideally what I would like to do within the Farseer Engine). Sorry to take up so much of your time, but hopefully there is an easy solution for this problem. Thanks again and have a great day!