Box2DX.Collision.PolygonDef.SetAsBox C# (CSharp) Method

SetAsBox() public method

Build vertices to represent an axis-aligned box.
public SetAsBox ( float hx, float hy ) : void
hx float The half-width
hy float The half-height.
return void
		public void SetAsBox(float hx, float hy)
		{
			VertexCount = 4;
			Vertices[0].Set(-hx, -hy);
			Vertices[1].Set(hx, -hy);
			Vertices[2].Set(hx, hy);
			Vertices[3].Set(-hx, hy);
		}

Same methods

PolygonDef::SetAsBox ( float hx, float hy, Vec2 center, float angle ) : void

Usage Example

        /// <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();
        }