Inheritance: ShapeDef
Ejemplo n.º 1
0
        /// <summary>
        /// Apply properties in text box to currently selected object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void b_ApplyProperties_Click(object sender, EventArgs e)
        {
            // apply these new settings
            if (currentlySelectedObject != null && AreObjPropertiesValid())
            {
                float newScale = float.Parse(tb_Scale.Text);
                currentlySelectedObject.Width = (currentlySelectedObject.Width / currentlySelectedObject.scale) * newScale;

                if ((currentlySelectedObject is InstasteelCircleObject || (currentlySelectedObject is CircleObject && !(currentlySelectedObject is PaintedObject))))
                {
                    currentlySelectedObject.Height = currentlySelectedObject.Width;

                    float radius = (float)currentlySelectedObject.Width / (2 * CASSWorld.SCALE);

                    // circle objects only
                    currentlySelectedObject.RemoveFromWorld();
                    CircleDef shape = new CircleDef();
                    shape.Radius = radius;
                    // HACK HACK HACK - this won't work for objects that have more than one shape!
                    shape.Density = currentlySelectedObject.shapes[0].Density;
                    shape.Friction = currentlySelectedObject.shapes[0].Friction;
                    shape.Restitution = currentlySelectedObject.shapes[0].Restitution;
                    currentlySelectedObject.shapes.Clear(); // get rid of the old, unscaled shape
                    currentlySelectedObject.shapes.Add(shape); // add the new one
                    currentlySelectedObject.AddToWorld();
                }
                else if (currentlySelectedObject.TextureFilename == "Art\\Objects\\BoxObjects\\bottomTexture2273")
                {
                    float halfWidth = (float)currentlySelectedObject.Width / (2 * CASSWorld.SCALE);
                    float halfHeight = (float)currentlySelectedObject.Height / (2 * CASSWorld.SCALE);

                    // box object only...
                    currentlySelectedObject.RemoveFromWorld();
                    // Create the collision shape
                    PolygonDef shape = new PolygonDef();
                    shape.SetAsBox(halfWidth, halfHeight);
                    // HACK HACK HACK - this won't work for objects that have more than one shape!
                    shape.Density = currentlySelectedObject.shapes[0].Density;
                    shape.Friction = currentlySelectedObject.shapes[0].Friction;
                    shape.Restitution = currentlySelectedObject.shapes[0].Restitution;
                    currentlySelectedObject.shapes.Clear(); // get rid of the old, unscaled shape
                    currentlySelectedObject.shapes.Add(shape); // add the new one
                    currentlySelectedObject.AddToWorld();
                }
                else if (currentlySelectedObject is InstasteelObject || !(currentlySelectedObject is PaintedObject))
                {
                    currentlySelectedObject.Height = (currentlySelectedObject.Height / currentlySelectedObject.scale) * newScale;
                    // Determine dimensions
                    float halfWidth = (float)currentlySelectedObject.Width / (2 * CASSWorld.SCALE);
                    float halfHeight = (float)currentlySelectedObject.Height / (2 * CASSWorld.SCALE);

                    // box object only...
                    currentlySelectedObject.RemoveFromWorld();
                    // Create the collision shape
                    PolygonDef shape = new PolygonDef();
                    shape.SetAsBox(halfWidth, halfHeight);
                    // HACK HACK HACK - this won't work for objects that have more than one shape!
                    shape.Density = currentlySelectedObject.shapes[0].Density;
                    shape.Friction = currentlySelectedObject.shapes[0].Friction;
                    shape.Restitution = currentlySelectedObject.shapes[0].Restitution;
                    currentlySelectedObject.shapes.Clear(); // get rid of the old, unscaled shape
                    currentlySelectedObject.shapes.Add(shape); // add the new one
                    currentlySelectedObject.AddToWorld();
                }

                if ((bool)cBox_StaticObject.Checked)
                {
                    MassData mass = new MassData();
                    mass.Mass = 0f;
                    currentlySelectedObject.Body.SetMass(mass);
                }
                else if (currentlySelectedObject.Body.GetMass()==0)
                {
                    MassData mass = new MassData();
                    mass.Mass = 1f;
                    currentlySelectedObject.Body.SetMass(mass);
                    currentlySelectedObject.Body.GetShapeList().Density = 1f;
                    currentlySelectedObject.Body.SetMassFromShapes();
                }

                float newRotation = float.Parse(tb_Rotation.Text);
                currentlySelectedObject.Angle = MathHelper.ToRadians(newRotation);

                currentlySelectedObject.scale = newScale;

                float newbound1 = float.Parse(tb_bound1.Text);
                float newbound2 = float.Parse(tb_bound2.Text);
                if (currentlySelectedObject is MovingObject)
                {

                    MovingObject temp = (MovingObject)currentlySelectedObject;
                    temp.bound1 = temp.Position.Y - newbound1;
                    temp.bound2 = temp.Position.Y + newbound2;
                    Console.WriteLine(temp.bound1 + " " + temp.bound2);
                    currentlySelectedObject = temp;

                }
                else if (currentlySelectedObject is HorizontalMovingObject)
                {
                    HorizontalMovingObject temp = (HorizontalMovingObject)currentlySelectedObject;
                    temp.bound1 = temp.Position.X - newbound1;
                    temp.bound2 = temp.Position.X + newbound2;
                    currentlySelectedObject = temp;
                }

            }

               pb_Level.Refresh();
        }
