Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed timestep

  • Loading branch information...
commit d358e2ecf98e7492fe6ae25c514376192befb794 1 parent e7ec6ae
Antti Keränen authored
6 ConnectionManager.cpp
@@ -78,18 +78,18 @@ void C_ConnectionPool::M_SendToAll(C_UdpSocket& sock, C_Packet& p) const
78 78 unsigned char keyvec=c->M_GetKeys();
79 79 if(keyvec & 0x1)
80 80 {
81   - b->SetAngularVelocity(-3.0f);
  81 + b->SetAngularVelocity(-0.3f);
82 82 }
83 83 else if(keyvec & 0x2)
84 84 {
85   - b->SetAngularVelocity(3.0f);
  85 + b->SetAngularVelocity(0.3f);
86 86 }
87 87 else b->SetAngularVelocity(0.0f);
88 88 if(keyvec & 0x8)
89 89 {
90 90 float32 a = c->M_GetEntity()->M_Body()->GetAngle();
91 91 b2Vec2 force = b2Vec2(-sin(a), cos(a));
92   - force *= 6.0f;
  92 + force *= 0.6f;
93 93 b->ApplyForceToCenter(force);
94 94 }
95 95 if(keyvec & 0x20)
39 client.cpp
@@ -87,19 +87,29 @@ int main()
87 87 p.M_Clear();
88 88
89 89 unsigned char keyvec=0;
90   - //unsigned char prevkeyvec=0;
  90 + unsigned char prevkeyvec=0;
91 91 C_Timer timer;
92 92 timer.M_Reset();
  93 +
  94 + double t=0.0;
  95 + double accu=0.0;
  96 + const double dt=0.1;
  97 + double currt=timer.M_Now();
  98 +
93 99 while(running)
94 100 {
  101 + timer.M_Reset();
95 102 double newt=timer.M_Now();
96   - double currt=
97   - idt.M_Reset();
  103 + double framet=newt-currt;
  104 + if(framet > 0.25)
  105 + {
  106 + framet=0.25;
  107 + }
  108 + currt=newt;
  109 +
98 110 keyvec=getkeys();
99   - /*
100 111 if(sock.M_Receive(p, 1, NULL, NULL))
101 112 {
102   - //idt.M_Reset();
103 113 C_Packet keys;
104 114 if(prevkeyvec != 0xF0)
105 115 {
@@ -109,7 +119,6 @@ int main()
109 119 prevkeyvec=keyvec;
110 120 while(p.M_Size()) C_PacketParser::M_Parse(p);
111 121 }
112   - */
113 122 //else
114 123 {
115 124 const std::vector<C_GfxEntity*>& entities = r->M_Entities();
@@ -121,18 +130,18 @@ int main()
121 130 b2Body* b=e->M_Body();
122 131 if(keyvec & 0x1)
123 132 {
124   - b->SetAngularVelocity(-3.0f);
  133 + b->SetAngularVelocity(-0.3f);
125 134 }
126 135 else if(keyvec & 0x2)
127 136 {
128   - b->SetAngularVelocity(3.0f);
  137 + b->SetAngularVelocity(0.3f);
129 138 }
130 139 else b->SetAngularVelocity(0.0f);
131 140 if(keyvec & 0x8)
132 141 {
133 142 float32 a = b->GetAngle();
134 143 b2Vec2 force = b2Vec2(-sin(a), cos(a));
135   - force *= 0.06f;
  144 + force *= 0.6f;
136 145 b->ApplyForceToCenter(force);
137 146 }
138 147 }
@@ -148,11 +157,17 @@ int main()
148 157 }
149 158 */
150 159 }
151   - //g_Sleep(1);
152   - g_Sleep(30-((int)idt.M_Get()*1000));
153   - pm->M_Simulate();
  160 + accu += framet;
  161 + while(accu >= dt)
  162 + {
  163 + //pm->M_Simulate(dt);
  164 + accu -= dt;
  165 + t += dt;
  166 + }
154 167 p.M_Clear();
155 168 r->M_Draw();
  169 +
  170 + //g_Sleep(30-((int)timer.M_Get()*1000));
156 171
157 172 running=!(C_Singleton::M_InputHandler()->M_Get(ESC));
158 173 }
11 packetparser.cpp
@@ -64,15 +64,16 @@ void C_PacketParser::M_GfxEntity(C_Packet& p, bool full)
64 64 if(full) p >> id >> type >> name >> scale >> x >> y >> angle;
65 65 else p >> id >> x >> y >> angle;
66 66
67   - //C_PhysicsManager* pm=C_Singleton::M_PhysicsManager();
  67 + C_PhysicsManager* pm=C_Singleton::M_PhysicsManager();
68 68 C_Renderer* r=C_Singleton::M_Renderer();
69 69 C_GfxEntity* e=r->M_GetEntity(id);
70   - //C_Entity* pe=pm->GetEntity(id);
  70 + C_Entity* pe=pm->GetEntity(id);
