private void HandleEntityData(EntityDataPacket p, float serverDeltaTime = 0) { Entity e = this.entityWorld.EntityManager.GetEntity(p.EntityId); if (e != null) { TransformComponent transform = e.GetComponent <TransformComponent>(); System.Diagnostics.Debug.Assert(transform != null, transform.GetType() + " not found"); // TODO: better client side prediction: /* position + (velocity * network_latency) * position + (velocity * network_latency) + (acceleration * network_latency^2) / 2 * might be inaccurate, due to high possible latency, > 100ms * * latency too high: extrapolate position using above (dead reckoning)? * latency in acceptable range: lerp predicted position and received position */ // adjust velocity continuosly, unless difference is too big, then change position float deltaValue = 30f; //pixel (unit; due to new scale) offset Vector2 dataPosition = new Vector2(p.PositionX, p.PositionY); if (!this.IsInbetween(transform.Position.X, dataPosition.X, deltaValue) || !this.IsInbetween(transform.Position.Y, dataPosition.Y, deltaValue)) { transform.Position = dataPosition; } if (!this.IsInbetween(transform.Rotation, p.Rotation, deltaValue / 120f)) { transform.Rotation = p.Rotation; } MotionComponent motion = e.GetComponent <MotionComponent>(); if (motion != null) { motion.Velocity = new Vector2(p.VelocityX, p.VelocityY); motion.AngularVelocity = p.AngularVelocity; //serverDeltaTime = serverDeltaTime; //time in totalseconds float convergeFactor = 0.1f; //percentage for adding velocity so that mismatched position catches up motion.Velocity += new Vector2((dataPosition.X - transform.Position.X) * convergeFactor * 100, (dataPosition.Y - transform.Position.Y) * convergeFactor * 100); motion.AngularVelocity += ((p.Rotation - transform.Rotation) * convergeFactor * 100); } } }
private EntityDataPacket CreateEntityStatePacket(Entity e) { EntityDataPacket p = new EntityDataPacket(); TransformComponent position = e.GetComponent <TransformComponent>(); p.EntityId = e.EntityId; p.PositionX = position.Position.X; p.PositionY = position.Position.Y; p.Rotation = position.Rotation; MotionComponent motion = e.GetComponent <MotionComponent>(); if (motion != null) { p.VelocityX = motion.Velocity.X; p.VelocityY = motion.Velocity.Y; p.AngularVelocity = motion.AngularVelocity; } return(p); }