Ejemplo n.º 2
0
        internal PolygonShape(ShapeDef def)
            : base(def)
        {
            Box2DXDebug.Assert(def.Type == ShapeType.PolygonShape);
            _type = ShapeType.PolygonShape;
            PolygonDef poly = (PolygonDef)def;

            // Get the vertices transformed into the body frame.
            _vertexCount = poly.VertexCount;
            Box2DXDebug.Assert(3 <= _vertexCount && _vertexCount <= Settings.MaxPolygonVertices);

            // Copy vertices.
            for (int i = 0; i < _vertexCount; ++i)
            {
                _vertices[i] = poly.Vertices[i];
            }

            // Compute normals. Ensure the edges have non-zero length.
            for (int i = 0; i < _vertexCount; ++i)
            {
                int  i1   = i;
                int  i2   = i + 1 < _vertexCount ? i + 1 : 0;
                Vec2 edge = _vertices[i2] - _vertices[i1];
                Box2DXDebug.Assert(edge.LengthSquared() > Common.Settings.FLT_EPSILON * Common.Settings.FLT_EPSILON);
                _normals[i] = Vec2.Cross(edge, 1.0f);
                _normals[i].Normalize();
            }

#if DEBUG
            // Ensure the polygon is convex.
            for (int i = 0; i < _vertexCount; ++i)
            {
                for (int j = 0; j < _vertexCount; ++j)
                {
                    // Don't check vertices on the current edge.
                    if (j == i || j == (i + 1) % _vertexCount)
                    {
                        continue;
                    }

                    // Your polygon is non-convex (it has an indentation).
                    // Or your polygon is too skinny.
                    float s = Vec2.Dot(_normals[i], _vertices[j] - _vertices[i]);
                    Box2DXDebug.Assert(s < -Settings.LinearSlop);
                }
            }

            // Ensure the polygon is counter-clockwise.
            for (int i = 1; i < _vertexCount; ++i)
            {
                float cross = Vec2.Cross(_normals[i - 1], _normals[i]);

                // Keep asinf happy.
                cross = Common.Math.Clamp(cross, -1.0f, 1.0f);

                // You have consecutive edges that are almost parallel on your polygon.
                float angle = (float)System.Math.Asin(cross);
                Box2DXDebug.Assert(angle > Settings.AngularSlop);
            }
#endif

            // Compute the polygon centroid.
            _centroid = ComputeCentroid(poly.Vertices, poly.VertexCount);

            // Compute the oriented bounding box.
            ComputeOBB(out _obb, _vertices, _vertexCount);

            // Create core polygon shape by shifting edges inward.
            // Also compute the min/max radius for CCD.
            for (int i = 0; i < _vertexCount; ++i)
            {
                int i1 = i - 1 >= 0 ? i - 1 : _vertexCount - 1;
                int i2 = i;

                Vec2 n1 = _normals[i1];
                Vec2 n2 = _normals[i2];
                Vec2 v  = _vertices[i] - _centroid;;

                Vec2 d = new Vec2();
                d.X = Vec2.Dot(n1, v) - Settings.ToiSlop;
                d.Y = Vec2.Dot(n2, v) - Settings.ToiSlop;

                // Shifting the edge inward by b2_toiSlop should
                // not cause the plane to pass the centroid.

                // Your shape has a radius/extent less than b2_toiSlop.
                Box2DXDebug.Assert(d.X >= 0.0f);
                Box2DXDebug.Assert(d.Y >= 0.0f);
                Mat22 A = new Mat22();
                A.Col1.X         = n1.X; A.Col2.X = n1.Y;
                A.Col1.Y         = n2.X; A.Col2.Y = n2.Y;
                _coreVertices[i] = A.Solve(d) + _centroid;
            }
        }
