Delta Engine
»
Support
»
Physics
»
Physics problem (Farseer)
Joined: 8/24/2011(UTC) Posts: 7
|
Why can't I get an object to go faster than ~FPS*2? Its not very fun when you can fly as fast as your bullets... Wall of code (example)
Code:class Game : DynamicModule
{
private Material2D material = Material2D.Default;
Size size = new Size(0.02f, 0.04f);
PhysicsBody testBody;
public Game()
: base("Simple Game", typeof(Application))
{
Settings.Debug.AllowDevelopmentAssemblyUpdating = true;
if (Physics.Instance == null)
{
Application.Quit();
return;
}
Settings.Extra.LimitFramerateNumber = 60;
Settings.Extra.GameLogicUpdatesPerSec = 0;
Physics.Gravity = Vector.Zero;
testBody = Physics.CreateRectangle(0.05f, 0.05f, 1);
testBody.Position2D = Point.Half;
Input.Commands[Command.QuitTest].Add(delegate
{
Application.Quit();
});
}
public override void Run()
{
if (Time.EverySecond)
rows.Clear();
DrawText("FPS: " + Time.Fps);
DrawText("Target fps (LimitFramerateNumber): " + Settings.Extra.LimitFramerateNumber);
DrawText("Settings.Extra.GameLogicUpdatesPerSec: " + Settings.Extra.GameLogicUpdatesPerSec);
DrawText("Velocity: " + testBody.LinearVelocity.Length.ToInvariantString());
DrawText("Press W to apply LinearImpulse or E for Force");
DrawText("Press Mouse.LeftButton to spawn and RightButton to apply a force to them");
DrawText("Press R to reset");
DrawText("Press 1, 2, 3 or 4 to limit FPS to (0, 30, 60, 120)");
HandleInput();
foreach (PhysicsBody body in Physics.Bodies)
{
material.Draw(new Rectangle(body.Position2D - size / 2f, size), body.Rotation);
}
DrawText_D();
}
private void HandleInput()
{
if (Input.Keyboard.IsPressed(InputButton.D1))
Settings.Extra.LimitFramerateNumber = 0;
else if (Input.Keyboard.IsPressed(InputButton.D2))
Settings.Extra.LimitFramerateNumber = 30;
else if (Input.Keyboard.IsPressed(InputButton.D3))
Settings.Extra.LimitFramerateNumber = 60;
else if (Input.Keyboard.IsPressed(InputButton.D4))
Settings.Extra.LimitFramerateNumber = 120;
if (Input.Keyboard.IsPressed(InputButton.W))
{
testBody.ApplyLinearImpulse(Vector.UnitY * 1000000f);
}
if (Input.Keyboard.IsPressed(InputButton.E))
{
testBody.ApplyForce(Vector.UnitY * 1000000f);
}
if (Input.Keyboard.IsPressed(InputButton.R))
{
testBody.Position2D = Point.Half;
testBody.LinearVelocity = Vector.Zero;
testBody.Rotation = 0f;
testBody.AngularVelocity2D = 0f;
List<PhysicsBody> bodiesToRemove = new List<PhysicsBody>();
foreach (PhysicsBody body in Physics.Bodies)
{
if (body != testBody)
bodiesToRemove.Add(body);
}
foreach (PhysicsBody body in bodiesToRemove)
{
body.Remove();
}
}
if (Input.Mouse.LeftButtonIsPressed)
{
PhysicsBody body = Physics.CreateRectangle(size.Width, size.Height, 1f);
body.Position2D = Input.Mouse.Position;
}
if (Input.Mouse.RightButtonIsPressed)
{
Physics.ApplyExplosion(new Vector(Input.Mouse.Position, 0f), 50f);
}
}
List<string> rows = new List<string>();
private void DrawText(string txt)
{
if (Time.EverySecond)
rows.Add(txt);
}
private void DrawText_D()
{
float top = ScreenSpace.DrawArea.Top;
foreach (string txt in rows)
{
Size s = Font.Default.Measure(txt);
Rectangle r = new Rectangle(ScreenSpace.DrawArea.Left, top, s.Width, s.Height);
Font.Default.Draw(txt, r);
top += s.Height;
}
}
}
This is tested with Farseer, I am not sure if the other engines have this problem or not. Edited by user Sunday, February 19, 2012 10:39:18 PM(UTC)
| Reason: typo
|
|
|
|
Medals:  Joined: 8/20/2011(UTC) Posts: 1,421 Location: Hannover
Thanks: 18 times Was thanked: 97 time(s) in 92 post(s)
|
Hi s990we, First of all, why do you limit your application to 60fps? If you allow more, you will obviously get more physics steps per second! Other than that there are currently 2 ways of increasing the number of times the physics is updated per frame: 1. Change it in your physics implementation (e.g. Delta.PhysicsEngines.Farseer, needs source code of course): Code:
#region UpdateSimulation
/// <summary>
/// Perform Farseer simulation step.
/// </summary>
protected override void UpdateSimulation(float timeStep)
{
// Update profiling update time also
profilingInfo.UpdateTime = farseerPhysicsWorld.UpdateTime;
if (IsPaused == false)
{
// Run 5 time steps with 1/5 time of the frame each
for (num=0; num<5; num++)
farseerPhysicsWorld.Step(timeStep/5.0f);
}
}
#endregion
2. If you don't want to change any physics implementation you can just call multiple times in your game code (remember that it will be executed once automatically, the more you call the more physics time will pass, so you might need to modify your values as well). I hope that helps. There also was some code for fixed time steps, but I guess it is commented out because I cannot find it. We still have 17+ physics features planed for v0.9.6+, it is probably in there ..
|
|
|
|
Delta Engine
»
Support
»
Physics
»
Physics problem (Farseer)
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.