71 71 if(e)
72 72 {
73   - //e->SetPosition(pe->GetPosition());
74   - //pe->SetPosition(x,y);
75   - //e->M_SetRotation(angle);
  73 + e->SetPosition(pe->GetPosition());
  74 + pe->SetPosition(x,y);
  75 + e->M_SetRotation(angle);
  76 + pe->SetRotation(angle);
76 77 }
77 78 else if(full)
78 79 {
5 physicsmanager.cpp
@@ -49,10 +49,9 @@ C_Entity* C_PhysicsManager::M_CreateStaticEntity(unsigned short id, const C_Mode
49 49 return m_Bodies.back();
50 50 }
51 51
52   -void C_PhysicsManager::M_Simulate()
  52 +void C_PhysicsManager::M_Simulate(double timestep)
53 53 {
54   - double dt=C_Singleton::M_Timer()->M_Get()/0.030;
55   - m_World.Step(m_TimeStep*dt, m_VelocityIterations, m_PositionIterations);
  54 + m_World.Step(timestep, m_VelocityIterations, m_PositionIterations);
56 55 }
57 56
58 57 void C_PhysicsManager::M_DestroyEntity(C_Entity* e)
2  physicsmanager.h
@@ -24,7 +24,7 @@ class C_PhysicsManager
24 24
25 25 std::vector<C_Entity*> m_Bodies;
26 26 public:
27   - void M_Simulate();
  27 + void M_Simulate(double timestep);
28 28 C_Entity* M_CreateDynamicEntity(const C_Model& m, float s=1.0f);
29 29 C_Entity* M_CreateDynamicEntity(unsigned short id, const C_Model& m, float s=1.0f);
30 30 C_Entity* M_CreateStaticEntity(const C_Model& m, float s=1.0f);
43 server.cpp
@@ -34,7 +34,7 @@ int main()
34 34 C_ContactListener contactlistener;
35 35 C_PhysicsManager* p = C_Singleton::M_PhysicsManager();
36 36 p->M_SetContactListener(new C_ContactListener);
37   - C_Timer* t = C_Singleton::M_Timer();
  37 + C_Timer* tmr = C_Singleton::M_Timer();
38 38
39 39 C_ModelManager* m = C_Singleton::M_ModelManager();
40 40 if(!m->M_Load("triangle", "test.2dmodel")) exit(1);
@@ -77,9 +77,24 @@ int main()
77 77 C_Packet packet;
78 78 C_Packet newplayers;
79 79
  80 + double t=0.0;
  81 + double accu=0.0;
  82 + const double dt=0.1;
  83 + C_Timer timer;
  84 + double currt=timer.M_Now();
  85 +
  86 + tmr->M_Reset();
  87 + timer.M_Reset();
80 88 while(run)
81 89 {
82   - t->M_Reset();
  90 + double newt=timer.M_Now();
  91 + double framet=newt-currt;
  92 + if(framet > 0.25)
  93 + {
  94 + framet=0.25;
  95 + }
  96 + currt=newt;
  97 +
83 98 while(sock.M_Receive(packet, 1, &ip, &port))
84 99 {
85 100 C_Connection* c = pool.M_Exists(ip,port);
@@ -157,15 +172,27 @@ int main()
157 172 {
158 173 *(*it) >> packet;
159 174 }
160   - pool.M_SendToAll(sock, packet);
161   - if(newplayers.M_Size())
  175 +
  176 + accu += framet;
  177 + while(accu >= dt)
  178 + {
  179 + p->M_Simulate(dt);
  180 + accu -= dt;
  181 + t += dt;
  182 + }
  183 +
  184 + if(100-(int)(timer.M_Get()*1000) == 0)
162 185 {
163   - pool.M_SendToAll(sock, newplayers);
164   - newplayers.M_Clear();
  186 + std::cout << "Sending" << std::endl;
  187 + pool.M_SendToAll(sock, packet);
  188 + if(newplayers.M_Size())
  189 + {
  190 + pool.M_SendToAll(sock, newplayers);
  191 + newplayers.M_Clear();
  192 + }
  193 + timer.M_Reset();
165 194 }
166 195 packet.M_Clear();
167   - g_Sleep(250-((int)t->M_Get()*1000));
168   - p->M_Simulate();
169 196 }
170 197 C_Singleton::M_DestroySingletons();
171 198 }
3  timer.cpp
@@ -19,7 +19,8 @@ double C_Timer::M_Now() const
19 19 #else
20 20 struct timeval cur;
21 21 gettimeofday(&cur, NULL);
22   - return (((cur.tv_sec*1000000)+(cur.tv_usec));
  22 + return ((cur.tv_sec*1000000)+(cur.tv_usec));
  23 + #endif
23 24 }
24 25
25 26 void C_Timer::M_Reset()

0 comments on commit d358e2e

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