1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
| import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.swing.JComponent; import javax.swing.JFrame;
public class PredictTest extends JComponent {
static final float TRESHOLD = 0.2f; static final boolean[] keyState = new boolean[0xffff]; static Server server = new Server(); static Client client = new Client(); static JComponent renderer = new JComponent() {
@Override public void paintComponent(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); drawPlayer(g, server.currentState.state, Color.BLUE.darker()); drawPlayer(g, client.interpolatedState.newestState.state, Color.RED.darker()); drawPlayer(g, client.interpolatedState.currentState.state, Color.GREEN.darker()); drawPlayer(g, client.predictedState.currentState.state, Color.BLACK); g.drawLine(400, 0, 400, getHeight()); }
void drawPlayer(Graphics g, State state, Color color) { g.setColor(color); g.fillOval((int) state.x - 10, (int) state.y - 10, 20, 20); } };
public static void main(String[] args) { renderer.addKeyListener(new KeyAdapter() {
@Override public void keyPressed(KeyEvent e) { keyState[e.getKeyCode()] = true; }
@Override public void keyReleased(KeyEvent e) { keyState[e.getKeyCode()] = false; } });
JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 640, 480); frame.add(renderer); frame.setVisible(true);
renderer.requestFocusInWindow();
new Thread(new Runnable() {
public void run() { server.gameLoop(); } }).start(); client.gameLoop(server); }
static void sleep(long millis) { try { Thread.sleep(millis); } catch (Exception ex) { ex.printStackTrace(); } }
static TimedState updateState(TimedState state, TimedInput timedInput, boolean doCollisions) { long timeDeltaMillis = timedInput.time - state.time; float x = state.state.x + timedInput.input.vx * timeDeltaMillis / 1000f; float y = state.state.y + timedInput.input.vy * timeDeltaMillis / 1000f; if (doCollisions) { x = Math.min(400, x); } TimedState newState = new TimedState(timedInput.time, new State(x, y)); return newState; }
static class Client { PredictedState predictedState = new PredictedState(new TimedState(0, new State(0, 0))); InterpolatedState interpolatedState = new InterpolatedState();
void gameLoop(Server server) { predictedState = new PredictedState(new TimedState(System.currentTimeMillis(), server.currentState.state)); while (true) { float dx = (keyState[KeyEvent.VK_LEFT] ? -150 : 0) + (keyState[KeyEvent.VK_RIGHT] ? 150 : 0); float dy = (keyState[KeyEvent.VK_UP] ? -150 : 0) + (keyState[KeyEvent.VK_DOWN] ? 150 : 0); long now = System.currentTimeMillis(); TimedInput timedInput = new TimedInput(now, new Input(dx, dy)); server.in.add(timedInput); TimedState timedStateFromServer = server.out.remove(); predictedState.update(timedInput, timedStateFromServer); interpolatedState.update(now, timedStateFromServer); renderer.repaint(); sleep(16); } } }
static class Server { DelayedFifo<TimedInput> in = new DelayedFifo(); DelayedFifo<TimedState> out = new DelayedFifo();
TimedState currentState;
Server() { currentState = new TimedState(0, new State(100, 100)); }
void gameLoop() { while (true) { tick(); renderer.repaint(); sleep(100); } }
private void tick() { while (true) { TimedInput timedInput = in.remove(); if (timedInput == null) { break; } currentState = updateState(currentState, timedInput, true); }
out.add(currentState); } }
static class DelayedFifo<T> { List<TimedEntry> list = Collections.synchronizedList(new ArrayList());
void add(final T object) { list.add(new TimedEntry(System.currentTimeMillis() + getDelay(), object)); }
T remove() { if (list.isEmpty()) { return null; }
TimedEntry entry = list.get(0); if (System.currentTimeMillis() > entry.time) { return (T) list.remove(0).object; } return null; }
private long getDelay() { return 300 + (int) (Math.random() * 100); }
class TimedEntry {
long time; Object object;
TimedEntry(long time, Object object) { this.time = time; this.object = object; } } }
static class Input { final float vx; final float vy;
public Input(float vx, float vy) { this.vx = vx; this.vy = vy; } }
static class State { final float x; final float y;
public State(float x, float y) { this.x = x; this.y = y; }
public float distance(State s) { float dx = s.x - x; float dy = s.y - y; return (float) Math.sqrt(dx*dx + dy*dy); }
@Override public String toString() { return "State("+x+","+y+")"; } }
static class TimedState { final long time; final State state;
public TimedState(long time, State state) { this.time = time; this.state = state; }
@Override public String toString() { return "TimedState(" + time + "," + state.toString() + ")"; } }
static class TimedInput {
final long time; final Input input;
public TimedInput(long time, Input input) { this.time = time; this.input = input; } }
static class PredictedState { InputAndStateList moveList = new InputAndStateList(); TimedState currentState;
PredictedState(TimedState startState) { this.currentState = startState; }
void update(TimedInput timedInput, TimedState correctState) { currentState = updateState(currentState, timedInput, false); InputAndState move = new InputAndState(timedInput.input, currentState); moveList.add(move); currentState = moveList.correct(currentState, correctState); } }
static class InputAndState { final Input input; final TimedState timedState;
public InputAndState(Input input, TimedState timedState) { this.input = input; this.timedState = timedState; }
TimedInput getTimedInput() { return new TimedInput(timedState.time, input); } }
static class InputAndStateList { List<InputAndState> list = new ArrayList();
void add(InputAndState move) { list.add(move); }
TimedState correct(TimedState currentState, TimedState serverState) { if (serverState != null) { removeBefore(serverState.time); if (!isOldestWithinThresholdTo(serverState.state)) { System.out.println("perform correction " + list.get(0).timedState + " != " + serverState); return update(serverState); } } return currentState; }
private void removeBefore(long time) { while (list.size() > 0 && list.get(0).timedState.time < time) { list.remove(0); } }
private boolean isOldestWithinThresholdTo(State state) { return (list.size() > 0 && list.get(0).timedState.state.distance(state) <= TRESHOLD); }
private TimedState update(TimedState currentState) { for (InputAndState oldMove : list) { currentState = updateState(currentState, oldMove.getTimedInput(), false); } return currentState; } }
static class InterpolatedState { List<TimedState> timedStateList = new ArrayList(); long prevStateTime = 0; TimedState prevState = new TimedState(0, new State(0, 0)); long newestStateTime = 0; TimedState newestState = new TimedState(0, new State(0, 0)); TimedState currentState = new TimedState(0, new State(0, 0)); long currentTime = 0; long prevTime = 0; float averageTimeBetweenPackets = 150; long timeCorrection = 0; float averageJitter = 50;
void update(long now, TimedState timedState) { long delta = now - prevTime; prevTime = now; if (timedState != null) { timedStateList.add(timedState); prevState = newestState; prevStateTime = newestStateTime; newestState = timedState; newestStateTime = now;
if (prevState.time != 0 && newestState.time != 0) { if (currentTime == 0) { currentTime = newestState.time; } if (Math.abs(newestState.time - prevState.time) < 1000) { float timeBetweenPackets = newestState.time - prevState.time; averageTimeBetweenPackets += (timeBetweenPackets - averageTimeBetweenPackets) * 0.1f;
long recieveDelta = newestStateTime - prevStateTime; long sendDelta = newestState.time - prevState.time; long jitter = recieveDelta - sendDelta; averageJitter += (jitter - averageJitter) * 0.1f;
long currentTimeBehindNewestState = newestState.time - currentTime; long targetTimeBehindNewestState = (long) (averageTimeBetweenPackets + averageJitter * 2); timeCorrection = targetTimeBehindNewestState - currentTimeBehindNewestState; } } }
long correction = 0; if (Math.abs(timeCorrection) > 0) { correction = timeCorrection / Math.abs(timeCorrection); } timeCorrection -= correction; currentTime += (delta - correction); if (timedStateList.size() >= 2) { currentTime = Math.min(timedStateList.get(timedStateList.size() - 1).time, currentTime); currentTime = Math.max(timedStateList.get(timedStateList.size() - 1).time - 500, currentTime);
while (timedStateList.size() >= 2 && timedStateList.get(1).time < currentTime) { timedStateList.remove(0); } TimedState s1 = timedStateList.get(0); TimedState s2 = timedStateList.get(1); float t = (currentTime - s1.time) / (float) (s2.time - s1.time); currentState = new TimedState(currentTime, new State( s1.state.x + t * (s2.state.x - s1.state.x), s1.state.y + t * (s2.state.y - s1.state.y) )); } } } } |