public MovablePlatform(float radius)
 {
     Radius = radius;
     PolyBody = new PolyBody(1, 4);
     color = Color.Gray;
     Load();
 }
예제 #2
0
 public Block(float scale)
 {
     Scale = scale;
     PolyBody = new PolyBody(1, 4);
     this.PolyBody.Friction = .3f;
     Load();
 }
 public FixedBlock(float radius)
 {
     Radius = radius;
     PolyBody = new PolyBody(1, 4);
     PolyBody.IsFixed = true;
     Load();
 }
예제 #4
0
 public Platform(float length)
 {
     Length = length;
     Width = DefaultThickness;
     PolyBody = new PolyBody(1, 4);
     PolyBody.IsFixed = true;
     Load();
 }
예제 #5
0
 public Bouncer(float radius)
 {
     Radius = radius;
     PolyBody = new PolyBody(1, 5);
     PolyBody.Restitution = 0.0f;
     color = Color.Gray;
     Load();
 }
예제 #6
0
 public Floor(float width)
 {
     Width = width;
     Height = .5f; // Constant
     PolyBody = new PolyBody(1, 4);
     color = Color.Gray;
     Load();
 }
예제 #7
0
 public Player(Vector2 position)
 {
     Scale = .5f;
     PolyBody = new PolyBody(1, 4);
     PolyBody.Position = position;
     PolyBody.Rotation = 0;
     PolyBody.Friction = .15f;
     PolyBody.CanSleep = false;
     PolyBody.IsRotationFixed = true;
     Load();
 }
예제 #8
0
        public Portal(string id, string partnerAddress, float x, float y)
        {
            Scale = .25f;
            PolyBody = new PolyBody(1, 10);
            PolyBody.Position = new Vector2(x, y);
            IsPhysicsable = false;

            ID = id;
            PartnerAddress = partnerAddress;

            Active = true;
            Load();
        }
        public GravityGrenade(Actor launcher, float weaponPower, Vector2 destination)
        {
            Scale = .7f;
            this.doNotSave = true;
            var player = (GameEngine.Singleton.GetPlayState())
                .ActiveMap.MainPlayer;
            Launcher = launcher;
            WeaponPower = weaponPower;
            PolyBody = new PolyBody(.1f, 3);
            PolyBody.Gravity = -9.8f * Vector2.UnitY;
            Load();
            PolyBody.Position = launcher.PolyBody.Position +
                (launcher.PolyBody.GetWidth() + (Sprite.BoundingBox().X/2)) * Math.Sign(destination.X - launcher.PolyBody.Position.X) *
                Vector2.UnitX;
            PolyBody.Velocity = computeTargetVelocity(destination);

            PolyBody.Collided += new PhysicsEventHandler(Detonate);
        }
예제 #10
0
        public static bool TestCollisionPartiPoly(Particle p, PolyBody bod, out Vector2 axis, out float disp)
        {
            axis = new Vector2();
            disp = float.MinValue;

            var norms = bod.GetNormals().ToList();

            Vector2 closeV = Vector2.Zero;
            var verts = bod.GetTransformedVertices();
            float min = float.MaxValue;
            foreach (var v in verts)
            {
                var d = (p.Position - v).LengthSquared();
                if (d < min)
                {
                    d = min;
                    closeV = v;
                }
            }
            if (closeV != Vector2.Zero && p.Position - closeV != Vector2.Zero)
            {
                var pNorm = Vector2.Normalize(p.Position - closeV);
                norms.Add(pNorm);
            }

            //
            //Col test
            //
            foreach (Vector2 ax in norms)
            {
                float d = Projection.IntervalDist(p.Project(ax), bod.Project(ax));

                if (d > 0) //no collision if even 1 separating axis found
                {
                    disp = 0;
                    return false;
                }
                else if (d > disp) //return disp w/ lowset overlap (ie: highest separation)
                {
                    disp = d;
                    axis = ax;
                }

            }

            if (Vector2.Dot(p.Position - bod.Position, axis) < 0) //make sure the normal always points from p->bod. (should this use .GetCentroid() instead of position?)
                axis = -axis;
            return true;
        }
예제 #11
0
 void Collided(PolyBody sender, object ignoreParamater)
 {
     var game = (GameEngineCore.GameStates.IPlayable)GameEngine.Singleton.ActiveState;
     if (game.ActiveMap.MainPlayer.PolyBody == sender)
     {
         this.actUpon();
         if (CollidedWith != null)
             CollidedWith(this, (PolyBody)sender);
     }
 }
