Exemplo n.º 1
0
        protected void SetPoints(Graphics graphics, Bitmap bitmap, ref ImageTexture texture, prim.Point point, ref prim.Size size)
        {
            if (size == null)
            {
                size = new prim.Size(bitmap.Width / graphics.size.w, bitmap.Height / graphics.size.h);
            }
            float[] vertices =
            {
                point.x + size.w, point.y + size.h, 0.0f, 1.0f, 1.0f, // top right
                point.x + size.w, point.y,          0.0f, 1.0f, 0.0f, // bottom right
                point.x,          point.y,          0.0f, 0.0f, 0.0f, // bottom left
                point.x,          point.y + size.h, 0.0f, 0.0f, 1.0f  // top left
            };

            uint[] indices = // note that we start from 0!
            {
                0, 1, 3,     // first triangle
                1, 2, 3      // second triangle
            };

            texture = new ImageTexture(graphics, bitmap, vertices, indices);
        }
Exemplo n.º 2
0
        public override void InitTexture(ref World world, ref Body body, prim.Point point, ref prim.Size size, ref ImageTexture texture, ref MapInterface.ObjectTemplate temp, Graphics graphics, bool isDynamic = false)
        {
            Bitmap bitmap = temp.images["default"][0].image;

            SetPoints(graphics, bitmap, ref texture, point, ref size);
            BodyDef    bodyDef = new BodyDef();
            PolygonDef polyBox = new PolygonDef();
            PolygonDef polyDef = new PolygonDef();

            bodyDef.Position.Set(point.x, point.y);
            polyDef.Density     = 1f;
            polyBox.Density     = 1f;
            bodyDef.Angle       = 0f;
            polyDef.VertexCount = temp.images["default"][0].collisionVectors.Count;
            polyBox.SetAsBox(size.w / 2, size.h / 2);
            List <prim.Point> points = new List <prim.Point>();

            foreach (MapInterface.Line line in temp.images["default"][0].collisionVectors)
            {
                float nx1 = ((float)line.x1 / bitmap.Width) * (float)size.w;
                float nx2 = ((float)line.x2 / bitmap.Width) * (float)size.w;
                float ny1 = ((float)line.y1 / bitmap.Height) * (float)size.h;
                float ny2 = ((float)line.y2 / bitmap.Height) * (float)size.h;
                points.Add(new prim.Point(nx1, ny1));
                points.Add(new prim.Point(nx2, ny2));
            }

            AddCollisionPoints(ref world, ref temp, bodyDef, polyDef, points, ref body);

            if (isDynamic)
            {
                body.SetMassFromShapes();
            }
        }
Exemplo n.º 3
0
        public virtual void InitTexture(ref World world, ref Body body, prim.Point point, ref prim.Size size, ref ImageTexture texture, ref MapInterface.ObjectTemplate temp, Graphics graphics, bool isDynamic = false)
        {
            Bitmap bitmap = temp.images["default"][0].image;

            SetPoints(graphics, bitmap, ref texture, point, ref size);

            BodyDef    bodyDef = new BodyDef();
            PolygonDef polyBox = new PolygonDef();
            PolygonDef polyDef = new PolygonDef();

            bodyDef.Position.Set(point.x, point.y);
            polyDef.Density     = 1f;
            polyBox.Density     = 1f;
            bodyDef.Angle       = 0f;
            polyDef.VertexCount = temp.images["default"][0].collisionVectors.Count;
            polyBox.SetAsBox(size.w / 2, size.h / 2);
            List <prim.Point> points = new List <prim.Point>();

            foreach (MapInterface.Line line in temp.images["default"][0].collisionVectors)
            {
                float nx1 = ((float)line.x1 / bitmap.Width) * (float)size.w;
                float nx2 = ((float)line.x2 / bitmap.Width) * (float)size.w;
                float ny1 = ((float)line.y1 / bitmap.Height) * (float)size.h;
                float ny2 = ((float)line.y2 / bitmap.Height) * (float)size.h;
                points.Add(new prim.Point(nx1, ny1));
                points.Add(new prim.Point(nx2, ny2));
            }
            //TODO: Sort vertices in CCW order
            //Example. Top triangle point -> bottom right -> bottem left
            //points = GetDistinctPoints(points);
            //foreach (prim.Point curPoint in points)
            //{
            //    vCnt += 1;
            //    polyDef.Vertices[vCnt].Set(curPoint.x, curPoint.y);
            //}

            //bodyDef.FixedRotation = true;
            //body = world.CreateBody(bodyDef);
            //body.CreateShape(polyDef);

            AddCollisionPoints(ref world, ref temp, bodyDef, polyDef, points, ref body);

            if (isDynamic)
            {
                body.SetMassFromShapes();
            }
        }