Thread: Time Dependent Movement?

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Time Dependent Movement?

    Hi I got my little paddle object and player working together to move around, and it does what I want it to except it does it at whatever frame rate the window is going at, (alot I assume). So when I tell my paddle to move up it flies off the screen, how do I make the movement updates time dependent?

    My project is based off of Nehe's Lesson 1

    Player.h
    Code:
    #include "vector2.h"
    #include "Paddle.h"
    class Player
    {
    public:
    	vector2 location;
    	Paddle paddle;
    
    	const vector2 & get_position() const { return location; }
    
    	Player()
    	{
    		location.x = 0;
    		location.y = 0;
    	}
    	void draw_paddle()
    	{
    		paddle.draw(location);
    	}
    	void move_paddle(vector2 direction)
    	{
    		location += direction;
    	}
    };
    
    Player player;
    Paddle.h

    Code:
    #include "vector2.h"
    class Paddle
    {
    public:
    
    	void draw(vector2 vec)
    	{
    		glTranslatef(vec.x,vec.y,-100.0f);
    		glBegin(GL_QUADS);						// Start Drawing Quads
    			glVertex3f(-1.0f, -5.0f,  1.0f);	// Bottom Left Of The Texture and Quad
    			glVertex3f( 1.0f, -5.0f,  1.0f);	// Bottom Right Of The Texture and Quad
    			glVertex3f( 1.0f,  5.0f,  1.0f);	// Top Right Of The Texture and Quad
    			glVertex3f(-1.0f,  5.0f,  1.0f);	// Top Left Of The Texture and Quad
    		glEnd();
    	}
    };
    Last edited by Shamino; 03-28-2009 at 06:21 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So don't make direction so big, or multiply it by the time-step involved.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You will need a time delta:

    Code:
    LARGE_INTEGER freq;
    LARGE_INTEGER lastTime;
    LARGE_INTEGER nowTime;
    
    QueryPerformanceFrequency(&freq);
    ...
    ...
    QueryPerformanceCounter(&lastTime);
    while (inGame)
    {
       QueryPerformanceCounter(&nowTime);
    
       float timeDelta = (nowTime.QuadPart - lastTime.QuadPart) * (1.0f / freq.QuadPart);
    
       Render();
       Update(timeDelta);
    
       lastTime = nowTime;
    }
    Now multiply all movements by the timeDelta passed to Update(). I would imagine something like this:

    Code:
    GameApp::Update(float timeDelta)
    {
       m_pScene->Update(timeDelta);
      ...
    }

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Thanks bubba I'll use the information you gave me in conjunction with this tidbit I found on google: Precise (and simple!) timer using time.h - C++ - Source Code | DreamInCode.net
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    First question, is this what you meant? Second off am I doing this right? I'm getting an error
    binary '*' : 'vector2' does not define this operator or a conversion to a type acceptable to the predefined operator


    Code:
    #include "vector2.h"
    #include "Paddle.h"
    #include "time.h"
    time_t timer;
    class Player
    {
    public:
    	vector2 location;
    	Paddle paddle;
    
    	const vector2 & get_position() const { return location; }
    
    	Player()
    	{
    		location.x = 0;
    		location.y = 0;
    	}
    	void draw_paddle()
    	{
    		paddle.draw(location);
    	}
    	void move_paddle(vector2 direction)
    	{
    		location += direction * time(&timer) * 1;
    
    	}
    };
    
    Player player;
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Around this time I will also note a bit of an oddity.

    Only when I move in the right direction it flies off screen now, it appears as up down and left are working.

    That is when I remove the extra silliness from the move function i just added.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    void move_paddle(vector2 direction)
    {
        location += direction * time(&timer) * 1;
    }
    What is this? Are you going to recompute your delta for every object? If you do your system will fail 100% because each delta will be slightly different. Compute your delta once before calling update and then use that delta for ALL objects.

    It's as simple as this:

    Code:
    ...
    m_vecPosition += m_vecVelocity * timeDelta;
    Or:

    Code:
    ...
    ComputeForces();
    RigidBody.Accel = RigidBody.Force / RigidBody.Mass;
    RigidBody.Velocity += RigidBody.Accel * timeDelta;
    RigidBody.Position += RigidBody.Velocity * timeDelta;
    ...
    This does not take into account friction or drag which 'normally' act in the exact opposite direction of velocity.

    But regardless if you are simulating physics or just moving things across the screen arbitrarily you will always multiply by the timeDelta which is expressed in seconds.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Bubba thank you yet again for pointing out the obvious (in all seriousness)
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 09:11 AM
  2. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM