up until now, my game (a side scroller) worked on fixed-time movement, meaning the amount of movement done every gameloop iteration was of fixed size.
this was achieved by creating a method Move() in my Entity class, from which all other game entities inherit:
class Entity
{
//…
public void Move()
{
X+=XSpeed;
Y+=YSpeed;
}
}
class Main
{
Entity[] Entities;
public void Move()
{
for(int i=0;i<Entities.length;i++)
{
Entities[i].Move();
}
}
public void Draw()
{
for(int i=0;i<Entities.length;i++)
{
Entities[i].Draw();
}
}
public void run()
{
while(true)
{
Move();
Draw();
try
{
Thread.sleep(1);
}
catch(Exception e)
{
}
}
}
public static void Main(String[] args)
{
Thread t=new thread(this);
t.start();
}
}
(this is just a simplified version, the real move() method is a little more complicated)
now, i know this is isn’t the correct method for doing this, because i move the Entities by a fixed amount regardless of how much time has passed. so i’ve started using time-corrected movement, that meant turning Entity.Move() to
public void Move(float Time)
{
X+=XSpeedTime;
Y+=YSpeedTime;
}
and calling this method with the Time Difference from the last game loop iteration as an argument.
problem is, when i did this, my performence have dropped significally-
my game loop worked at 2 miliseconds (without rendering), and now it has dropped to 30 miliseconds…
three questions:
- what do you think caused the drop? the another argument in the method call, or the extra calculations (they haven’t gotten that complicated when i added the time factor)?.
- maybe i’m doing something wrong and i shouldn’t experience such a drop if i work correctly?
- maybe it’s worth reverting back to fixed time movement and trying to stabalize the game loop? after all, a 28 milisec difference is quite alot…
Thanks in advance, and sorry for the extra long post,
noam