Skip to content
Browse files

fixed timestep

  • Loading branch information...
1 parent e7ec6ae commit d358e2ecf98e7492fe6ae25c514376192befb794 @Detegr committed Mar 13, 2013
Showing with 76 additions and 33 deletions.
  1. +3 −3 ConnectionManager.cpp
  2. +27 −12 client.cpp
  3. +6 −5 packetparser.cpp
  4. +2 −3 physicsmanager.cpp
  5. +1 −1 physicsmanager.h
  6. +35 −8 server.cpp
  7. +2 −1 timer.cpp
View
6 ConnectionManager.cpp
@@ -78,18 +78,18 @@ void C_ConnectionPool::M_SendToAll(C_UdpSocket& sock, C_Packet& p) const
unsigned char keyvec=c->M_GetKeys();
if(keyvec & 0x1)
{
- b->SetAngularVelocity(-3.0f);
+ b->SetAngularVelocity(-0.3f);
}
else if(keyvec & 0x2)
{
- b->SetAngularVelocity(3.0f);
+ b->SetAngularVelocity(0.3f);
}
else b->SetAngularVelocity(0.0f);
if(keyvec & 0x8)
{
float32 a = c->M_GetEntity()->M_Body()->GetAngle();
b2Vec2 force = b2Vec2(-sin(a), cos(a));
- force *= 6.0f;
+ force *= 0.6f;
b->ApplyForceToCenter(force);
}
if(keyvec & 0x20)
View
39 client.cpp
@@ -87,19 +87,29 @@ int main()
p.M_Clear();
unsigned char keyvec=0;
- //unsigned char prevkeyvec=0;
+ unsigned char prevkeyvec=0;
C_Timer timer;
timer.M_Reset();
+
+ double t=0.0;
+ double accu=0.0;
+ const double dt=0.1;
+ double currt=timer.M_Now();
+
while(running)
{
+ timer.M_Reset();
double newt=timer.M_Now();
- double currt=
- idt.M_Reset();
+ double framet=newt-currt;
+ if(framet > 0.25)
+ {
+ framet=0.25;
+ }
+ currt=newt;
+
keyvec=getkeys();
- /*
if(sock.M_Receive(p, 1, NULL, NULL))
{
- //idt.M_Reset();
C_Packet keys;
if(prevkeyvec != 0xF0)
{
@@ -109,7 +119,6 @@ int main()
prevkeyvec=keyvec;
while(p.M_Size()) C_PacketParser::M_Parse(p);
}
- */
//else
{
const std::vector<C_GfxEntity*>& entities = r->M_Entities();
@@ -121,18 +130,18 @@ int main()
b2Body* b=e->M_Body();
if(keyvec & 0x1)
{
- b->SetAngularVelocity(-3.0f);
+ b->SetAngularVelocity(-0.3f);
}
else if(keyvec & 0x2)
{
- b->SetAngularVelocity(3.0f);
+ b->SetAngularVelocity(0.3f);
}
else b->SetAngularVelocity(0.0f);
if(keyvec & 0x8)
{
float32 a = b->GetAngle();
b2Vec2 force = b2Vec2(-sin(a), cos(a));
- force *= 0.06f;
+ force *= 0.6f;
b->ApplyForceToCenter(force);
}
}
@@ -148,11 +157,17 @@ int main()
}
*/
}
- //g_Sleep(1);
- g_Sleep(30-((int)idt.M_Get()*1000));
- pm->M_Simulate();
+ accu += framet;
+ while(accu >= dt)
+ {
+ //pm->M_Simulate(dt);
+ accu -= dt;
+ t += dt;
+ }
p.M_Clear();
r->M_Draw();
+
+ //g_Sleep(30-((int)timer.M_Get()*1000));
running=!(C_Singleton::M_InputHandler()->M_Get(ESC));
}
View
11 packetparser.cpp
@@ -64,15 +64,16 @@ void C_PacketParser::M_GfxEntity(C_Packet& p, bool full)
if(full) p >> id >> type >> name >> scale >> x >> y >> angle;
else p >> id >> x >> y >> angle;
- //C_PhysicsManager* pm=C_Singleton::M_PhysicsManager();
+ C_PhysicsManager* pm=C_Singleton::M_PhysicsManager();
C_Renderer* r=C_Singleton::M_Renderer();
C_GfxEntity* e=r->M_GetEntity(id);
- //C_Entity* pe=pm->GetEntity(id);
+ C_Entity* pe=pm->GetEntity(id);
if(e)
{
- //e->SetPosition(pe->GetPosition());
- //pe->SetPosition(x,y);
- //e->M_SetRotation(angle);
+ e->SetPosition(pe->GetPosition());
+ pe->SetPosition(x,y);
+ e->M_SetRotation(angle);
+ pe->SetRotation(angle);
}
else if(full)
{
View
5 physicsmanager.cpp
@@ -49,10 +49,9 @@ C_Entity* C_PhysicsManager::M_CreateStaticEntity(unsigned short id, const C_Mode
return m_Bodies.back();
}
-void C_PhysicsManager::M_Simulate()
+void C_PhysicsManager::M_Simulate(double timestep)
{
- double dt=C_Singleton::M_Timer()->M_Get()/0.030;
- m_World.Step(m_TimeStep*dt, m_VelocityIterations, m_PositionIterations);
+ m_World.Step(timestep, m_VelocityIterations, m_PositionIterations);
}
void C_PhysicsManager::M_DestroyEntity(C_Entity* e)
View
2 physicsmanager.h
@@ -24,7 +24,7 @@ class C_PhysicsManager
std::vector<C_Entity*> m_Bodies;
public:
- void M_Simulate();
+ void M_Simulate(double timestep);
C_Entity* M_CreateDynamicEntity(const C_Model& m, float s=1.0f);
C_Entity* M_CreateDynamicEntity(unsigned short id, const C_Model& m, float s=1.0f);
C_Entity* M_CreateStaticEntity(const C_Model& m, float s=1.0f);
View
43 server.cpp
@@ -34,7 +34,7 @@ int main()
C_ContactListener contactlistener;
C_PhysicsManager* p = C_Singleton::M_PhysicsManager();
p->M_SetContactListener(new C_ContactListener);
- C_Timer* t = C_Singleton::M_Timer();
+ C_Timer* tmr = C_Singleton::M_Timer();
C_ModelManager* m = C_Singleton::M_ModelManager();
if(!m->M_Load("triangle", "test.2dmodel")) exit(1);
@@ -77,9 +77,24 @@ int main()
C_Packet packet;
C_Packet newplayers;
+ double t=0.0;
+ double accu=0.0;
+ const double dt=0.1;
+ C_Timer timer;
+ double currt=timer.M_Now();
+
+ tmr->M_Reset();
+ timer.M_Reset();
while(run)
{
- t->M_Reset();
+ double newt=timer.M_Now();
+ double framet=newt-currt;
+ if(framet > 0.25)
+ {
+ framet=0.25;
+ }
+ currt=newt;
+
while(sock.M_Receive(packet, 1, &ip, &port))
{
C_Connection* c = pool.M_Exists(ip,port);
@@ -157,15 +172,27 @@ int main()
{
*(*it) >> packet;
}
- pool.M_SendToAll(sock, packet);
- if(newplayers.M_Size())
+
+ accu += framet;
+ while(accu >= dt)
+ {
+ p->M_Simulate(dt);
+ accu -= dt;
+ t += dt;
+ }
+
+ if(100-(int)(timer.M_Get()*1000) == 0)
{
- pool.M_SendToAll(sock, newplayers);
- newplayers.M_Clear();
+ std::cout << "Sending" << std::endl;
+ pool.M_SendToAll(sock, packet);
+ if(newplayers.M_Size())
+ {
+ pool.M_SendToAll(sock, newplayers);
+ newplayers.M_Clear();
+ }
+ timer.M_Reset();
}
packet.M_Clear();
- g_Sleep(250-((int)t->M_Get()*1000));
- p->M_Simulate();
}
C_Singleton::M_DestroySingletons();
}
View
3 timer.cpp
@@ -19,7 +19,8 @@ double C_Timer::M_Now() const
#else
struct timeval cur;
gettimeofday(&cur, NULL);
- return (((cur.tv_sec*1000000)+(cur.tv_usec));
+ return ((cur.tv_sec*1000000)+(cur.tv_usec));
+ #endif
}
void C_Timer::M_Reset()

0 comments on commit d358e2e

Please sign in to comment.
Something went wrong with that request. Please try again.