Completely implemented new interpolation

This commit is contained in:
Anuken
2018-02-03 20:43:48 -05:00
parent 23c1edc3fa
commit da22f8fbce
8 changed files with 39 additions and 35 deletions

View File

@@ -22,7 +22,7 @@ allprojects {
appName = "Mindustry" appName = "Mindustry"
gdxVersion = '1.9.8' gdxVersion = '1.9.8'
aiVersion = '1.8.1' aiVersion = '1.8.1'
uCoreVersion = 'f8a3b56' uCoreVersion = 'f40fdf3'
} }
repositories { repositories {

View File

@@ -112,6 +112,7 @@ public class NetClient extends Module {
int enemies = 0; int enemies = 0;
ByteBuffer data = ByteBuffer.wrap(packet.data); ByteBuffer data = ByteBuffer.wrap(packet.data);
long time = data.getLong();
byte groupid = data.get(); byte groupid = data.get();
@@ -134,7 +135,7 @@ public class NetClient extends Module {
} }
data.position(data.position() + SyncEntity.getWriteSize((Class<? extends SyncEntity>) group.getType())); data.position(data.position() + SyncEntity.getWriteSize((Class<? extends SyncEntity>) group.getType()));
} else { } else {
entity.read(data); entity.read(data, time);
} }
} }
@@ -324,8 +325,9 @@ public class NetClient extends Module {
void sync(){ void sync(){
if(Timers.get("syncPlayer", playerSyncTime)){ if(Timers.get("syncPlayer", playerSyncTime)){
byte[] bytes = new byte[player.getWriteSize()]; byte[] bytes = new byte[player.getWriteSize() + 8];
ByteBuffer buffer = ByteBuffer.wrap(bytes); ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putLong(TimeUtils.millis());
player.write(buffer); player.write(buffer);
PositionPacket packet = new PositionPacket(); PositionPacket packet = new PositionPacket();

View File

@@ -123,8 +123,11 @@ public class NetServer extends Module{
}); });
Net.handleServer(PositionPacket.class, (id, packet) -> { Net.handleServer(PositionPacket.class, (id, packet) -> {
ByteBuffer buffer = ByteBuffer.wrap(packet.data);
long time = buffer.getLong();
Player player = connections.get(id); Player player = connections.get(id);
player.read(ByteBuffer.wrap(packet.data)); player.read(buffer, time);
}); });
Net.handleServer(ShootPacket.class, (id, packet) -> { Net.handleServer(ShootPacket.class, (id, packet) -> {
@@ -238,9 +241,10 @@ public class NetServer extends Module{
//calculate amount of entities to go into this packet //calculate amount of entities to go into this packet
int csize = Math.min(amount-i, maxsize); int csize = Math.min(amount-i, maxsize);
//create a byte array to write to //create a byte array to write to
byte[] bytes = new byte[csize*writesize + 1]; byte[] bytes = new byte[csize*writesize + 1 + 8];
//wrap it for easy writing //wrap it for easy writing
current = ByteBuffer.wrap(bytes); current = ByteBuffer.wrap(bytes);
current.putLong(TimeUtils.millis());
//write the group ID so the client knows which group this is //write the group ID so the client knows which group this is
current.put((byte)group.getID()); current.put((byte)group.getID());
} }

View File

@@ -1,7 +1,6 @@
package io.anuke.mindustry.entities; package io.anuke.mindustry.entities;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils; import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.graphics.Fx; import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.graphics.Shaders; import io.anuke.mindustry.graphics.Shaders;
@@ -261,22 +260,20 @@ public class Player extends SyncEntity{
data.putFloat(angle); data.putFloat(angle);
data.putShort((short)health); data.putShort((short)health);
data.put((byte)(dashing ? 1 : 0)); data.put((byte)(dashing ? 1 : 0));
data.putLong(TimeUtils.millis());
} }
@Override @Override
public void read(ByteBuffer data) { public void read(ByteBuffer data, long time) {
float x = data.getFloat(); float x = data.getFloat();
float y = data.getFloat(); float y = data.getFloat();
float angle = data.getFloat(); float angle = data.getFloat();
short health = data.getShort(); short health = data.getShort();
byte dashing = data.get(); byte dashing = data.get();
long time = data.getLong();
interpolator.targetrot = angle;
this.health = health; this.health = health;
this.dashing = dashing == 1; this.dashing = dashing == 1;
interpolator.targetrot = angle;
interpolator.time = 0f; interpolator.time = 0f;
interpolator.last.set(this.x, this.y); interpolator.last.set(this.x, this.y);
interpolator.target.set(x, y); interpolator.target.set(x, y);
@@ -289,9 +286,12 @@ public class Player extends SyncEntity{
i.time += 1f / i.spacing * Timers.delta(); i.time += 1f / i.spacing * Timers.delta();
lerp2(Tmp.v2.set(i.last), i.target, i.time); Mathf.lerp2(Tmp.v2.set(i.last), i.target, i.time);
x = Tmp.v2.x;
y = Tmp.v2.y;
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
if(isAndroid && i.target.dst(i.last) > 2f && Timers.get(this, "dashfx", 2)){ if(isAndroid && i.target.dst(i.last) > 2f && Timers.get(this, "dashfx", 2)){
Angles.translation(angle + 180, 3f); Angles.translation(angle + 180, 3f);
@@ -302,17 +302,6 @@ public class Player extends SyncEntity{
Angles.translation(angle + 180, 3f); Angles.translation(angle + 180, 3f);
Effects.effect(Fx.dashsmoke, x + Angles.x(), y + Angles.y()); Effects.effect(Fx.dashsmoke, x + Angles.x(), y + Angles.y());
} }
x = Tmp.v2.x;
y = Tmp.v2.y;
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
}
private Vector2 lerp2 (Vector2 v, Vector2 target, float alpha) {
v.x = (v.x) + ((target.x - v.x) * alpha);
v.y = (v.y) + ((target.y - v.y) * alpha);
return v;
} }
public Color getColor(){ public Color getColor(){

View File

@@ -14,14 +14,14 @@ public abstract class SyncEntity extends DestructibleEntity{
static{ static{
setWriteSize(Enemy.class, 4 + 4 + 2 + 2); setWriteSize(Enemy.class, 4 + 4 + 2 + 2);
setWriteSize(Player.class, 4 + 4 + 4 + 2 + 1 + 8); setWriteSize(Player.class, 4 + 4 + 4 + 2 + 1);
} }
public abstract void writeSpawn(ByteBuffer data); public abstract void writeSpawn(ByteBuffer data);
public abstract void readSpawn(ByteBuffer data); public abstract void readSpawn(ByteBuffer data);
public abstract void write(ByteBuffer data); public abstract void write(ByteBuffer data);
public abstract void read(ByteBuffer data); public abstract void read(ByteBuffer data, long time);
public abstract void interpolate(); public abstract void interpolate();
public int getWriteSize(){ public int getWriteSize(){

View File

@@ -1,16 +1,19 @@
package io.anuke.mindustry.entities.enemies; package io.anuke.mindustry.entities.enemies;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.entities.Bullet; import io.anuke.mindustry.entities.Bullet;
import io.anuke.mindustry.entities.BulletType; import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.SyncEntity; import io.anuke.mindustry.entities.SyncEntity;
import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.NetEvents; import io.anuke.mindustry.net.NetEvents;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.Entity; import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.entities.SolidEntity; import io.anuke.ucore.entities.SolidEntity;
import io.anuke.ucore.util.Angles; import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Mathf; import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Timer; import io.anuke.ucore.util.Timer;
import io.anuke.ucore.util.Tmp;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@@ -126,27 +129,33 @@ public class Enemy extends SyncEntity {
} }
@Override @Override
public void read(ByteBuffer data) { public void read(ByteBuffer data, long time) {
float x = data.getFloat(); float x = data.getFloat();
float y = data.getFloat(); float y = data.getFloat();
short angle = data.getShort(); short angle = data.getShort();
short health = data.getShort(); short health = data.getShort();
interpolator.target.set(x, y);
interpolator.targetrot = angle/2f;
this.health = health; this.health = health;
interpolator.targetrot = angle / 2f;
interpolator.time = 0f;
interpolator.last.set(this.x, this.y);
interpolator.target.set(x, y);
interpolator.spacing = Math.max(((TimeUtils.timeSinceMillis(time) / 1000f) * 60f), 1f);
} }
@Override @Override
public void interpolate() { public void interpolate() {
Interpolator i = interpolator; Interpolator i = interpolator;
if(i.target.dst(x, y) > 16){
set(i.target.x, i.target.y);
}
x = Mathf.lerpDelta(x, i.target.x, 0.4f); i.time += 1f / i.spacing * Timers.delta();
y = Mathf.lerpDelta(y, i.target.y, 0.4f);
Mathf.lerp2(Tmp.v2.set(i.last), i.target, i.time);
x = Tmp.v2.x;
y = Tmp.v2.y;
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f); angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
} }

View File

@@ -142,7 +142,7 @@ public class Packets {
@Override @Override
public void read(ByteBuffer buffer) { public void read(ByteBuffer buffer) {
data = new byte[SyncEntity.getWriteSize(Player.class)]; data = new byte[SyncEntity.getWriteSize(Player.class) + 8];
buffer.get(data); buffer.get(data);
} }
} }

View File

@@ -15,11 +15,11 @@ import static io.anuke.mindustry.Vars.headless;
import static io.anuke.mindustry.Vars.playerGroup; import static io.anuke.mindustry.Vars.playerGroup;
public class KryoRegistrator { public class KryoRegistrator {
public static boolean fakeLag = true; public static boolean fakeLag = false;
public static final int fakeLagAmount = 500; public static final int fakeLagAmount = 500;
static{ static{
Log.set(Log.LEVEL_ERROR); Log.set(Log.LEVEL_WARN);
Log.setLogger(new Logger(){ Log.setLogger(new Logger(){
public void log (int level, String category, String message, Throwable ex) { public void log (int level, String category, String message, Throwable ex) {