Пример #1
0
        protected override void Open()
        {
            dispose += DemoHelper.BasicDemoSetup(DemoInfo);


            Scene.Engine.AddLogic(new GravityPointField(new Vector2D(500, 500), 1000, new Lifespan()));


            Light light = new Light();

            light.Position.X = 000;
            light.Position.Y = 000;
            light.Position.Z = 0100;

            Body        lightBody    = new Body(new PhysicsState(), ShapeFactory.CreateCircle(15, 20), 40, new Coefficients(0, 1), new Lifespan());
            BodyGraphic lightGraphic = new BodyGraphic(lightBody);

            lightGraphic.DrawProperties.Add(new Color3Property(1, 1, 1));

            Scene.AddGraphic(lightGraphic);

            lightBody.PositionChanged += delegate(object sender, EventArgs e)
            {
                light.Position = lightBody.State.Position.Linear.ToVector3D(100);
            };

            IShape shape = ShapeFactory.CreateSprite(
                Cache <SurfacePolygons> .GetItem("Monkey.png"),
                Cache <Surface> .GetItem("MonkeyNormal.bmp"),
                false, true,
                3, 8, 16,
                light);

            DemoHelper.AddGrid(DemoInfo, shape, 40, new BoundingRectangle(40, 40, 900, 900), 100, 100);
        }
Пример #2
0
        public static BodyGraphic CreateGraphic(Body body)
        {
            BodyGraphic result = new BodyGraphic(body);

            result.DrawProperties.Add(new StateColorProperty(body));
            return(result);
        }
Пример #3
0
        public static List <Body> AddParticles(DemoOpenInfo info, Vector2D position, Vector2D velocity, int count)
        {
            Graphic[]   graphics = new Graphic[count];
            List <Body> result   = new List <Body>(count);
            Scalar      angle    = MathHelper.TwoPi / count;

            for (int index = 0; index < count; ++index)
            {
                Body particle = new Body(
                    new PhysicsState(new ALVector2D(0, position)),
                    ParticleShape.Default,
                    1f,
                    new Coefficients(1, .5f),// coefficients.Duplicate(),
                    new Lifespan(.9f));
                result.Add(particle);
                Vector2D direction = Vector2D.FromLengthAndAngle(1, index * angle + ((Scalar)Rand.NextDouble() - .5f) * angle);
                particle.State.Position.Linear += direction;
                particle.State.Velocity.Linear  = direction * Rand.Next(200, 1001) + velocity;
                //particle.Collided += new EventHandler<CollisionEventArgs>(particle_Collided);
                BodyGraphic graphic = CreateGraphic(particle);
                graphics[index] = graphic;
            }
            info.Scene.AddGraphicRange(graphics);
            return(result);
        }
        // Methods
        public virtual void drawBody(Form form)
        {
            BodyGraphic graphic = new BodyGraphic(BodyColor);

            graphic.Location = new Point(10, Y);
            graphic.Size     = new Size(100, 100);
            graphic.Type     = BodyType;
            form.Controls.Add(graphic);
        }
Пример #5
0
        protected override void Open()
        {
            dispose += DemoHelper.BasicDemoSetup(DemoInfo);

            IShape fighterShape = ShapeFactory.CreateSprite(Cache <SurfacePolygons> .GetItem("fighter.png"), 4, 50, 5);
            Body   fighter      = new Body(new PhysicsState(new ALVector2D(0, 300, 300)), fighterShape, 5000, new Coefficients(0, 1), new Lifespan());

            fighter.State.Velocity.Angular = 9;
            fighter.Mass.MomentOfInertia   = Scalar.PositiveInfinity;

            BodyGraphic fighterGraphic = new BodyGraphic(fighter);

            Scene.AddGraphic(fighterGraphic);

            BodyGraphic fighterGraphic2 = (BodyGraphic)fighterGraphic.Duplicate();

            fighterGraphic2.Body.State.Position.Linear.Y = 500;
            fighterGraphic2.Body.State.Velocity.Angular  = -8.5f;
            Scene.AddGraphic(fighterGraphic2);


            Body      template2 = new Body(new PhysicsState(new ALVector2D(0, 200, 200)), ParticleShape.Default, 4, new Coefficients(0, 1), new Lifespan());
            Matrix2x3 m         = Matrix2x3.FromRotationZ(2);
            int       count     = 0;

            for (int x = -0; x < 1000; x += 25)
            {
                for (int y = -0; y < 1000; y += 25)
                {
                    count++;
                    Body body = template2.Duplicate();
                    body.State.Position.Linear = new AdvanceMath.Vector2D(x, y);
                    body.ApplyPosition();
                    body.State.Velocity.Linear.X = (250 - x) / 10f;
                    body.State.Velocity.Linear.Y = (250 - y) / 10f;
                    body.State.Velocity.Linear   = m * body.State.Velocity.Linear;
                    body.LinearDamping           = .9999f;
                    BodyGraphic g1 = new BodyGraphic(body);
                    Scene.AddGraphic(g1);
                }
            }
            Scene.Engine.AddLogic(new GravityPointField(new Vector2D(400, 400), 500, new Lifespan()));
        }
