Пример #1
0
        public void Control(float thrust, float torque, bool firing)
        {
            Firing = firing;

            thrust = Fmath.Clamp(thrust, 0.0f, 1.0f);


            if (torque == 0)
            {
                // This is negative feedback, so im unsure why torque and angular velocity have different signs.
                torque = AngularVelocity * 10f;
            }
            torque = Fmath.Clamp(torque, -1.0f, 1.0f);

            /*
             * float epsilon = 0.001f;
             * if (torque < epsilon && torque > -epsilon)
             * {
             *  float dt = (CollisionBody.AngularVelocity > epsilon) ? 1f : ((CollisionBody.AngularVelocity < -epsilon) ? -1f : 0f);
             *  torque = dt;
             * }
             */


            CollisionBody.AddForce(Fmath.CosSin(Angle, Thrust * thrust));
            CollisionBody.AddTorque(Torque * torque);
        }
Пример #2
0
        public override void OnDestroy()
        {
            float subsize = Size / 2;

            if (subsize > MinimumSize)
            {
                Vector2 ecc = Fmath.Rotate(new Vector2(Size / 4, Size / 4), Angle);

                // Hack to pull the pending forces out of the collision system and applying them immediately.
                Velocity += (CollisionBody.Force / 60f) * CollisionBody.InvMass;

                for (int i = 0; i < 4; i++)
                {
                    ecc = Fmath.RotatePos(ecc);

                    Asteroid subasteroid = new Asteroid(
                        Position + ecc,
                        Velocity + ecc * EjectionVelocity / Size,
                        subsize,
                        Angle,
                        AngularVelocity + EjectionRotation - Fmath.RandF(2 * EjectionRotation)
                        );

                    Context.AddEntity(subasteroid);
                }
            }
            else
            {
                Context.AddEvent(new Explosion(Position, Size * 1.5f, 1f, Color.White));
            }

            base.OnDestroy();
        }
Пример #3
0
        public override void Predict(long timestamp)
        {
            long  delta  = timestamp - baseTimestamp;
            float deltas = (delta / 1000.0f);

            Velocity = baseVelocity + Fmath.CosSin(Angle, Acceleration * deltas);
            Position = basePosition + (baseVelocity * deltas) + Fmath.CosSin(Angle, Acceleration * (deltas * deltas) / 2);
            Angle    = baseAngle + (AngularVelocity * deltas);
        }
Пример #4
0
        public override void Draw(SpriteBatch batch)
        {
            Color color = (Creator == null) ? Color.White : Color.Lerp(Creator.Color, Color.White, 0.5f);
            float gSize = Fmath.Sqrt(Size) + 0.1f;

            Drawing.DrawTriangle(batch, Position, new Vector2(15, 5) * gSize, Angle, color);
            float tSize = gSize * Velocity.Length() / 6f;

            Drawing.DrawBullet(batch, Position - Fmath.CosSin(Angle, (12f + tSize) / 2f * gSize), new Vector2(tSize, 6) * gSize, Angle, color);
        }
Пример #5
0
 private Ship NewPlayer(Color color)
 {
     return(new Ship(
                Fmath.RandomVector(Boundary / 2) + (Boundary / 4),
                Fmath.CosSin(Fmath.RandAngle(), Fmath.RandF(50)),
                color,
                Fmath.RandAngle(),
                Fmath.RandF(1f)
                ));
 }
Пример #6
0
 private Asteroid NewAsteroid(float scale)
 {
     return(new Asteroid(
                Fmath.RandomVector(Boundary),
                Fmath.CosSin(Fmath.RandAngle(), Fmath.RandF(50)),
                scale / 2 + Fmath.RandF(scale / 2),
                Fmath.RandAngle(),
                Fmath.RandF(1f)
                ));
 }
Пример #7
0
        public Projectile(Ship creator)
        {
            Position        = creator.Position;
            Angle           = creator.Angle;
            Velocity        = creator.Velocity + Fmath.CosSin(Angle, Speed);
            AngularVelocity = 0f;

            Position += Fmath.CosSin(Angle) * creator.Size.X / 2;

            Creator = creator;

            Creator.Push(-Fmath.CosSin(Angle, Recoil));
        }
Пример #8
0
        public Missile(Ship creator, float size)
        {
            Size            = size;
            Position        = creator.Position;
            Angle           = creator.Angle;
            baseVelocity    = creator.baseVelocity + Fmath.CosSin(Angle, Speed);
            AngularVelocity = 0f;

            Position += Fmath.CosSin(Angle) * creator.Size.X / 2;
            Velocity  = baseVelocity;

            Creator = creator;

            Creator.Push(-Fmath.CosSin(Angle, Recoil));
        }
Пример #9
0
        public override void OnCollide(Physical phys)
        {
            IsDestroyed = true;

            float sqrtSize = Fmath.Sqrt(Size);
            float aoe      = ExplosiveArea * sqrtSize;

            foreach (Physical p in Context.GetEntitiesWithin(Position, aoe))
            {
                Vector2 deltaPos = p.Position - Position;
                float   alpha    = 1f - (deltaPos.Length() / (aoe * 1.2f));
                if (alpha > 0)
                {
                    deltaPos.Normalize();
                    p.Push(deltaPos * alpha * ExplosiveForce * Size);
                    p.Hitpoints -= alpha * ExplosiveDamage * Size;
                }
            }

            Context.AddEvent(new Explosion(Position, 50f * sqrtSize, 1.5f * sqrtSize, Creator.Color));
        }
Пример #10
0
        public override void Update(float delta)
        {
            Position = CollisionBody.Position;
            Angle    = CollisionBody.Angle;

            if (Angle > MathHelper.TwoPi || Angle < MathHelper.TwoPi)
            {
                Angle = Fmath.Mod(Angle, MathHelper.TwoPi);
            }

            if ((CollisionBody.LinearVelocity - Velocity).LengthSquared() > VelocityTolerance * VelocityTolerance ||
                (Fmath.Abs(CollisionBody.AngularVelocity - AngularVelocity) > VelocityTolerance))
            {
                RequestMotionUpdate();
            }

            if (Hitpoints < 0)
            {
                IsDestroyed = true;
            }
        }
Пример #11
0
 public override void Update(float delta)
 {
     Velocity += Fmath.CosSin(Angle, Acceleration) * delta;
     base.Update(delta);
 }
Пример #12
0
 public Point2 PositionToGrid(Vector2 position)
 {
     return(new Point2(Fmath.Floor(position.X / TileSize),
                       Fmath.Floor(position.Y / TileSize)));
 }