private CompensationWorld CreateWorld(int cmdSeq, DefaultBulletSegment segment) { // DebugUtil.AppendShootText(cmdSeq,"Append Segment,renderTime:{0}",segment); var serverTime = segment.ServerTime; CompensationWorld world; if (!_compensationWorlds.TryGetValue(serverTime, out world)) { world = _compensationWorldCreator.CreateCompensationWorld(serverTime); if (world == null) { _logger.ErrorFormat("create compensation world at time {0}, FAILED", segment.ServerTime); segment.BulletEntityAgent.IsValid = false; } else { _logger.DebugFormat("create compensation world at time {0}, SUCC", serverTime); _compensationWorlds[serverTime] = world; _compensationWorldList.Add(world); } } if (world == null) { return(null); } world.Self = segment.BulletEntityAgent.OwnerEntityKey; world.ExcludePlayerList = segment.BulletEntityAgent.ExcludePlayerList; return(world); }
private void InspectOldBulletHit(int cmdSeq, DefaultBulletSegment segment, CompensationWorld world) { RaycastHit camDirHit; if (world.Raycast(segment.RaySegment, out camDirHit, _hitboxLayerMask, cmdSeq)) { DebugUtil.AppendShootText(cmdSeq, "[Inspect]old world.Raycas sucess"); _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntityAgent, camDirHit, world); } }
public void MoveBullet(IBulletEntityAgent bulletEntityAgent, int renderTime, List <DefaultBulletSegment> allBulletSegments, int cmdSeq) { if (renderTime < bulletEntityAgent.NextFrameTime) { return; } var origin = bulletEntityAgent.Position; var velocity = bulletEntityAgent.Velocity; var gravity = bulletEntityAgent.Gravity; var velocityDecay = bulletEntityAgent.VelocityDecay; var distance = bulletEntityAgent.Distance; float interval = (renderTime - bulletEntityAgent.ServerTime) / 1000.0f; Vector3 oldOrigin = origin; // O(1) = O(0) + V(0) * t; origin.x = origin.x + velocity.x * interval; origin.y = origin.y + velocity.y * interval; origin.z = origin.z + velocity.z * interval; if (DebugConfig.DrawBulletLine) { RuntimeDebugDraw.Draw.DrawLine(oldOrigin, origin, Color.blue, 60f); Debug.DrawLine(oldOrigin, origin, Color.red, 60f); } // V(1) = V(0) + a * t Vector3 v = velocity; v.y = v.y - gravity * interval; v = v * Mathf.Pow(velocityDecay, interval); velocity = v; RaySegment raySegment = new RaySegment(); raySegment.Ray.origin = oldOrigin; var direction = origin - oldOrigin; raySegment.Ray.direction = direction; raySegment.Length = direction.magnitude; distance += raySegment.Length; // string rayInfo = string.Format("direction {0}, length {1},readInterval {2}, move {3} -> {4}, stepinterval {5}",direction,raySegment.Length, interval, oldOrigin, origin, _stepInterval); DefaultBulletSegment segment = DefaultBulletSegment.Allocate(renderTime, raySegment, bulletEntityAgent); allBulletSegments.Add(segment); if (Mathf.Approximately(v.magnitude, 0)) { bulletEntityAgent.IsValid = false; _logger.ErrorFormat("bullet velocity is zero, set to invalid"); DebugUtil.AppendShootText(cmdSeq, "[Bullet Move] bullet {0}invalid", bulletEntityAgent.OwnerEntityKey); } bulletEntityAgent.Position = origin; bulletEntityAgent.ServerTime = renderTime; bulletEntityAgent.Velocity = velocity; bulletEntityAgent.Distance = distance; bulletEntityAgent.NextFrameTime = renderTime + _stepInterval; // DebugUtil.AppendShootText(cmdSeq,"[Bullet Move]rayInfo :{0} //// bulletInfo :{1} ",rayInfo,bulletEntityAgent.ToMoveString()); }
//profiler:热点 world.Raycast -- hitboxHandler.EnableHitBox --HitBoxTransformProvider.SetActive() private void InspectNewBulletHit(int cmdSeq, DefaultBulletSegment segment, CompensationWorld world) { RaycastHit camDirHit; segment.BulletEntityAgent.IsNew = false; RaycastHit gunDirHit; var camRaySegment = segment.RaySegment; bool checkGunDirObstacle = false; while (segment.BulletEntityAgent.IsValid && world.Raycast(camRaySegment, out camDirHit, _hitboxLayerMask, cmdSeq)) { segment.BulletEntityAgent.SetIsNewInspect(true); if (!checkGunDirObstacle) { checkGunDirObstacle = true; //如果击中物体,从枪口向击中位置做射线检测,如果有物体,则使用枪口方向的结果 Vector3 startPosition = segment.BulletEntityAgent.GunEmitPosition; var target = camDirHit.point; var dir = target - startPosition; var blockCheckSegment = new RaySegment { Length = Vector3.Distance(target, startPosition) - GlobalConst.RaycastStepOffset, Ray = new Ray(startPosition, dir.normalized) }; while (segment.BulletEntityAgent.IsValid && world.Raycast(blockCheckSegment, out gunDirHit, _hitboxLayerMask, cmdSeq)) { segment.BulletEntityAgent.SetIsBlockInspect(true); _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntityAgent, gunDirHit, world); blockCheckSegment.Ray.origin = gunDirHit.point + blockCheckSegment.Ray.direction * GlobalConst.RaycastStepOffset; } } if (segment.BulletEntityAgent.IsValid) { _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntityAgent, camDirHit, world); camRaySegment.Ray.origin = camDirHit.point + camRaySegment.Ray.direction * GlobalConst.RaycastStepOffset; } } if (segment.BulletEntityAgent.IsValid) { if (!checkGunDirObstacle) { //如果没有击中物体,从枪口向第一帧末子弹到达的位置做检测,如果有物体,使用枪口方向的结果 var startPosition = segment.BulletEntityAgent.GunEmitPosition; var target = segment.RaySegment.Ray.direction * segment.RaySegment.Length + segment.RaySegment.Ray.origin; var dir = target - startPosition; var blockCheckSegment = new RaySegment { Length = Vector3.Distance(target, startPosition) - GlobalConst.RaycastStepOffset, Ray = new Ray(startPosition, dir.normalized) }; while (segment.BulletEntityAgent.IsValid && world.Raycast(blockCheckSegment, out gunDirHit, _hitboxLayerMask, cmdSeq)) { segment.BulletEntityAgent.SetIsBlockInspect(true); checkGunDirObstacle = true; _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntityAgent, gunDirHit, world); blockCheckSegment.Ray.origin = gunDirHit.point + blockCheckSegment.Ray.direction * GlobalConst.RaycastStepOffset; } } } }