Esempio n. 1
0
        private static void CreateBullets(Body ship)
        {
            Func<Vector2, Body> createBullet = pos =>
            {
                var body = new Body(world);
                body.BodyType = BodyType.Dynamic;
                body.IsBullet = true;

                var shape = new FarseerCircleShape(0.15f / 4, 1);
                body.CreateFixture(shape);

                body.Position = ship.Position + ship.GetWorldVector(pos);
                body.Rotation = ship.Rotation;
                body.ApplyForce(body.GetWorldVector(new Vector2(0.0f, -15f)));

                body.OnCollision += (a, b, contact) =>
                {
                    world.RemoveBody(body);
                    return false;
                };

                return body;
            };

            createBullet(new Vector2(-0.575f, -0.20f));
            createBullet(new Vector2(0.575f, -0.20f));
        }
Esempio n. 2
0
        public static void CenterGravity(Body Body)
        {
            Vector2f Pos = Body.Position.ToVec();

            Quantity Radius = SI.px.Quant(Pos.Length()).Convert(SI.m) + SI.m.Quant(1000);
            Quantity F = Constants.G * (SI.kg.Quant(Body.Mass) * SI.kg.Quant(5.972e16)) / (Radius ^ 2);

            Vector2f Force = -Pos.Normalize() * (float)F.Amount;
            Body.ApplyForce(Force.ToVec());
        }
Esempio n. 3
0
        public void Load()
        {
            var palette = Palette.FromXmlFile(DataPack.Colors.PaleBlue);
            Add(new BgColorValue(palette.ColorSets[0].Colors[1]));
            _actorColor = palette.ColorSets[0].Colors[3];

            // scene
            var scene = Add(RubeScene.FromFile(DataPack.Physic.Potato));
            Add(new PropertyValue<Vector2>(new Vector2(0, 20),
                () => Physic.World.Gravity,
                v => Physic.World.Gravity = v));

            // ui
            _body = scene.GetBody("actor");
            Add(new BodyStateRecorder(_body, "actor"));
            //_body.Position = new Vector2(0, 10);
            var actor = Add(new GameUI { Name = "actor"} );

            var tracker = actor.Add(new BodyMouseTracker(_body, .4f));
            Add(new BodyStateRecorder(tracker, "tracker"));

            actor.AddRange(
                new KeyBinding(Keys.Space, () => _body.ApplyForce(new Vector2(0, 20))),
                new FlyController(_body, 10, 50),
                new MouseZoomController(),
                new TrackingBodyCameraController(tracker, UpdateTime.Sim),
                new CameraStateRecorder()
            );
            _body.UserData = this;

            // rendering
            Add(new DrawComponent(DrawBody));
            var staticBodies = scene.Bodies.Where(b => b.BodyType == BodyType.Static);
            var contours = staticBodies.GetContours();
            foreach (var contour in contours)
            {
                var strip = ShapeFactory.PolygonStrip(contour, .3f, .01f);
                _staticBodiesContours.AddRange(strip);
            }

            // debug
            Add(new UpdateComponent(Update, UpdateTime.Sim));
        }
Esempio n. 4
0
        //TODO: Comment better
        /// <summary>
        /// Moves the body on the path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="body">The body.</param>
        /// <param name="time">The time.</param>
        /// <param name="strength">The strength.</param>
        /// <param name="timeStep">The time step.</param>
        public static void MoveBodyOnPath(Path path, Body body, float time, float strength, float timeStep, bool if_type2 = true)
        {
            Vector2 destination = path.GetPosition(time);
            Vector2 positionDelta = body.Position - destination;
            if ( positionDelta.Length() != 0 )
                positionDelta.Normalize();  // Added for game
            Vector2 velocity = (positionDelta / timeStep) * strength;
            Vector2 vel = Math.Min(8,body.LinearVelocity.Length()+3) * -velocity * body.Mass;

            if (if_type2 && vel.Length() > 1)
            {
                float velocity_magnitude_X = Math.Abs(body.LinearVelocity.X);
                float velocity_magnitude_Y = Math.Abs(body.LinearVelocity.Y);

                float force_magnitude_X = Math.Abs(vel.X);
                float force_magnitude_Y = Math.Abs(vel.Y);

                bool horiz = force_magnitude_X > 2;
                bool vert = force_magnitude_Y > 2;

                bool high_vert_low_horiz = (velocity_magnitude_X < velocity_magnitude_Y - 10);
                bool high_horiz_low_vert = velocity_magnitude_X > velocity_magnitude_Y + 10;

                if ((horiz) && high_vert_low_horiz)
                {
                    vel.X *= 2f;
                    vel.Y /= 2f;
                    //body.LinearVelocity = new Vector2(body.LinearVelocity.X,body.LinearVelocity.Y/1.5f);
                }
                if (vert && high_horiz_low_vert)
                {
                    vel.Y *= 2f;
                    vel.X /= 2f;
                   // body.LinearVelocity = new Vector2(body.LinearVelocity.X/1.5f, body.LinearVelocity.Y);
                }
                if (Math.Sign(vel.Y) != Math.Sign(body.LinearVelocity.Y))
                {
                    vel.Y *= 3;
                }
                if (Math.Sign(vel.X) != Math.Sign(body.LinearVelocity.X))
                {
                    vel.X *= 3;
                }
                body.ApplyForce(vel);
            }
            else if(path.GetLength()>0.5f)
            {
                body.LinearVelocity = -velocity;
            }
        }
Esempio n. 5
0
        private void CreateBullets()
        {
            Func<Vector2, Body> createBullet = pos =>
            {
                var body = new Body(Game.World);
                body.BodyType = BodyType.Dynamic;
                body.IsBullet = true;
                body.UserData = new RadarData(RadarType.Bullet, new NetBullet(body));

                var shape = new CircleShape(0.15f / 4, 1);
                body.CreateFixture(shape);

                body.Position = Body.Position + Body.GetWorldVector(pos);
                body.Rotation = Body.Rotation;
                body.ApplyForce(body.GetWorldVector(new Vector2(0.0f, -10f)));

                body.OnCollision += (a, b, contact) =>
                {
                    Game.World.RemoveBody(body);
                    return false;
                };

                return body;
            };

            createBullet(new Vector2(-0.575f, -0.20f));
            createBullet(new Vector2(0.575f, -0.20f));
        }