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

Author Topic: Problem with FPS-free space physics  (Read 1963 times)

0 Members and 1 Guest are viewing this topic.

Nbxy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Problem with FPS-free space physics
« on: March 27, 2014, 03:48:44 pm »
I am trying to create a game that would work exactly the same with either 60fps or 2000fps or anything between. Of course high fps as 2000 or even greater is never going to be needed. That is needless to say, so don't bother posting some rant about that subject. That is not what my post is about. But instead, tell me how can I achieve my goal with the following code.
Quote
// Initial values
private float speed = 10f;
private float friction = 0.999f;
private float thrust = 10;
private int turnSpeed = 360;

// Turning a craft
if (Keyboard.IsKeyPressed(Keyboard.Key.Num4)) allCrafts[3].Angle = allCrafts[3].Angle - (allCrafts[3].TurnSpeed * delta);
if (Keyboard.IsKeyPressed(Keyboard.Key.Num6)) allCrafts[3].Angle = allCrafts[3].Angle + (allCrafts[3].TurnSpeed * delta);

// Updating all crafts
foreach (Craft craft in allCrafts)
{
    craft.Momentum_X = (delta * craft.Momentum_X * craft.Friction) + (delta * craft.Thrust * Math.Cos(craft.Angle * (Math.PI / 180.0))) * (craft.ThrustActive - craft.ReverseActive * 0.5);
    craft.Momentum_Y = (delta * craft.Momentum_Y * craft.Friction) - (delta * craft.Thrust * -Math.Sin(craft.Angle * (Math.PI / 180.0))) * (craft.ThrustActive - craft.ReverseActive * 0.5);
               
    craft.X = craft.X + ((float)craft.Momentum_X * craft.Speed * delta);
    craft.Y = craft.Y + ((float)craft.Momentum_Y * craft.Speed * delta);

    craft.Frame = (byte)(craft.Angle/15);

    if (craft.Angle > 360) craft.Angle = 0;
    if (craft.Angle < 0) craft.Angle = 360;

    if (craft.Frame > 23) craft.Frame = 0;
    if (craft.Frame < 0) craft.Frame = 23;

    craft.Shape.TextureRect = new IntRect(0 + (craft.Frame * 64), 0, 64, 64);
    craft.Shape.Position = new Vector2f(craft.X - 25, craft.Y - 25);
    craft.Draw(window);
}

I have no problems with the loop. Everything worked just fine before I added the space physics into the code. The problem here is with the friction, thrust and the momentum factors, I think. For some reason I can't figure out how to use the delta correctly with them. Turning the craft works perfectly fine. Even with 10fps it takes the same time for the craft to turn full circle as with 2000-3000fps, so all fine with that part.

With the current code my craft barely moves since I tried to implement delta in the "craft.Momentum_X =..." -part. If I completely remove delta from there it works about correctly if there is no fps-limit (normally about 2000-3000fps on my system) but of course then if I use something like "window.SetFramerateLimit(60);" it all appears too fast. The craft is about the same speed, but it doesn't accelerate or stop nearly as fast. I get that it's about the fact that I don't multiply those values with delta... but I can't just get my head around it... Help?

Edit: For all it's worth. I am using C# with SFML.Net 2.1.
« Last Edit: March 27, 2014, 03:51:12 pm by Nbxy »

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Problem with FPS-free space physics
« Reply #1 on: March 27, 2014, 04:00:59 pm »
Quote
(craft.ThrustActive - craft.ReverseActive * 0.5)

Maybe you have to mulitiply with delta here somehow.

Nbxy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem with FPS-free space physics
« Reply #2 on: March 29, 2014, 01:09:52 am »
Quote
(craft.ThrustActive - craft.ReverseActive * 0.5)

Maybe you have to mulitiply with delta here somehow.

Thanks for the effort but that doesn't seem to do the trick. I've tried dozens of places for the multiplier and none of them did the trick. Should I try to separate the whole thing into smaller expressions or would that even change anything.

Of course the "easy" way would to but a static FPS but it I don't see much honor in that since the systems I've tried the my game with vary a lot with the CPU and GPU capabilities so it's bound to slow down on older systems and with a low frame rate limit it doesn't use my high-end-PC:s resources nearly enough.

Can anyone figure this one out?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Problem with FPS-free space physics
« Reply #3 on: March 29, 2014, 01:41:51 am »
I think reading this link may be beneficial to you: http://gafferongames.com/game-physics/fix-your-timestep/

 

anything