예제 #12
0
        //axis must be from a->b!
        /// <summary>
        /// finds vertices of each polygon which are within the other polygon
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="axis"></param>
        /// <param name="disp"></param>
        /// <returns></returns>
        public static List<Contact> FindSubContactsPoly_CheapVerts(PolyBody a, PolyBody b, Vector2 axis, float disp)
        {
            var cons = new List<Contact>();

            foreach (var v in a.GetTransformedVertices())
            {
                if(TestPointInPoly(b, v))
                    cons.Add(new Contact(a, b, v, axis, disp));
            }
            foreach (var v in b.GetTransformedVertices())
            {
                if(TestPointInPoly(a, v))
                    cons.Add(new Contact(a, b, v, axis, disp));
            }

            return cons;
        }
예제 #13
0
        //axis must be from a->b!
        public static List<Contact> FindSubContactsPoly(PolyBody a, PolyBody b, Vector2 axis, float disp)
        {
            Vector2[] supA = GetSupports(b, axis); //get supports. instead of iterating over ALL vertices, only iterate over 4 closest, based on sep axis
            Vector2[] supB = GetSupports(a, -axis);

            var cons = new List<Contact>();

            for (int i = 0; i < supA.Length; i++)
                for (int j = 0; j < supB.Length; j++)
                {
                    Vector2 vout;
                    if (TestCollisionLine(supA[i], supA[(i + 1) % supA.Length], supB[j], supB[(j + 1) % supB.Length], out vout))
                    {
                        cons.Add(new Contact(a, b, vout, axis, disp));
                    }
                }

            return cons;
        }
        public static Texture2D GenerateTexFromPolygon(Polygon p)
        {
            var ppm = MathUtil.PixelsPerMeter;

            var np = new PolyBody(1, p.RawVertices.Length);
            //np.RawVertices = p.GetRotatedVertices();
            np.RawVertices = p.RawVertices;
            np.Position = Vector2.Zero;
            np.Rotation = 0;

            var bb = np.GetBoundingBoxRaw();

            int w = (int)(bb.Width * ppm);
            int h = (int)(bb.Height * ppm);
            Color[] col = new Color[w * h];

            Debug.Trace("Generating textures...");

            /*for (int x = 0; x < w; x++)
                for (int y = 0; y < h; y++)
                {
                    var xt = bb.xmin + x/ppm; //transform into polygon-space
                    var yt = bb.ymax - y/ppm;
                    var vt = new Vector2((float)xt, (float)yt);
                    if (CollisionEngine.TestPointInPoly(np, vt))
                    {
                        col[x + y * w] = Color.White;
                    }
                    else
                    {
                        col[x + y * w] = Color.TransparentBlack;
                    }
                }*/
            Enumerable.Range(0, w).PForEach(x =>
                {
                    for (int y = 0; y < h; y++)
                    {
                        var xt = bb.xmin + x / ppm; //transform into polygon-space
                        var yt = bb.ymax - y / ppm;
                        var vt = new Vector2((float)xt, (float)yt);
                        if (CollisionEngine.TestPointInPoly(np, vt))
                        {
                            col[x + y * w] = Color.White;
                        }
                        else
                        {
                            col[x + y * w] = Color.Black;
                        }
                    }
                }
            );

            var sb = (SpriteBatch)(Globals.Variables["SpriteBatch"]);
            var dev = (GraphicsDevice)(Globals.Variables["GraphicsDevice"]);
            Texture2D tex = new Texture2D(dev, w, h, false, SurfaceFormat.Color);
            tex.SetData<Color>(col);

            /*MemoryStream texStream = new MemoryStream();
            texStream.BeginWrite();
            col.

            Texture2D tex = Texture2D.FromFile((GraphicsDevice)(Globals.Variables["GraphicsDevice"]), texStream);*/

            Debug.Trace("Done generating!");
            return tex;
        }
        void MouseClick(GUIElement sender, MouseEventArgs e)
        {
            if (e.CurrentMouseState.LeftButton == ButtonState.Released)
            {
                pemitter.TurnOff();
            }

            if (e.isClicked(MouseButtons.Right))
            {
                int vertexCount = (new Random()).Next(4, 4);
                if (vertexCount >= 9)
                {
                    vertexCount = 25;
                }
                var polybody = new PolyBody(1f, vertexCount);
                polybody.MakeRegularFromRad(.8f);
                polybody.Mass = 1f;
                polybody.Position = new Vector2(e.CurrentMouseState.X, e.CurrentMouseState.Y).C2P();
                polybody.Restitution = 0.1f;
                polybody.Friction = .20f;

                AddAndInit(polybody);
                //vertices.Add(new Vector2(e.CurrentMouseState.X, e.CurrentMouseState.Y).C2P());
            }
        }
 // Helper Functions
 // Events
 void KeyUp(GUIElement sender, KeyEventArgs e)
 {
     if (e.InterestingKeys.Contains<Keys>(Keys.Escape))
         this.Exit();
     if (e.InterestingKeys.Contains<Keys>(Keys.OemTilde))
         GameEngine.Singleton.AddAndLoad(new ConsoleState());
     if (vertices.Count > 0 && e.InterestingKeys.Contains(Keys.Enter))
     {
         var body = new PolyBody(1, vertices.Count);
         body.ConstructFromVertices(vertices.ToArray());
         body.IsFixed = true;
         PhysicsManager.AddRigidBody(body);
         body.tex = GraphicsGenerator.GenerateTexFromPolygon(body);
         vertices.Clear();
     }
 }
        // Init Functions
        void InitPhysicsEngine()
        {
            pemitter = new ParticleEmmiter(PhysicsManager, new Vector2(1f, .5f), Vector2.One, 10f, 50f, 3f);

            var ground = new PolyBody(1, 4);
            ground.MakeRect(20, 1, (new Vector2(size.Width / 2, 0)));
            ground.IsFixed = true;
            AddAndInit(ground);

            /*var polybody = new PolyBody(1f, (new Random()).Next(5, 5)); //3,8
            polybody.MakeRegularFromRad(.8f);
            polybody.Mass = 1f;
            polybody.Position = new Vector2(size.Width / 2, size.Height / 2);
            polybody.Restitution = 0.0f;

            AddAndInit(polybody);*/

            var rotPlat = new PolyBody(1f, 4);
            rotPlat.MakeRect(8, 1);
            rotPlat.Mass = 4f;
            rotPlat.Position = new Vector2(size.Width / 2, size.Height / 2);
            rotPlat.Restitution = 0.0f;
            rotPlat.IsPivot = true;

            AddAndInit(rotPlat);

            #region bouncer
            float width1 = 4f;
            float width2 = 2f;

            float height = .5f;

            var p1 = new PolyBody(3, 4);
            p1.MakeRect(width1, height);
            p1.Position = new Vector2(size.Width / 2, 1);
            var p1_con1 = -(Vector2.UnitX * width1 / 2 * 3 / 4);
            var p1_con2 = (Vector2.UnitX * width1 / 2 * 3 / 4);

            var p2 = new PolyBody(1, 4);
            p2.MakeRect(width2, height);
            p2.Position = new Vector2(size.Width / 2, 3.5f);
            var p2_con1 = -(Vector2.UnitX * width2 / 2 * 3 / 4) - (Vector2.UnitY * height/2 * 2 / 3);
            var p2_con2 = (Vector2.UnitX * width2 / 2 * 3 / 4) - (Vector2.UnitY * height/2 * 2 / 3);

            Spring s1 = new Spring(p1, p2, p1_con1, p2_con1, 3f, 30f, 1.0f);
            Spring s2 = new Spring(p1, p2, p1_con2, p2_con2, 3f, 30f, 1.0f);

            Spring s3 = new Spring(p1, p2, p1_con1, p2_con2, 3f, 30f, 1.0f);
            Spring s4 = new Spring(p1, p2, p1_con2, p2_con1, 3f, 30f, 1.0f);

            s1.SetLengthToCurrent();
            s2.SetLengthToCurrent();

            PhysicsManager.AddBinaryForceComponent(s1);
            PhysicsManager.AddBinaryForceComponent(s2);
            PhysicsManager.AddBinaryForceComponent(s3);
            PhysicsManager.AddBinaryForceComponent(s4);

            AddAndInit(p1);
            AddAndInit(p2);
            #endregion bouncer
        }
        private void DrawPolygon(PolyBody body)
        {
            Vector2 position = body.Position;
            Color color;
            if (body.IsSleeping)
                color = Color.Black;
            else
                color = Color.Red;

            GraphicsEngine.DrawPolygon((Polygon)body, color, new Vector2(.1f).P2C().X);
            //GraphicsEngine.DrawForces(position, ((PolyBody)body).currForces, 3, .1f);
        }
 private void AddAndInit(PolyBody p)
 {
     PhysicsManager.AddRigidBody(p);
     p.tex = GraphicsGenerator.GenerateTexFromPolygon(p);
 }
 private void FillPolygon(PolyBody body, Actor actor)
 {
     GraphicsEngine.FillPolygon(body, ((PolyBody)body).tex, actor.color);
 }
예제 #21
0
 public static Contact FindFirstContact_ReallyCheap(PolyBody spirit, PolyBody body, Vector2 axis, float disp)
 {
     foreach (var v in spirit.GetTransformedVertices())
     {
         if (TestPointInPoly(body, v))
             return new Contact(spirit, body, v, axis, disp);
     }
     return new Contact(null, null, Vector2.Zero, Vector2.Zero, 0f);
 }