Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am building a game in which I want to simulate smooth movement of an object upon pressing a key or any other event. Could someone advice me to get that feel of gliding of an object, sliding on a frictionless surface. How could I have that?

share|improve this question
add comment

6 Answers 6

up vote 3 down vote accepted

Well, simulating a frictionless object is really just like simulating an object with friction, except you leave out the friction.

Seriously, the only sensible interpretation I can make of your question is that you've either forgotten Newtonian mechanics since high school, or that you're still in school and haven't studied it yet. In either case, here's a quick refresher on motion under constant acceleration:

Let x(t) be the position and v(t) the velocity of the object at time t. Then the position and velocity of the object after a time step Δt under constant acceleration a are:

  • v(t + Δt) = v(t) + (a × Δt)
  • x(t + Δt) = x(t) + (v(t) × Δt) + (a × Δt² / 2)

You can express these quantities in any units you like, as long as they're consistent. For video games, a common choice is to measure time in update steps (which might typically occur, say, 20 to 60 times per second for smooth-looking motion), position in pixels, velocity in pixels per time step and acceleration in pixels per time step squared. This simplifies the formulas, since, in particular, Δt = 1.

Also, if the time step (and/or the acceleration per time step) is small enough, the (a × Δt² / 2) term will be so much smaller than (v(t) × Δt) that it can be left out of the second formula without anyone really noticing. A lot of games do that, since it makes the formulas even simpler.

And, before you ask, usually the positions, velocities and accelerations will indeed be two- or three-dimensional vectors. This is not a problem — you just apply the formulas to each component of the vectors separately. It's also perfectly okay for any or all of these quantities to be negative.

share|improve this answer
add comment

The idea is that friction is actually an explicitly added step, additional logic. So the answer is, "You don't". You just build for the basics -- forces, velocity, mass, collision response and then you will be able to play with frictionless bodies. The simulation is the theoretical "perfect" environment.

share|improve this answer
add comment

Fundamentally frictionless movement is just playing with inertia. In whatever your movement code is, don't stop the player immediately when they release their button/analog stick. Instead you want to very slowly reduce their velocity to zero. Or if you wanted to be truly frictionless you never reduce their velocity.

share|improve this answer
    
The moving part will still very, very, very slowly lose energy (and thus, slow down) due to Van der Waals forces between their particles even with perfect lack of any friction. Alas, I've yet to see a game physics engine which would include them. –  Martin Sojka Oct 25 '11 at 15:07
    
Wouldn't Van der Waals forces be a form of friction? –  Nathan Reed Oct 25 '11 at 16:32
    
@Nathan Reed: Kinda, sorta. If you leave them out, the object's molecules fall apart rather quickly. :) –  Martin Sojka Oct 25 '11 at 18:25
add comment

Your basic game code should be organized like this:

IntializeGameState
while (GameIsRunnnig)
begin
  handleInput
  updateGameState
  DrawNextframe
end

For the simualation of a moving object, you should store your objects current position, movement speed and direction.

When initialzing, set the movement speed to 0.

During handleInput, check if any inputs from the user have been made and if so, modify the movement speed and direction of the controlled object accordingly.

Then, during UpdateGamestate, add the current movement vector to the current postion.

Now, redraw your object at the new Position.

This are the basic steps to do. You will need some kind of timer to make your animation smooth by making each step of the animation take an equal amount of time.

share|improve this answer
add comment

Not sure if this is helpful, but this is the code I used in my asteroids game:

if (input.isKeyDown(Input.KEY_W))
    {

        dx += Math.sin(Math.toRadians(image.getRotation())) * delta * 0.01;
        dy += -Math.cos(Math.toRadians(image.getRotation())) * delta * 0.01;

    } 

    x += dx;
    y += dy;

    dx *= 0.98;
    dy *= 0.98;

Increasing the values of the *= on dx and dy decreases the "friction" the player receives. The clever part is that there isn't any top speed, the ship just slows it's accelration down until it's not noticeable. Of course, in a true frictionless environment you could accelerate infinitely, reaching ludicrous high speeds. In that case, you would simply remove this:

   dx *= 0.98;
   dy *= 0.98;
share|improve this answer
    
I did that through using the delta variable, which in most frameworks means frame time. –  Derek Oct 26 '11 at 16:30
add comment

Store the current velocity of an object with each object. Each frame use that velocity along with the frame time to calculate how far to move it.

Acceleration (thrusters) and deceleration (brakes, or friction) simply modify the velocity to a new value.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.