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.
// Initial valuesprivate float speed
= 10f
;private float friction
= 0
.999f
;private float thrust
= 10;private int turnSpeed
= 360;// Turning a craftif (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 craftsforeach (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.