Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: A time question  (Read 5890 times)

0 Members and 1 Guest are viewing this topic.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« on: September 15, 2008, 10:20:36 pm »
Hi, using the GetFrameTime() method correctly the hero in my game should move and jump the same no matter the FPS right? Right now he jumps higher in release mode than in debug mode. I'm suppose to use GetFrameTime() for every new calculation of the x- and y-positions of my hero right?

Haven't used time before as you can see :)

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: A time question
« Reply #1 on: September 15, 2008, 11:51:45 pm »
Quote from: "dabo"
Hi, using the GetFrameTime() method correctly the hero in my game should move and jump the same no matter the FPS right? Right now he jumps higher in release mode than in debug mode. I'm suppose to use GetFrameTime() for every new calculation of the x- and y-positions of my hero right?

Haven't used time before as you can see :)


If you're getting different action speeds based on FPS, you're doing something wrong.
Basically, every game action should have a "speed" in some reasonable units.  In the case of your hero, it might be in pixels per second.
Then each frame you'd simply multiply his speed times the number of seconds for that frame.  Make sense?

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
A time question
« Reply #2 on: September 16, 2008, 06:22:59 am »
It works by moving the object by the frame time multiplied by how many pixels you want the object to move each second, or the speed.
The frame time being the measure of how long it took for a call to sf::Window::Display to occur since the last call of it.

Code: [Select]
float Offset  = App.GetFrameTime() * 128.f;
float OffsetX = 0.f;
float OffsetY = 0.f;

if (Input.IsKeyDown(sf::Key::Up))    OffsetY = -Offset;
if (Input.IsKeyDown(sf::Key::Left))  OffsetX = -Offset;
if (Input.IsKeyDown(sf::Key::Down))  OffsetY += Offset;
if (Input.IsKeyDown(sf::Key::Right)) OffsetX += Offset;

View.Move(OffsetX, OffsetY);


This way, if a frame is rendered in one second (1 FPS), the object will move exactly one times the speed of your object every loop.
However, if a frame is rendered every 1/4 of a second (4 FPS), the object will move 1/4 the pixels every loop.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« Reply #3 on: September 16, 2008, 06:37:43 pm »
Ok, thanks. I'm gonna try to figure out what I'm doing wrong.

Another thing, right now I'm not limiting the FPS (I get ~800 in release mode), what is a reasonably limit? I tried 30 and 60 but the movements don't seem that smooth.

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
A time question
« Reply #4 on: September 16, 2008, 07:57:01 pm »
Try using...
Code: [Select]
App.UseVerticalSync(true);

... instead of a direct FPS limit.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« Reply #5 on: September 16, 2008, 08:29:04 pm »
I still get ~800fps in release mode. I remember Laurent saying you have to set so that you let the application choose if v-sync should be used or not (some graphic card setting I suppose). Too bad I don't know where.

Dig

  • Newbie
  • *
  • Posts: 31
    • View Profile
A time question
« Reply #6 on: September 16, 2008, 11:22:54 pm »
If you have an ATI card and have that annoying Catalyst Control Center in the system tray that has options for controlling vertical sync for DirectX and OpenGL applications.

Assuming you're on windows right clicking on the desktop a bringing up properties->settings->advanced sometimes includes additional tab specific to your video card which should have those options.

No doubt nVidia has something similar.  For other graphics cards I'm not sure.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« Reply #7 on: September 17, 2008, 06:37:54 pm »
http://www.intel.com/support/graphics/sb/CS-004692.htm
(Mobile IntelĀ® 945GM Express Chipset Family)

Here it says how to turn v-sync off, doing the opposite doesn't make a difference. I see no setting to let the application choose.

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
A time question
« Reply #8 on: September 17, 2008, 07:08:19 pm »
I doubt he is running at ~800 Hz. :?

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« Reply #9 on: September 18, 2008, 05:45:37 pm »
What?

I'm gonna show you my code after I've done some more testing. Still got the time problem.

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
A time question
« Reply #10 on: September 19, 2008, 12:25:47 am »
Quote from: "Wizzard"
I doubt he is running at ~800 Hz. :?


I can easily get 1500+ FPS on my machine in  my engine if not much is going on.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« Reply #11 on: September 26, 2008, 08:05:03 pm »
Ok, here is most of my code (left out the unnecessary code, collision checking etc.), why isen't this fps-independent?

Code: [Select]
#define MAX_FORCE 2.05
#define BLOCK_SIZE 32

int main()
{
sf::RenderWindow Game(sf::VideoMode(640, 480), "Platform");

sf::Event Event;

sf::Image imgPlr;
if(!imgPlr.LoadFromFile("./data/block.png"))
return EXIT_FAILURE;

imgPlr.SetSmooth(false);

sf::Sprite sprPlr(imgPlr, sf::Vector2f(128.f, 64.f));

bool jumping = false;
bool standing = false;
float x = 128.f;
float y = 64.f;
float force = 0.f;
float frameRate;
float gravity = 3.14f;
float offset = 0.f;

while(Game.IsOpened())
{
Game.Clear();

while(Game.GetEvent(Event))
{
switch(Event.Type)
{
case sf::Event::KeyPressed:
if(Event.Key.Code == sf::Key::Escape)
Game.Close();
}
}

frameRate = Game.GetFrameTime();

const sf::Input& Input = Game.GetInput();

if(Input.IsKeyDown(sf::Key::Left))
x -= 60 * frameRate;

if(Input.IsKeyDown(sf::Key::Right))
x += 60 * frameRate;

if(Input.IsKeyDown(sf::Key::X) && !jumping && standing)
{
jumping = true;
standing = false;
force = -2.f;
}
else
{
if(!standing)
force += gravity * frameRate;

if(force > MAX_FORCE)
force = MAX_FORCE;
      }

      y += force - offset;

sprPlr.SetPosition(x, y);

Game.Draw(sprPlr);

Game.Display();
}

return EXIT_SUCCESS;
}

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
A time question
« Reply #12 on: September 26, 2008, 08:09:04 pm »
Quote from: "dabo"
Ok, here is most of my code (left out the unnecessary code, collision checking etc.), why isen't this fps-independent?


From a quick look, probably because you're not multiplying all relevant variables by the frame time.  You have the right idea of calculating movement variables, but you missed one.  Look again...  :wink:

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« Reply #13 on: September 26, 2008, 08:54:48 pm »
I have always thought that you multiply with the frame time when you use the old value to get the new one, for example using operators:

Code: [Select]
+= -=
I don't think I need it here (called only once at the beginning of a jump):
Code: [Select]
force = -2.f;
...beacuse later:
Code: [Select]
y += force - offset;
...I move the player up 2 (offset = 0), and then it will be increased using the frame time:
Code: [Select]
force += gravity * frameRate;

I've tried a bunch of different stuff but can't figure it out.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
A time question
« Reply #14 on: October 01, 2008, 01:02:13 pm »
Anyone?