Пример #6
0
        public static DisposeCallback CreateTank(DemoOpenInfo info, Vector2D position, List <Body> result)
        {
            Lifespan avatarLifespan = new Lifespan();

            IShape shape = ShapeFactory.CreateSprite(Cache <SurfacePolygons> .GetItem("tank.png"), 4, 18, 2);

            ObjectIgnorer ignorer  = new ObjectIgnorer();
            Body          tankBody = new Body(new PhysicsState(new ALVector2D(0, 0, 0)),
                                              shape,
                                              300,//new MassInfo(40, Scalar.PositiveInfinity),
                                              new Coefficients(0, 1),
                                              avatarLifespan);

            result.Add(tankBody);
            tankBody.State.Position.Linear += position;
            tankBody.ApplyPosition();

            tankBody.CollisionIgnorer = ignorer;
            BodyGraphic graphic = CreateGraphic(tankBody);

            graphic.ZOrder = 2;
            info.Scene.AddGraphic(graphic);

            Scalar            wheelSize     = 18;
            Scalar            wheelSpacing  = -9;
            Scalar            lenghtPercent = .84f;
            Matrix2x3         ident         = Matrix2x3.Identity;
            BoundingRectangle rect;

            shape.CalcBoundingRectangle(ref ident, out rect);
            Scalar          y         = (rect.Max.Y + 4);
            Body            lastWheel = null;
            BoundingPolygon polygon   = new BoundingPolygon(shape.Vertexes);

            Ray      ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis);
            Scalar   y3   = y - polygon.Intersects(ray2);
            Vector2D avatarBarrelOffset = new Vector2D(rect.Max.X + 10, y3);

            CircleShape wheelShape = ShapeFactory.CreateColoredCircle(wheelSize, 30);
            Scalar      force      = 0;

            for (Scalar x = rect.Min.X + wheelSize; x < (rect.Max.X - wheelSize) * lenghtPercent; x += (wheelSize * 2 + wheelSpacing))
            {
                Ray    ray = new Ray(new Vector2D(x, y), -Vector2D.YAxis);
                Scalar y2  = y - polygon.Intersects(ray);



                Vector2D offset = new Vector2D(x, y2);

                Body wheel = new Body(
                    new PhysicsState(new ALVector2D(0, offset + position)),
                    wheelShape,
                    10,
                    new Coefficients(0, 3),//  coefficients.Duplicate(),
                    avatarLifespan);
                result.Add(wheel);

                wheel.CollisionIgnorer = ignorer;
                wheel.AngularDamping   = .9f;
                wheel.Updated         += delegate(object sender, UpdatedEventArgs e)
                {
                    wheel.State.ForceAccumulator.Angular += force;
                };
                info.Scene.AddGraphic(CreateGraphic(wheel));

                HingeJoint joint = new HingeJoint(tankBody, wheel, offset + position, avatarLifespan);
                joint.Softness = .1f;
                info.Scene.Engine.AddJoint(joint);

                if (lastWheel != null)
                {
                    AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan);
                    info.Scene.Engine.AddJoint(joint2);
                }
                lastWheel = wheel;
            }


            CircleShape weaponShape = ShapeFactory.CreateColoredCircle(5, 8);

            //now begins the abuse of anominous delegates (BIG TIME)

            EventHandler <KeyboardEventArgs> keyDownHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                case Key.LeftArrow:
                    force = -1500000;
                    break;

                case Key.RightArrow:
                    force = 1500000;
                    break;

                case Key.Space:

                    Scalar velocity = 2000;

                    Matrix2x3 toWorld       = tankBody.Matrices.ToWorld;
                    Matrix2x2 toWorldNormal = tankBody.Matrices.ToWorldNormal;

                    //  Matrix2D mat = avatarBodies[0].Matrices.ToWorld;
                    Vector2D     direction = toWorldNormal * Vector2D.XAxis;
                    PhysicsState state     = new PhysicsState();
                    state.Position.Linear = toWorld * (avatarBarrelOffset);
                    state.Velocity.Linear = velocity * direction + tankBody.State.Velocity.Linear;

                    Body weapon = new Body(state,
                                           weaponShape,
                                           5,
                                           new Coefficients(1, 1),
                                           new Lifespan(10));
                    //weapon.CollisionIgnorer = tankBody.CollisionIgnorer;

                    weapon.Collided += delegate(object sender2, CollisionEventArgs e2)
                    {
                        if (!weapon.Lifetime.IsExpired)
                        {
                            weapon.Lifetime.IsExpired = true;
                            AddParticles(info, weapon.State.Position.Linear, weapon.State.Velocity.Linear * .5f, 50);
                        }
                    };

                    //  weapon.Collided += weapon_Collided;
                    tankBody.State.Velocity.Linear -= (velocity * weapon.Mass.Mass * tankBody.Mass.MassInv) * direction;
                    info.Scene.AddGraphic(CreateGraphic(weapon));
                    break;
                }
            };
            EventHandler <KeyboardEventArgs> keyUpHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                case Key.LeftArrow:
                    force = 0;
                    break;

                case Key.RightArrow:
                    force = 0;
                    break;
                }
            };

            Events.KeyboardDown += keyDownHandler;
            Events.KeyboardUp   += keyUpHandler;

            return(delegate()
            {
                Events.KeyboardDown -= keyDownHandler;
                Events.KeyboardUp -= keyUpHandler;
            });
        }