Example #1
0
 public CommandParameters(Id <FreeObject> id, Player player, Position2 position, Velocity2 velocity)
 {
     this.id       = id.Simple;
     this.playerId = player.ID.Simple;
     this.position = position;
     this.velocity = velocity;
 }
Example #2
0
        public override void Update(TimeSpan t)
        {
            this.game.CollisionHandler.HandleCollision(this);

            if (this.Deleted)
            {
                return;
            }

            var acceleration = Vector2.Zero;

            foreach (var body in this.game.Bodies)
            {
                var shape = body.Shape;

                var difference = shape.Center - this.position;

                var distanceSquared = difference.LengthSquared;

                var a = Constants.G * body.Mass / distanceSquared.NumericValue;

                var dirNormal = difference.Direction.Vector;

                acceleration += dirNormal * a;
            }

            this.velocity += new Velocity2(acceleration * (float)t.NumericValue);

            this.position += this.velocity * t;
        }
Example #3
0
        public bool Remove(ICollider c, out Velocity2 v)
        {
            var i       = c.ColliderHashCode % Table.Length;
            var myArray = Table[i].AsSpan();

            for (var j = 0; j < myArray.Length; j++)
            {
                if (ReferenceEquals(c, myArray[j]?.Collider))
                {
                    v          = myArray[j].Velocity;
                    myArray[j] = null;
                    for (var k = j; k < myArray.Length - 1; k++)
                    {
                        myArray[k]     = myArray[k + 1];
                        myArray[k + 1] = null;
                        if (myArray[k] == null)
                        {
                            break;
                        }
                    }
                    return(true);
                }
            }
            v = null;
            return(false);
        }
Example #4
0
    public Force2(Velocity2 tracker, float accelleration, Angle angle, TimeSpan?duration = null)
    {
        this.Accelleration = accelleration;
        this.Angle         = angle;
        this.tracker       = tracker ?? throw new ArgumentNullException();
        this.Duration      = duration.HasValue ? duration.Value : TimeSpan.Zero;

        if (Duration < TimeSpan.Zero)
        {
            this.IsPermanentForce = true;
        }
        else
        {
            this.EndTime = DateTime.Now + Duration;
        }

        if (Duration == TimeSpan.Zero)
        {
            var end      = tracker.Collider.Bounds.OffsetByAngleAndDistance(tracker.Angle, tracker.Speed).OffsetByAngleAndDistance(angle, accelleration);
            var newAngle = tracker.Collider.Bounds.CalculateAngleTo(end);
            var newSpeed = tracker.Collider.Bounds.CalculateDistanceTo(end);
            tracker.Angle = newAngle;
            tracker.Speed = newSpeed;
            this.Dispose();
        }

        ConsoleApp.Current.Invoke(async() =>
        {
            while (this.IsExpired == false)
            {
                Evaluate();
                await Task.Yield();
            }
        });
    }
Example #5
0
 public Data(Position2 p, Velocity2 v, Unit z, Speed vz, bool collideWithProjectileColliders)
 {
     this.position = p;
     this.velocity = v;
     this.z = z;
     this.vz = vz;
     this.collideWithProjectileColliders = collideWithProjectileColliders;
 }
Example #6
0
        public FreeObject(GameState game, Id <FreeObject> id, Position2 position, Velocity2 velocity)
            : base(game, id)
        {
            this.position = position;
            this.velocity = velocity;

            game.ContinuousSynchronizer.Sync(this);
        }
Example #7
0
 public CollidingParticle(GameState game,
                          Position2 position, Velocity2 velocity,
                          Unit z, Speed vz,
                          bool collideWithProjectileColliders)
     : base(game)
 {
     this.data = new Data(position, velocity, z, vz, collideWithProjectileColliders);
 }
Example #8
0
 public Data(Position2 p, Velocity2 v, Unit z, Speed vz, bool collideWithProjectileColliders)
 {
     this.position = p;
     this.velocity = v;
     this.z        = z;
     this.vz       = vz;
     this.collideWithProjectileColliders = collideWithProjectileColliders;
 }
