public static NdArray <Real> SingleInputForward(NdArray <Real> x, Real beta, Real betaInv, IFunction <Real> softplus) { Real[] y = new Real[x.Data.Length]; for (int b = 0; b < x.BatchCount; b++) { for (int i = 0; i < x.Length; i++) { y[i + b * x.Length] = x.Data[i + b * x.Length] * beta; } //0との比較が必要 Real maxval = 0; for (int i = 0; i < x.Length; i++) { if (maxval < y[i + b * x.Length]) { maxval = y[i + b * x.Length]; } } for (int i = 0; i < x.Length; i++) { y[i + b * x.Length] = (maxval + Math.Log(1.0f + Math.Exp(-Math.Abs(x.Data[i + b * x.Length] * beta)))) * betaInv; } } return(NdArray.Convert(y, x.Shape, x.BatchCount, softplus)); }
private void GameTick(object state) { if (World == null) { return; } if (_initiated) { _tickTime++; if (World.Player != null && Client.HasSpawned) { // player.IsSpawned = Spawned; if (World.Player.IsFlying != _flying) { _flying = World.Player.IsFlying; McpeAdventureSettings settings = McpeAdventureSettings.CreateObject(); settings.flags = GetAdventureFlags(); Client.SendPacket(settings); //SendPlayerAbilities(player); } var pos = (PlayerLocation)World.Player.KnownPosition.Clone(); if (pos.DistanceTo(_lastSentLocation) > 0.0f || MathF.Abs(pos.HeadYaw - _lastSentLocation.HeadYaw) > 0.0f || MathF.Abs(pos.Pitch - _lastSentLocation.Pitch) > 0.0f) { SendLocation(pos); _lastSentLocation = pos; } if ((pos.DistanceTo(_lastLocation) > 16f || MathF.Abs(pos.HeadYaw - _lastLocation.HeadYaw) >= 5.0f) && (_tickTime - _lastPrioritization >= 10)) { World.ChunkManager.FlagPrioritization(); SendLocation(pos); _lastLocation = pos; UnloadChunks(new ChunkCoordinates(pos), Client.ChunkRadius + 3); _lastPrioritization = _tickTime; } } if (_tickTime % 20 == 0 && CustomConnectedPong.CanPing) { Client.SendPing(); } World.Player.OnTick(); World.EntityManager.Tick(); World.PhysicsEngine.Tick(); } }
private static Vector3 FlipX(Vector3 origin, Vector3 size) { if (origin.X >= 0) { origin.X = -(((MathF.Abs(origin.X) / size.X) + 1) * size.X); } else { origin.X = ((MathF.Abs(origin.X) / size.X) - 1) * size.X; } return(origin); }
public void UpdatePlayerPosition(PlayerLocation location) { var oldPosition = Player.KnownPosition; if (!ChunkManager.TryGetChunk(new ChunkCoordinates(location), out _)) { Player.WaitingOnChunk = true; } Player.KnownPosition = location; Player.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, location)); }
public void Update(GameTime elapsed) { float dt = (float)elapsed.ElapsedGameTime.TotalMilliseconds / 50f; foreach (var entity in PhysicsEntities.ToArray()) { try { if (entity is Entity e) { if (e.NoAi) { continue; } UpdateEntity(e, dt); TruncateVelocity(e, dt); var boundingBox = e.GetBoundingBox(e.KnownPosition); bool anySolid = false; foreach (var corner in boundingBox.GetCorners().Where(x => x.Y == boundingBox.Min.Y)) { var blockcoords = new BlockCoordinates( new PlayerLocation( corner.X, Math.Floor(corner.Y - 0.01f), corner.Z)); var block = World.GetBlock(blockcoords.X, blockcoords.Y, blockcoords.Z); var blockBoundingBox = block.GetBoundingBox(blockcoords); //..onGround = onGround || block.Solid; if (block.Solid && MathF.Abs(boundingBox.Min.Y - blockBoundingBox.Max.Y) <= 0.05f) { anySolid = true; break; } } e.KnownPosition.OnGround = anySolid; } } catch (Exception ex) { Log.Warn(ex, $"Entity tick threw exception: {ex.ToString()}"); } } sw.Restart(); }
public void UpdateEntityPosition(long entityId, PlayerLocation position, bool relative = false, bool updateLook = false, bool updatePitch = false) { if (EntityManager.TryGet(entityId, out Entity entity)) { entity.KnownPosition.OnGround = position.OnGround; if (!relative) { var oldPosition = entity.KnownPosition; entity.KnownPosition = position; //if (entity is PlayerMob p) { entity.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, position)); } } else { var oldPosition = entity.KnownPosition; float offset = 0f; /* if (this.ServerType == ServerType.Bedrock) * { * offset = (float) entity.PositionOffset; * }*/ //TODO: Fix position offset entity.KnownPosition.X += position.X; entity.KnownPosition.Y += (position.Y - offset); entity.KnownPosition.Z += position.Z; //if (entity is PlayerMob p) { entity.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, entity.KnownPosition)); } //entity.KnownPosition.Move(position); } if (updateLook) { //entity.KnownPosition.Yaw = position.Yaw; if (updatePitch) { entity.KnownPosition.Pitch = position.Pitch; } entity.KnownPosition.HeadYaw = position.HeadYaw; // entity.UpdateHeadYaw(position.HeadYaw); } } }
public override void genericMaskUpdate(NdArray <Real> mask, NdArray <Real> weight) { Real[] scoreDrop = new Real[weight.Length]; Real[] scoreGrow = new Real[weight.Length]; for (int i = 0; i < scoreDrop.Length; i++) { scoreDrop[i] = Math.Abs(mask.Data[i] * weight.Data[i]);//マスク前の重みにマスクを掛ける 元の実装だとここに1e-5の正規乱数が足される scoreGrow[i] = mask.Grad[i]; } //マスクと重みを更新 Update(scoreDrop, scoreGrow, mask, weight); }
private bool DetectOnGround(Entity e) { var entityBoundingBox = e.BoundingBox; var offset = 0f; //if (Math.Round(entityBoundingBox.Min.Y) <= (int) entityBoundingBox.Min.Y) if (entityBoundingBox.Min.Y % 1 < 0.05f) { offset = -1f; } bool foundGround = false; foreach (var corner in entityBoundingBox.GetCorners() .Where(x => Math.Abs(x.Y - entityBoundingBox.Min.Y) < 0.001f)) { var blockcoords = new BlockCoordinates( new PlayerLocation(corner.X, Math.Floor(corner.Y + offset), corner.Z)); var block = World.GetBlockState(blockcoords.X, blockcoords.Y, blockcoords.Z); if (block?.Model == null || !block.Block.Solid) { continue; } foreach (var box in block.Model.GetBoundingBoxes(blockcoords).OrderBy(x => x.Max.Y)) { var yDifference = MathF.Abs(entityBoundingBox.Min.Y - box.Max.Y); // <= 0.01f if (yDifference > 0.015f) { continue; } if (box.Intersects(entityBoundingBox)) { foundGround = true; } //return true; } } return(foundGround); }
public void Update(GameTime elapsed) { float dt = (float)elapsed.ElapsedGameTime.TotalMilliseconds / 50f; foreach (var entity in PhysicsEntities.ToArray()) { try { if (entity is Entity e) { if (e.NoAi) { continue; } //if (!e.AlwaysTick && !e.IsRendered) continue; var original = e.KnownPosition.ToVector3(); var velocity = e.Velocity; velocity = UpdateEntity(e, velocity, out var playerY); e.KnownPosition.Move(velocity * dt); if (playerY > e.KnownPosition.Y) { e.KnownPosition.Y = playerY; } UpdateOnGround(e); //e.KnownPosition.OnGround = onGround; e.DistanceMoved += MathF.Abs(Vector3.Distance(original, e.KnownPosition.ToVector3())); //TruncateVelocity(e, dt); } } catch (Exception ex) { Log.Warn(ex, $"Entity tick threw exception: {ex.ToString()}"); } } sw.Restart(); }
public void Angleでの値は6桁精度で取れる() { void assert(Table.Angle angle) { var s1 = Table.Sin(angle); var c1 = Table.Cos(angle); var theta = angle.ToRadian(); var s2 = M.Sin(theta); var c2 = M.Cos(theta); Assert.True(M.Abs(s1 - s2) < 1e-6f); Assert.True(M.Abs(c1 - c2) < 1e-6f); } for (int i = 0; i < 256; i++) { assert(new Table.Angle((byte)i)); } }
private void UpdateOnGround(Entity e) { var entityBoundingBox = e.GetBoundingBox(new Vector3(e.KnownPosition.X, MathF.Round(e.KnownPosition.Y, 2, MidpointRounding.ToZero), e.KnownPosition.Z)); bool anySolid = false; var yPos = e.KnownPosition.Y; var offset = 0f; if (Math.Round(yPos, 2) % 1 == 0) { offset = 1f; } foreach (var corner in entityBoundingBox.GetCorners().Where(x => Math.Abs(x.Y - entityBoundingBox.Min.Y) < 0.001f)) { var blockcoords = new BlockCoordinates( new PlayerLocation( corner.X, Math.Floor(corner.Y - offset), corner.Z)); var block = World.GetBlockState(blockcoords.X, blockcoords.Y, blockcoords.Z); var blockBoundingBox = block.Model.GetBoundingBox(blockcoords); //..onGround = onGround || block.Solid; if (block.Block.Solid && blockBoundingBox.Contains(corner) != ContainmentType.Disjoint) { var partBoundingBox = block.Model.GetPartBoundingBox(blockcoords, entityBoundingBox); if (partBoundingBox.HasValue) { var yDifference = MathF.Abs(entityBoundingBox.Min.Y - partBoundingBox.Value.Max.Y); // <= 0.01f if (yDifference <= 0.01f) { anySolid = true; break; } } } } e.KnownPosition.Y = yPos; e.KnownPosition.OnGround = anySolid; }
public void Atan2誤差1パーセント以下の精度で計算できる() { void assertYX(T y, T x) { var t1 = Table.Atan2(y, x); var t2 = M.Atan2(y, x); Assert.True(M.Abs(t1 - t2) < 2e-3); } void assert(T theta) { var(y, x) = (M.Cos(theta), M.Sin(theta)); assertYX(y, x); } var r = new Random(); for (int i = 0; i < 1000; i++) { var theta = (float)r.NextDouble() * 1000 - 500; assert(theta); } for (int i = 0; i < 1000; i++) { var y = (float)r.NextDouble() * 1000 - 500; var x = (float)r.NextDouble() * 1000 - 500; assertYX(y, x); } foreach (var theta in _cornerCase) { assert(theta); } for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { assertYX(x, y); } } }
public void テーブル上にある値は6桁精度で取れる() { void assert(T theta) { var s1 = Table.Sin(theta); var c1 = Table.Cos(theta); var s2 = M.Sin(theta); var c2 = M.Cos(theta); Assert.True(M.Abs(s1 - s2) < 1e-6f); Assert.True(M.Abs(c1 - c2) < 1e-6f); } for (int i = -256; i <= 256; i++) { var theta = 2 * M.PI / 256 * i; assert(theta); } foreach (var theta in _cornerCase) { assert(theta); } }
public void 精度が悪いところでも誤差2パーセント程度の精度で取れる() { void assert(T theta) { var s1 = Table.Sin(theta); var c1 = Table.Cos(theta); var s2 = M.Sin(theta); var c2 = M.Cos(theta); Assert.True(M.Abs(s1 - s2) < 1.3e-2f); Assert.True(M.Abs(c1 - c2) < 1.3e-2f); } for (int i = -256; i <= 256; i++) { var theta = 2 * M.PI / 256 * (i + 0.5f); assert(theta); } foreach (var theta in _cornerCase) { assert(theta); } }
/// <inheritdoc cref="INumber{TSelf}.Abs(TSelf)" /> public static float Abs(float value) => MathF.Abs(value);
public void Update(GameTime elapsed) { float dt = ((float)elapsed.ElapsedGameTime.TotalSeconds); //if (sw.ElapsedMilliseconds) // dt = (float) sw.ElapsedMilliseconds / 1000f; Hit.Clear(); foreach (var entity in PhysicsEntities.ToArray()) { try { if (entity is Entity e) { if (e.NoAi) { continue; } bool wasColliding = e.IsCollidingWithWorld; //TruncateVelocity(e, dt); var velocity = e.Velocity; if (e.IsInWater && velocity.Y < 0f) { velocity.Y *= 0.8f; } else if (e.IsInLava) { // velocity.Y *= 0.5f; } if (!e.IsFlying && !e.KnownPosition.OnGround && e.IsAffectedByGravity) { velocity -= new Vector3(0f, (float)(e.Gravity * dt), 0f); //var modifier = new Vector3(1f, (float) (1f - (e.Gravity * dt)), 1f); //velocity *= modifier; } var rawDrag = (float)(1f - ((e.Drag * 0.91f) * dt)); velocity *= new Vector3(rawDrag, 1f, rawDrag); var position = e.KnownPosition; var preview = position.PreviewMove(velocity * dt); if (e.IsSneaking && e.KnownPosition.OnGround) { var newFeetBlock = e.Level?.GetBlock(preview + Vector3.Down); if (!newFeetBlock.Solid) { velocity = new Vector3(-velocity.X, velocity.Y, -velocity.Z); } } bool onGround = e.KnownPosition.OnGround; // if (velocity != Vector3.Zero) { var boundingBox = e.GetBoundingBox(preview); Bound bound = new Bound(World, boundingBox, preview); if (bound.GetIntersecting(boundingBox, out var blocks)) { velocity = AdjustForY( e.GetBoundingBox(new Vector3(position.X, preview.Y, position.Z)), blocks, velocity, position, out float?yCollisionPoint); if (yCollisionPoint.HasValue) { if (yCollisionPoint > position.Y) { //We hit our head. onGround = false; } else { if (!onGround) { onGround = true; } } } Hit.AddRange(blocks.Select(x => x.box)); //var solid = blocks.Where(b => b.block.Solid && b.box.Max.Y > position.Y).ToArray(); var solid = blocks.Where( b => b.block.Solid && b.box.Max.Y > position.Y && b.block.CanCollide()).ToArray(); if (solid.Length > 0) { var heighest = solid.OrderByDescending(x => x.box.Max.Y).FirstOrDefault(); if (MathF.Abs(heighest.box.Max.Y - boundingBox.Min.Y) <= 0.65f && e.KnownPosition.OnGround && !e.IsFlying) { //if (!heighest.block.BlockState.Model // .GetIntersecting(heighest.coordinates, boundingBox) // .Any(x => x.Max.Y > heighest.box.Max.Y)) //if (!blocks.Any(x => x.)) { e.KnownPosition.Y = (float)heighest.box.Max.Y; } } if (!wasColliding) { //var min = Vector3.Transform(boundingBox.Min, // Matrix.CreateRotationY(-MathHelper.ToRadians(position.HeadYaw))); //var max = Vector3.Transform(boundingBox.Max, // Matrix.CreateRotationY(-MathHelper.ToRadians(position.HeadYaw))); var min = boundingBox.Min; var max = boundingBox.Max; var minX = min.X; var maxX = max.X; var previewMinX = new Vector3(minX, preview.Y, preview.Z); bool checkX = false; if (!solid.Any( x => { var contains = x.box.Contains(previewMinX); return(contains != ContainmentType.Contains && contains != ContainmentType.Intersects); })) { previewMinX = new Vector3(maxX, preview.Y, preview.Z); if (solid.Any( x => { var contains = x.box.Contains(previewMinX); return(contains != ContainmentType.Contains && contains != ContainmentType.Intersects); })) { checkX = true; } } else { checkX = true; } if (checkX) { for (float x = 1f; x > 0f; x -= 0.1f) { Vector3 c = (position - preview) * new Vector3(x, 1f, 1f) + position; if (solid.All( s => { var contains = s.box.Contains(c); return(contains != ContainmentType.Contains && contains != ContainmentType.Intersects); })) { velocity = new Vector3(c.X - position.X, velocity.Y, velocity.Z); break; } } } var minZ = min.Z; var maxZ = max.Z; var previewMinZ = new Vector3(preview.X, preview.Y, minZ); bool checkZ = false; if (!solid.Any( x => { var contains = x.box.Contains(previewMinZ); return(contains != ContainmentType.Contains && contains != ContainmentType.Intersects); })) { previewMinZ = new Vector3(preview.X, preview.Y, maxZ); if (solid.Any( x => { var contains = x.box.Contains(previewMinZ); return(contains != ContainmentType.Contains && contains != ContainmentType.Intersects); })) { checkZ = true; } } else { checkZ = true; } if (checkZ) { for (float x = 1f; x > 0f; x -= 0.1f) { Vector3 c = (position - preview) * new Vector3(1f, 1f, x) + position; if (solid.All( s => { var contains = s.box.Contains(c); return(contains != ContainmentType.Contains && contains != ContainmentType.Intersects); })) { velocity = new Vector3(velocity.X, velocity.Y, c.Z - position.Z); break; } } } } } } } e.Velocity = velocity; var beforeMove = e.KnownPosition.ToVector3(); e.KnownPosition.Move(velocity * dt); // if (e is PlayerMob p) { e.DistanceMoved += MathF.Abs(Vector3.Distance(beforeMove, e.KnownPosition.ToVector3())); } //var rawDrag = (float) (1f - (e.Drag * dt)); //e.Velocity = velocity;// * new Vector3(1f, 0.98f, 1f); //e.KnownPosition.Move(e.Velocity * dt); TruncateVelocity(e, dt); /*var feetBlock = e.Level.GetBlockState(e.KnownPosition.GetCoordinates3D()); * * if (!feetBlock.Block.Solid) * { * e.KnownPosition.OnGround = false; * } * else * { * * }*/ //if (MathF.Abs(e.Velocity.Y) < 0.000001f) { e.KnownPosition.OnGround = MathF.Abs(e.Velocity.Y) < 0.000001f || onGround; } { //e.KnownPosition.OnGround = true; } } } catch (Exception ex) { Log.Warn(ex, $"Entity tick threw exception: {ex.ToString()}"); } } if (Hit.Count > 0) { LastKnownHit = Hit.ToArray(); } sw.Restart(); }
private void UpdateMovementInput(GameTime gt) { if (!_allowMovementInput) { return; } var moveVector = Vector3.Zero; var now = DateTime.UtcNow; if (Player.CanFly) { if (InputManager.IsPressed(InputCommand.ToggleCameraFree)) { Player.IsFlying = !Player.IsFlying; } else if (InputManager.IsDown(InputCommand.MoveUp) || InputManager.IsDown(InputCommand.Jump)) { if ((InputManager.IsBeginPress(InputCommand.MoveUp) || InputManager.IsBeginPress(InputCommand.Jump)) && now.Subtract(_lastUp).TotalMilliseconds <= 125) { Player.IsFlying = !Player.IsFlying; } _lastUp = now; } } float speedFactor = (float)Player.CalculateMovementSpeed(); if (InputManager.IsDown(InputCommand.MoveForwards)) { moveVector.Z += 1; if (!Player.IsSprinting && Player.CanSprint) { if (InputManager.IsBeginPress(InputCommand.MoveForwards) && now.Subtract(_lastForward).TotalMilliseconds <= 125) { Player.IsSprinting = true; } } _lastForward = now; } else { if (Player.IsSprinting) { Player.IsSprinting = false; } } if (InputManager.IsDown(InputCommand.MoveBackwards)) { moveVector.Z -= 1; } if (InputManager.IsDown(InputCommand.MoveLeft)) { moveVector.X += 1; } if (InputManager.IsDown(InputCommand.MoveRight)) { moveVector.X -= 1; } if (Player.IsFlying) { //speedFactor *= 1f + (float)Player.FlyingSpeed; //speedFactor *= 2.5f; if (InputManager.IsDown(InputCommand.MoveUp)) { moveVector.Y += 1; } if (InputManager.IsDown(InputCommand.MoveDown)) { moveVector.Y -= 1; Player.IsSneaking = true; } else { Player.IsSneaking = false; } } else { if (Player.FeetInWater && InputManager.IsDown(InputCommand.MoveUp)) { Player.Velocity = new Vector3(Player.Velocity.X, 1f * speedFactor, Player.Velocity.Z); } else if (!Player.IsInWater && Player.KnownPosition.OnGround && (InputManager.IsDown(InputCommand.Jump) || InputManager.IsDown(InputCommand.MoveUp))) { if (Player.Velocity.Y <= 0.00001f && Player.Velocity.Y >= -0.00001f && Math.Abs(LastVelocity.Y - Player.Velocity.Y) < 0.0001f) { // moveVector.Y += 42f; // Player.Velocity += new Vector3(0f, 4.65f, 0f); // //, 0); Player.Jump(); } } if (!Player.IsInWater) //Sneaking in water is not a thing. { if (InputManager.IsDown(InputCommand.MoveDown) || InputManager.IsDown(InputCommand.Sneak)) { Player.IsSneaking = true; } else //if (_prevKeyState.IsKeyDown(KeyBinds.Down)) { Player.IsSneaking = false; } } } WasInWater = Player.FeetInWater; // if (moveVector != Vector3.Zero) { var velocity = moveVector * speedFactor; velocity = Vector3.Transform(velocity, Matrix.CreateRotationY(-MathHelper.ToRadians(Player.KnownPosition.HeadYaw))); velocity = Player.Level.PhysicsEngine.UpdateEntity(Player, velocity, out _); if (Player.IsFlying) { if ((Player.Velocity * new Vector3(1, 1, 1)).Length() < velocity.Length()) { var old = Player.Velocity; Player.Velocity += new Vector3(velocity.X - old.X, velocity.Y - old.Y, velocity.Z - old.Z); } else { Player.Velocity = new Vector3(velocity.X, velocity.Y, velocity.Z); } } else { var old = Player.Velocity; var oldLength = (Player.Velocity * new Vector3(1, 0, 1)).Length(); if (oldLength < velocity.Length()) { Player.Velocity += new Vector3(velocity.X - old.X, 0, velocity.Z - old.Z); } else { Player.Velocity = new Vector3(MathF.Abs(old.X) < 0.0001f ? velocity.X : old.X, Player.Velocity.Y, MathF.Abs(old.Z) < 0.0001f ? velocity.Z : old.Z); } } //speedFactor *= 20; //Player.Velocity += (moveVector * speedFactor);// new Vector3(moveVector.X * speedFactor, moveVector.Y * (speedFactor), moveVector.Z * speedFactor); } LastSpeedFactor = speedFactor; if (IgnoreNextUpdate) { IgnoreNextUpdate = false; } else { var checkMouseInput = true; if (GamePadInputListener != null && GamePadInputListener.IsConnected) { var inputValue = GamePadInputListener.GetCursorPosition(); if (inputValue != Vector2.Zero) { checkMouseInput = false; var look = (new Vector2((inputValue.X), (inputValue.Y)) * (float)GamepadSensitivity) * (float)(gt.ElapsedGameTime.TotalSeconds); look = -look; Player.KnownPosition.HeadYaw -= look.X; Player.KnownPosition.Pitch -= look.Y; Player.KnownPosition.HeadYaw = MathUtils.NormDeg(Player.KnownPosition.HeadYaw); Player.KnownPosition.Pitch = MathHelper.Clamp(Player.KnownPosition.Pitch, -89.9f, 89.9f); } } if (checkMouseInput) { var e = MouseInputListener.GetCursorPosition(); var centerX = Graphics.Viewport.Width / 2; var centerY = Graphics.Viewport.Height / 2; if (e.X < 10 || e.X > Graphics.Viewport.Width - 10 || e.Y < 10 || e.Y > Graphics.Viewport.Height - 10) { _previousMousePosition = new Vector2(centerX, centerY); Mouse.SetPosition(centerX, centerY); IgnoreNextUpdate = true; } else { var mouseDelta = _previousMousePosition - e; //this.GlobalInputManager.CursorInputListener.GetCursorPositionDelta(); var look = (new Vector2((-mouseDelta.X), (mouseDelta.Y)) * (float)CursorSensitivity) * (float)(gt.ElapsedGameTime.TotalSeconds); look = -look; Player.KnownPosition.HeadYaw -= look.X; Player.KnownPosition.Pitch -= look.Y; Player.KnownPosition.HeadYaw = MathUtils.NormDeg(Player.KnownPosition.HeadYaw); Player.KnownPosition.Pitch = MathHelper.Clamp(Player.KnownPosition.Pitch, -89.9f, 89.9f); //Player.KnownPosition.Pitch = MathHelper.Clamp(Player.KnownPosition.Pitch + look.Y, -89.9f, 89.9f); // Player.KnownPosition.Yaw = (Player.KnownPosition.Yaw + look.X) % 360f; // Player.KnownPosition.Yaw %= 360f; _previousMousePosition = e; } } } LastVelocity = Player.Velocity; }
private void UpdateEntity(Entity e, float deltaTime) { var originalPosition = e.KnownPosition; Hit.Clear(); var position = e.KnownPosition; var originalEntityBoundingBox = e.GetBoundingBox(position); var before = e.Velocity; var velocity = e.Velocity; var preview = position.PreviewMove(velocity * deltaTime); var boundingBox = e.GetBoundingBox(preview); Bound bound = new Bound(World, boundingBox, preview); if (bound.GetIntersecting(boundingBox, out var blocks)) { if (AdjustForY( originalEntityBoundingBox, e.GetBoundingBox(new Vector3(position.X, preview.Y, position.Z)), blocks, ref velocity, out var yCollisionPoint)) { e.CollidedWithWorld(before.Y < 0 ? Vector3.Down : Vector3.Up, yCollisionPoint); //onGround = e.KnownPosition.OnGround; } var solidBlocks = blocks.Where(x => x.block.Solid).ToArray(); if (solidBlocks.Length > 0) { if (!e.IsFlying && velocity.Y >= 0f) { var matchingBlocks = solidBlocks .Where(x => x.block.Solid && x.box.Max.Y > preview.Y && x.block.CanCollide()).ToArray(); if (matchingBlocks.Length > 0) { var closest = matchingBlocks.Min(x => x.box.Max.Y); if (MathF.Abs(preview.Y - closest) <= 0.55f) { e.KnownPosition.Y = closest + 0.005f; position.Y = closest + 0.005f; } } } if (AdjustForX( originalEntityBoundingBox, e.GetBoundingBox(new Vector3(preview.X, position.Y, position.Z)), blocks, ref velocity, out var xCollisionPoint)) { e.CollidedWithWorld(before.X < 0 ? Vector3.Left : Vector3.Right, xCollisionPoint); } if (AdjustForZ( originalEntityBoundingBox, e.GetBoundingBox(new Vector3(position.X, position.Y, preview.Z)), blocks, ref velocity, out var zCollisionPoint)) { e.CollidedWithWorld(before.Z < 0 ? Vector3.Backward : Vector3.Forward, zCollisionPoint); } Hit.AddRange(blocks.Select(x => x.box)); } } if (Hit.Count > 0) { LastKnownHit = Hit.ToArray(); } e.Velocity = velocity; e.KnownPosition.Move(e.Velocity * deltaTime); //e.KnownPosition.OnGround = onGround; e.DistanceMoved += MathF.Abs(Vector3.Distance(originalPosition, e.KnownPosition.ToVector3())); }
static float INumber <float> .Abs(float value) => MathF.Abs(value);
private void GameTick(object state) { var isRealTick = _isRealTick; _isRealTick = !isRealTick; if (World == null) { return; } if (_initiated) { if (_isRealTick) { _tickTime++; } var p = World.Player; if (p != null && p is Player player && Client.HasSpawned) { // player.IsSpawned = Spawned; if (player.IsFlying != _flying && _isRealTick) { _flying = player.IsFlying; McpeAdventureSettings settings = McpeAdventureSettings.CreateObject(); settings.flags = GetAdventureFlags(); Client.SendPacket(settings); //SendPlayerAbilities(player); } var pos = (PlayerLocation)player.KnownPosition.Clone(); if (pos.DistanceTo(_lastSentLocation) > 0.0f) { Client.SendMcpeMovePlayer(new MiNET.Utils.PlayerLocation(pos.X, pos.Y + Player.EyeLevel, pos.Z, pos.HeadYaw, pos.Yaw, -pos.Pitch), player.KnownPosition.OnGround); _lastSentLocation = pos; } if ((pos.DistanceTo(_lastLocation) > 16f || MathF.Abs(pos.HeadYaw - _lastLocation.HeadYaw) >= 10f) && (_tickTime - _lastPrioritization >= 10 && _isRealTick)) { World.ChunkManager.FlagPrioritization(); _lastLocation = pos; UnloadChunks(new ChunkCoordinates(pos), Client.ChunkRadius + 3); _lastPrioritization = _tickTime; } } if (_isRealTick && _tickTime % 20 == 0 && CustomConnectedPong.CanPing) { Client.SendPing(); } if (_isRealTick) { World.Player.OnTick(); World.EntityManager.Tick(); } } }
private void UpdateEntity(Entity e, float deltaTime) { var originalPosition = e.KnownPosition; Hit.Clear(); var position = e.KnownPosition; //var originalPosition = position; var originalEntityBoundingBox = e.GetBoundingBox(position); var before = e.Velocity; var velocity = e.Velocity; var preview = position.PreviewMove(velocity * deltaTime); var boundingBox = e.GetBoundingBox(preview); var bounding = new BoundingBox( new Vector3( MathF.Min(originalEntityBoundingBox.Min.X, boundingBox.Min.X), MathF.Min(originalEntityBoundingBox.Min.Y, boundingBox.Min.Y), MathF.Min(originalEntityBoundingBox.Min.Z, boundingBox.Min.Z)), new Vector3( MathF.Max(originalEntityBoundingBox.Max.X, boundingBox.Max.X), MathF.Max(originalEntityBoundingBox.Max.Y, boundingBox.Max.Y), MathF.Max(originalEntityBoundingBox.Max.Z, boundingBox.Max.Z))); var modifiedPreview = preview; Bound bound = new Bound(World, bounding, preview); if (bound.GetIntersecting(bounding, false, out var blocks)) { var solidBlocks = blocks.Where(x => x.block.Solid).ToArray(); if (solidBlocks.Length > 0) { if (AdjustForY(e, originalEntityBoundingBox, e.GetBoundingBox(new Vector3(position.X, preview.Y, position.Z)), solidBlocks, ref velocity, out var yCollisionPoint, ref position)) { e.CollidedWithWorld(before.Y < 0 ? Vector3.Down : Vector3.Up, yCollisionPoint); } if (AdjustForX(e, originalEntityBoundingBox, e.GetBoundingBox(new Vector3(preview.X, position.Y, position.Z)), solidBlocks, ref velocity, out var xCollisionPoint, ref position)) { e.CollidedWithWorld(before.X < 0 ? Vector3.Left : Vector3.Right, xCollisionPoint); } if (AdjustForZ(e, originalEntityBoundingBox, e.GetBoundingBox(new Vector3(position.X, position.Y, preview.Z)), solidBlocks, ref velocity, out var zCollisionPoint, ref position)) { e.CollidedWithWorld(before.Z < 0 ? Vector3.Backward : Vector3.Forward, zCollisionPoint); } Hit.AddRange(solidBlocks.Select(x => x.box)); } } if (Hit.Count > 0) { LastKnownHit = Hit.ToArray(); } if (before.Y >= 0f && position.Y > originalPosition.Y) { e.KnownPosition.Y = position.Y; } e.Velocity = velocity; e.KnownPosition.Move(e.Velocity * deltaTime); //e.KnownPosition.OnGround = onGround; e.DistanceMoved += MathF.Abs(Vector3.Distance(originalPosition, e.KnownPosition.ToVector3())); }
private void DoHealthAndExhaustion() { var elapsed = (DateTime.UtcNow - _lastMovementUpdate).TotalSeconds; var pos = Entity.KnownPosition; var distance = MathF.Abs( Vector3.DistanceSquared( new Vector3(pos.X, 0, pos.Z), new Vector3(_lastExhaustionPosition.X, 0, _lastExhaustionPosition.Z))); if (Entity.IsSprinting) { Exhaust(distance * 0.1f); } else if (Entity.IsInWater) { Exhaust(distance * 0.01f); } _ticker += 1; if (_ticker >= 80) { _ticker = 0; } if (_ticker == 0) { if (Hunger >= 18 && Health < MaxHealth) { Heal(1); Exhaust(3); } else if (Hunger <= 0 && _health > 1) { TakeHit(1); } } while (_exhaustion >= 4) { _exhaustion -= 4; if (Saturation > 0) { Saturation -= 1; } else { Hunger -= 1; // Saturation = 0; if (Hunger < 0) { Hunger = 0; } } } _lastExhaustionPosition = pos; _lastMovementUpdate = DateTime.UtcNow; }