Ejemplo n.º 3
0
        internal PolygonShape(ShapeDef def) : base(def)
        {
            Box2DXDebug.Assert(def.Type == ShapeType.PolygonShape);
            this._type = ShapeType.PolygonShape;
            PolygonDef polygonDef = (PolygonDef)def;

            this._vertexCount = polygonDef.VertexCount;
            Box2DXDebug.Assert(3 <= this._vertexCount && this._vertexCount <= Settings.MaxPolygonVertices);
            for (int i = 0; i < this._vertexCount; i++)
            {
                this._vertices[i] = polygonDef.Vertices[i];
            }
            for (int i = 0; i < this._vertexCount; i++)
            {
                int  num  = i;
                int  num2 = (i + 1 < this._vertexCount) ? (i + 1) : 0;
                Vec2 a    = this._vertices[num2] - this._vertices[num];
                Box2DXDebug.Assert(a.LengthSquared() > Settings.FLT_EPSILON * Settings.FLT_EPSILON);
                this._normals[i] = Vec2.Cross(a, 1f);
                this._normals[i].Normalize();
            }
            for (int i = 0; i < this._vertexCount; i++)
            {
                for (int j = 0; j < this._vertexCount; j++)
                {
                    if (j != i && j != (i + 1) % this._vertexCount)
                    {
                        float num3 = Vec2.Dot(this._normals[i], this._vertices[j] - this._vertices[i]);
                        Box2DXDebug.Assert(num3 < -Settings.LinearSlop);
                    }
                }
            }
            for (int i = 1; i < this._vertexCount; i++)
            {
                float num4 = Vec2.Cross(this._normals[i - 1], this._normals[i]);
                num4 = Box2DX.Common.Math.Clamp(num4, -1f, 1f);
                float num5 = (float)System.Math.Asin((double)num4);
                Box2DXDebug.Assert(num5 > Settings.AngularSlop);
            }
            this._centroid = PolygonShape.ComputeCentroid(polygonDef.Vertices, polygonDef.VertexCount);
            PolygonShape.ComputeOBB(out this._obb, this._vertices, this._vertexCount);
            for (int i = 0; i < this._vertexCount; i++)
            {
                int  num  = (i - 1 >= 0) ? (i - 1) : (this._vertexCount - 1);
                int  num2 = i;
                Vec2 a2   = this._normals[num];
                Vec2 a3   = this._normals[num2];
                Vec2 b    = this._vertices[i] - this._centroid;
                Vec2 b2   = default(Vec2);
                b2.X = Vec2.Dot(a2, b) - Settings.ToiSlop;
                b2.Y = Vec2.Dot(a3, b) - Settings.ToiSlop;
                Box2DXDebug.Assert(b2.X >= 0f);
                Box2DXDebug.Assert(b2.Y >= 0f);
                Mat22 mat = default(Mat22);
                mat.Col1.X            = a2.X;
                mat.Col2.X            = a2.Y;
                mat.Col1.Y            = a3.X;
                mat.Col2.Y            = a3.Y;
                this._coreVertices[i] = mat.Solve(b2) + this._centroid;
            }
        }