Example #9
0
 public CollidingParticle(GameState game,
     Position2 position, Velocity2 velocity,
     Unit z, Speed vz,
     bool collideWithProjectileColliders)
     : base(game)
 {
     this.data = new Data(position, velocity, z, vz, collideWithProjectileColliders);
 }
Example #10
0
        public override void Update(TimeSpan elapsedTime)
        {
            var difference = this.goal - this.position;

            var acceleration = new Acceleration2(difference.NumericValue);

            this.velocity += acceleration * 3 * elapsedTime;

            this.position += this.velocity * elapsedTime;

            this.velocity *= Mathf.Pow(0.01f, (float)elapsedTime.NumericValue);

            if (this.deletionTime != Instant.Zero && this.game.Time >= this.deletionTime)
            {
                this.Delete();
            }
        }
Example #11
0
        public Friction2(Velocity2 tracker, float evalFrequency = DefaultFrictionEvalFrequency)
        {
            this.tracker = tracker;
            tracker.Collider.OnDisposed(this.Dispose);

            ConsoleApp.Current.Invoke(async() =>
            {
                while (this.IsExpired == false)
                {
                    tracker.Speed *= Decay;
                    if (tracker.Speed < .1f)
                    {
                        tracker.Speed = 0;
                    }
                    await Task.Delay((int)evalFrequency);
                }
            });
        }
Example #12
0
        private void drawProjectilePathPreview(PrimitiveGeometry geo)
        {
            var s       = this.body.Shape;
            var dVector = this.aimDirection.Vector;

            var v = new Velocity2(dVector * 0.9f) + this.body.Velocity;
            var p = s.Center + new Difference2(dVector * s.Radius.NumericValue * 1.5f);

            geo.LineWidth = 0.05f;
            geo.Color     = Color.Gray;

            for (int i = 0; i < 50; i++)
            {
                var acceleration = Vector2.Zero;

                foreach (var body in this.game.Bodies)
                {
                    var shape = body.Shape;

                    var difference = shape.Center - p;

                    var distanceSquared = difference.LengthSquared;

                    var a = Constants.G * body.Mass / distanceSquared.NumericValue;

                    var dirNormal = difference.Direction.Vector;

                    acceleration += dirNormal * a;
                }

                var speedFactor = Math.Min(0.5f / acceleration.Length.Sqrted(), 0.5f / v.Speed.NumericValue.Squared());

                var t = TimeSpan.One * speedFactor;


                v += new Velocity2(acceleration * (float)t.NumericValue);

                var p2 = p + v * t;

                geo.DrawLine(p.Vector, p2.Vector);

                p = p2;
            }
        }
Example #13
0
        internal (int RowIndex, int ColIndex) Add(ICollider c, Velocity2 v)
        {
            var i       = c.ColliderHashCode % Table.Length;
            var myArray = Table[i].AsSpan();

            for (var j = 0; j < myArray.Length; j++)
            {
                if (myArray[j] == null)
                {
                    myArray[j] = new Item(c, v);
                    return(i, j);
                }
            }
            var biggerArray = new Item[myArray.Length * 2];

            Array.Copy(Table[i], biggerArray, myArray.Length);
            biggerArray[myArray.Length] = new Item(c, v);
            Table[i] = biggerArray;
            return(i, myArray.Length);
        }
Example #14
0
        public bool TryGetValue(ICollider c, out Velocity2 v)
        {
            var i       = c.ColliderHashCode % Table.Length;
            var myArray = Table[i].AsSpan();

            for (var j = 0; j < myArray.Length; j++)
            {
                var item = myArray[j];
                if (item == null)
                {
                    v = null;
                    return(false);
                }

                if (ReferenceEquals(c, item.Collider))
                {
                    v = item.Velocity;
                    return(true);
                }
            }
            v = null;
            return(false);
        }
Example #15
0
    protected override async Task Startup()
    {
        InitPause();
        var random = new Random(100);

        var camera = LayoutRoot.Add(new Camera()
        {
            BigBounds = new RectF(0, 0, 400, 400)
        }).Fill();

        camera.CameraLocation = camera.BigBounds.Center.ToRect(camera.Width, camera.Height).TopLeft;

        FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.W, null, () => DefaultColliderGroup.SpeedRatio = DefaultColliderGroup.SpeedRatio + .1f, this);
        FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.S, null, () => DefaultColliderGroup.SpeedRatio = Math.Max(0, DefaultColliderGroup.SpeedRatio - .1f), this);

        while (true)
        {
            var left = camera.Add(new ConsoleControl()
            {
                Width      = 5,
                Height     = 2,
                X          = ConsoleMath.Round(camera.BigBounds.Center.Left - 50),
                Y          = ConsoleMath.Round(camera.BigBounds.Center.Top),
                Background = new RGB((byte)random.Next(60, 120), (byte)random.Next(60, 120), (byte)random.Next(60, 120))
            });

            var right = camera.Add(new ConsoleControl()
            {
                Width      = 5,
                Height     = 2,
                X          = ConsoleMath.Round(camera.BigBounds.Center.Left + 50),
                Y          = ConsoleMath.Round(camera.BigBounds.Center.Top),
                Background = new RGB((byte)random.Next(60, 120), (byte)random.Next(60, 120), (byte)random.Next(60, 120))
            });

            await Task.WhenAll(left.FadeIn(delayProvider: DelayProvider), right.FadeIn(delayProvider: DelayProvider));

            var leftV = new Velocity2(left, DefaultColliderGroup)
            {
                Bounce = true
            };
            leftV.Speed = 90;
            leftV.Angle = Angle.Right;

            var rightV = new Velocity2(right, DefaultColliderGroup)
            {
                Bounce = true
            };
            rightV.Speed = 10;
            rightV.Angle = Angle.Left;

            FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.UpArrow, null, () =>
            {
                leftV.SpeedRatio = leftV.SpeedRatio + .1f;
            }, this);

            FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.DownArrow, null, () =>
            {
                leftV.SpeedRatio = Math.Max(0, leftV.SpeedRatio - .1f);
            }, this);

            await TaskEx.WhenAny(PauseManager.Delay(5000), leftV.ImpactOccurred.CreateNextFireTask());

            await Task.WhenAll(left.FadeOut(duration: 2000, delayProvider: DelayProvider), right.FadeOut(duration: 2000, delayProvider: DelayProvider));

            left.Dispose();
            right.Dispose();
        }
        camera.BigBounds = default;
    }
Example #16
0
 private SingleParameters(Id <FreeObject> id, Position2 position, Velocity2 velocity)
 {
     this.id       = id.Simple;
     this.position = position;
     this.velocity = velocity;
 }
Example #17
0
 public Projectile(GameState game, Id <FreeObject> id, Player owner,
                   Position2 position, Velocity2 velocity)
     : base(game, id, position, velocity)
 {
     this.owner = owner;
 }
Example #18
0
 public Item(ICollider c, Velocity2 v)
 {
     Collider = c;
     Velocity = v;
 }
Example #19
0
 public static bool HasLineOfSight(this Velocity2 from, ICollider to) => HasLineOfSight(from.Collider, to, from.GetObstaclesSlow());
 private SingleParameters(Id<FreeObject> id, Position2 position, Velocity2 velocity)
 {
     this.id = id.Simple;
     this.position = position;
     this.velocity = velocity;
 }
Example #21
0
 public Motion2(Velocity2 velocity2)
 {
     Velocity = velocity2;
 }
 public Projectile(GameState game, Id<FreeObject> id, Player owner,
     Position2 position, Velocity2 velocity)
     : base(game, id, position, velocity)
 {
     this.owner = owner;
 }
Example #23
0
    protected override float RunActual(ConsoleApp app)
    {
        int n = 0;

        app.Invoke(async() =>
        {
            await Task.Delay(1000);
            app.LayoutRoot.Background = new RGB(20, 20, 20);
            var random = new Random(100);
            var nLabel = app.LayoutRoot.Add(new Label()
            {
                ZIndex = 100, Text = $"N = {n}".ToCyan()
            }).CenterHorizontally().DockToTop(padding: 2);

            var leftWall   = app.LayoutRoot.Add(new ConsoleControl());
            var rightWall  = app.LayoutRoot.Add(new ConsoleControl());
            var topWall    = app.LayoutRoot.Add(new ConsoleControl());
            var bottomWall = app.LayoutRoot.Add(new ConsoleControl());

            var w             = 8000;
            var h             = 8000;
            leftWall.Bounds   = app.LayoutRoot.Center().Offset(-w / 2, 0).ToRect(2, h);
            rightWall.Bounds  = app.LayoutRoot.Center().Offset(w / 2, 0).ToRect(2, h);
            topWall.Bounds    = app.LayoutRoot.Center().Offset(0, -h / 2).ToRect(w, 1);
            bottomWall.Bounds = app.LayoutRoot.Center().Offset(0, h / 2).ToRect(w, 1);

            var colliderGroup = new ColliderGroup(app);
            new Velocity2(leftWall, colliderGroup);
            new Velocity2(rightWall, colliderGroup);
            new Velocity2(topWall, colliderGroup);
            new Velocity2(bottomWall, colliderGroup);

            var slowCount = 0;
            while (true)
            {
                slowCount = colliderGroup.LatestDT < 50 ? 0 : slowCount + 1;
                if (slowCount == 10)
                {
                    break;
                }

                var el        = app.LayoutRoot.Add(new ConsoleControl());
                el.Bounds     = app.LayoutRoot.Bounds.Center.ToRect(2, 1);
                el.Background = new RGB((byte)random.Next(60, 120), (byte)random.Next(60, 120), (byte)random.Next(60, 120));
                while (app.LayoutRoot.Controls.Where(e => e != el && e.Touches(el)).Any())
                {
                    el.Bounds = new RectF(random.Next(0, app.LayoutRoot.Width - 2), random.Next(0, app.LayoutRoot.Height - 1), el.Width, el.Height);
                }
                var v = new Velocity2(el, colliderGroup)
                {
                    Bounce = true
                };
                v.Speed = random.Next(0, 80);
                v.Angle = random.Next(0, 360);
                n++;
                nLabel.Text = $"N = {n}, DT = {ConsoleMath.Round(colliderGroup.LatestDT)}".ToConsoleString();
                await Task.Delay(10);
            }
            app.Stop();
        });
        app.Run();
        return(n);
    }
        private void drawProjectilePathPreview(PrimitiveGeometry geo)
        {
            var s = this.body.Shape;
            var dVector = this.aimDirection.Vector;

            var v = new Velocity2(dVector * 0.9f) + this.body.Velocity;
            var p = s.Center + new Difference2(dVector * s.Radius.NumericValue * 1.5f);

            geo.LineWidth = 0.05f;
            geo.Color = Color.Gray;

            for (int i = 0; i < 50; i++)
            {
                var acceleration = Vector2.Zero;

                foreach (var body in this.game.Bodies)
                {
                    var shape = body.Shape;

                    var difference = shape.Center - p;

                    var distanceSquared = difference.LengthSquared;

                    var a = Constants.G * body.Mass / distanceSquared.NumericValue;

                    var dirNormal = difference.Direction.Vector;

                    acceleration += dirNormal * a;
                }

                var speedFactor = Math.Min(0.5f / acceleration.Length.Sqrted(), 0.5f / v.Speed.NumericValue.Squared());

                var t = TimeSpan.One * speedFactor;

                v += new Velocity2(acceleration * (float)t.NumericValue);

                var p2 = p + v * t;

                geo.DrawLine(p.Vector, p2.Vector);

                p = p2;
            }
        }
Example #25
0
 public void UpdatePositionAndVelocity(Position2 position, Velocity2 velocity)
 {
     this.position = position;
     this.velocity = velocity;
 }