Exemplo n.º 1
0
        public void Reset()
        {
            if (vertexes.Count < 3)
            {
                return;
            }

            if (b2Body != null)
            {
                refWorld.B2World.DestroyBody(b2Body);
                b2Body.Dispose();
            }

            b2BodyDef = new BodyDef();

            b2BodyDef.Angle    = Angle / 180.0f * 3.14f;
            b2BodyDef.Position = PhysicalConvert.Tob2Vector(CenterPosition);

            b2PolygonDefs = DivideToTriangles(vertexes);
            b2Body        = refWorld.B2World.CreateBody(b2BodyDef);

            foreach (var item in b2PolygonDefs)
            {
                b2Body.CreateFixture(item);
            }

            if (physicalShapeType == PhysicalShapeType.Dynamic)
            {
                b2Body.SetMassFromShapes();
            }
        }
Exemplo n.º 2
0
        PolygonDef CreatePolygonDef(asd.Vector2DF vertex1, asd.Vector2DF vertex2, asd.Vector2DF vertex3)
        {
            PolygonDef b2PolygonDef   = new PolygonDef();
            var        sortedVertexes = new List <asd.Vector2DF>();

            sortedVertexes.Add(vertex1);
            sortedVertexes.Add(vertex2);
            sortedVertexes.Add(vertex3);
            sortedVertexes.Sort((a, b) => a.Degree.CompareTo(b.Degree));
            b2PolygonDef.VertexCount = 3;
            for (int i = 0; i < 3; i++)
            {
                b2PolygonDef.Vertices[i] = PhysicalConvert.Tob2Vector(sortedVertexes[i]);
            }

            b2PolygonDef.Density     = Density;
            b2PolygonDef.Restitution = Restitution;
            b2PolygonDef.Friction    = Friction;
            b2PolygonDef.Filter      = new FilterData()
            {
                GroupIndex   = GroupIndex,
                CategoryBits = CategoryBits,
                MaskBits     = MaskBits
            };
            return(b2PolygonDef);
        }
Exemplo n.º 3
0
 public void Reset()
 {
     if (b2Body != null)
     {
         refWorld.B2World.DestroyBody(b2Body);
         b2Body.Dispose();
     }
     b2BodyDef               = new BodyDef();
     b2CircleDef             = new CircleDef();
     b2BodyDef.Angle         = Angle / 180.0f * 3.14f;
     b2BodyDef.Position      = PhysicalConvert.Tob2Vector(Position);
     b2CircleDef.Radius      = PhysicalConvert.Tob2Single(OuterDiameter / 2.0f);
     b2CircleDef.Density     = Density;
     b2CircleDef.Restitution = Restitution;
     b2CircleDef.Friction    = Friction;
     b2CircleDef.Filter      = new FilterData()
     {
         GroupIndex   = GroupIndex,
         CategoryBits = CategoryBits,
         MaskBits     = MaskBits
     };
     b2Body = refWorld.B2World.CreateBody(b2BodyDef);
     b2Body.CreateFixture(b2CircleDef);
     if (physicalShapeType == PhysicalShapeType.Dynamic)
     {
         b2Body.SetMassFromShapes();
     }
 }
Exemplo n.º 4
0
 public void Reset()
 {
     if (b2Body != null)
     {
         refWorld.B2World.DestroyBody(b2Body);
         b2Body.Dispose();
     }
     b2BodyDef          = new BodyDef();
     b2PolygonDef       = new PolygonDef();
     b2BodyDef.Position = PhysicalConvert.Tob2Vector(DrawingArea.Size / 2.0f + DrawingArea.Position);
     CenterPosition     = DrawingArea.Size / 2.0f;
     b2BodyDef.Angle    = Angle / 180.0f * 3.14f;
     b2PolygonDef.SetAsBox(PhysicalConvert.Tob2Single(DrawingArea.Width) / 2.0f, PhysicalConvert.Tob2Single(DrawingArea.Height) / 2.0f);
     b2PolygonDef.Density             = Density;
     b2PolygonDef.Restitution         = Restitution;
     b2PolygonDef.Friction            = Friction;
     b2PolygonDef.Filter.GroupIndex   = GroupIndex;
     b2PolygonDef.Filter.CategoryBits = CategoryBits;
     b2PolygonDef.Filter.MaskBits     = MaskBits;
     b2Body = refWorld.B2World.CreateBody(b2BodyDef);
     b2Body.CreateFixture(b2PolygonDef);
     if (physicalShapeType == PhysicalShapeType.Dynamic)
     {
         b2Body.SetMassFromShapes();
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 衝撃を加える
 /// </summary>
 /// <param name="vector">衝撃を加える方向</param>
 /// <param name="position">衝撃を加えるローカル位置</param>
 public void SetImpulse(asd.Vector2DF vector, asd.Vector2DF position)
 {
     if (!IsActive)
     {
         return;
     }
     b2Body.ApplyImpulse(PhysicalConvert.Tob2Vector(vector), PhysicalConvert.Tob2Vector(CenterPosition + position));
 }
Exemplo n.º 6
0
 /// <summary>
 /// 力を加える
 /// </summary>
 /// <param name="vector">力を加える方向</param>
 /// <param name="position">力を加えるローカル位置</param>
 public void SetForce(asd.Vector2DF vector, asd.Vector2DF position)
 {
     if (!IsActive)
     {
         return;
     }
     b2Body.ApplyForce(PhysicalConvert.Tob2Vector(vector), PhysicalConvert.Tob2Vector(DrawingArea.Position + position));
 }
Exemplo n.º 7
0
 public void SyncB2body()
 {
     if (!IsActive)
     {
         return;
     }
     base.Position = PhysicalConvert.ToAsdVector(b2Body.GetPosition());
     base.Angle    = b2Body.GetAngle() * 180.0f / 3.14f;
 }
Exemplo n.º 8
0
 public void SyncB2body()
 {
     if (!IsActive)
     {
         return;
     }
     asd.Vector2DF move = PhysicalConvert.ToAsdVector(b2Body.GetPosition()) - (DrawingArea.Size / 2.0f + DrawingArea.Position);
     base.DrawingArea = new asd.RectF(DrawingArea.Position + move, DrawingArea.Size);
     base.Angle       = b2Body.GetAngle() * 180.0f / 3.14f;
 }
Exemplo n.º 9
0
 public void SyncB2body()
 {
     if (!IsActive)
     {
         return;
     }
     centerPosition = PhysicalConvert.ToAsdVector(b2Body.GetPosition());
     angle          = b2Body.GetAngle() * 180.0f / 3.14f;
     for (int i = 0; i < 3; i++)
     {
         asd.Vector2DF temp = vertexes[i];
         temp.Degree += Angle;
         base.SetPointByIndex(CenterPosition + temp, i);
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// ワールドを初期化
        /// </summary>
        /// <param name="worldRect">適用範囲</param>
        /// <param name="gravity">重力</param>
        public PhysicalWorld(asd.RectF worldRect, asd.Vector2DF gravity)
        {
            physicalShapes      = new List <PhysicalShape>();
            collisionController = new CollisionController(this);
            AABB aabb = new AABB();

            aabb.LowerBound = PhysicalConvert.Tob2Vector(worldRect.Position);
            aabb.UpperBound = PhysicalConvert.Tob2Vector(worldRect.Vertexes[2]);
            b2World         = new World(aabb, PhysicalConvert.Tob2Vector(gravity), true);
            b2World.SetContactListener(collisionController);
            b2World.SetContactFilter(new ContactFilter());
            TimeStep           = 1.0f / 60.0f;
            VelocityItetions   = 8;
            PositionIterations = 1;
        }
Exemplo n.º 11
0
 public void SyncB2body()
 {
     if (!IsActive)
     {
         return;
     }
     centerPosition = PhysicalConvert.ToAsdVector(b2Body.GetPosition());
     angle          = b2Body.GetAngle() * 180.0f / 3.14f;
     base.ClearVertexes();
     foreach (var item in vertexes)
     {
         asd.Vector2DF temp = item;
         temp.Degree += Angle;
         base.AddVertex(CenterPosition + temp);
     }
 }
Exemplo n.º 12
0
        public void Reset()
        {
            if (b2Body != null)
            {
                refWorld.B2World.DestroyBody(b2Body);
                b2Body.Dispose();
            }

            b2BodyDef    = new BodyDef();
            b2PolygonDef = new PolygonDef();

            b2BodyDef.Angle    = Angle / 180.0f * 3.14f;
            b2BodyDef.Position = PhysicalConvert.Tob2Vector(CenterPosition);

            var sortedVertexes = new List <asd.Vector2DF>();

            foreach (var item in vertexes)
            {
                sortedVertexes.Add(item);
            }
            sortedVertexes.Sort((a, b) => a.Degree.CompareTo(b.Degree));
            b2PolygonDef.VertexCount = 3;
            for (int i = 0; i < 3; i++)
            {
                b2PolygonDef.Vertices[i] = PhysicalConvert.Tob2Vector(sortedVertexes[i]);
            }

            b2PolygonDef.Density     = Density;
            b2PolygonDef.Restitution = Restitution;
            b2PolygonDef.Friction    = Friction;
            b2PolygonDef.Filter      = new FilterData()
            {
                GroupIndex   = GroupIndex,
                CategoryBits = CategoryBits,
                MaskBits     = MaskBits
            };

            b2Body = refWorld.B2World.CreateBody(b2BodyDef);
            b2Body.CreateFixture(b2PolygonDef);

            if (physicalShapeType == PhysicalShapeType.Dynamic)
            {
                b2Body.SetMassFromShapes();
            }
        }
Exemplo n.º 13
0
        void IContactListener.BeginContact(Contact contact)
        {
            if (!contact.AreTouching)
            {
                return;
            }
            CollisionData temp = new CollisionData();
            WorldManifold worldManifold;

            contact.GetWorldManifold(out worldManifold);
            temp.BodyA  = contact.FixtureA.Body;
            temp.BodyB  = contact.FixtureB.Body;
            temp.Points = new List <asd.Vector2DF>();
            for (int i = 0; i < contact.Manifold.PointCount; i++)
            {
                temp.Points.Add(PhysicalConvert.ToAsdVector(worldManifold.Points[i]));
            }
            collisionShapes.Add(temp);
        }
Exemplo n.º 14
0
        void IContactListener.PostSolve(Contact contact, ContactImpulse impulse)
        {
            CollisionData temp = new CollisionData();

            foreach (var item in collisionShapes)
            {
                if (item.BodyA == contact.FixtureA.Body && item.BodyB == contact.FixtureB.Body)
                {
                    temp = item;
                    break;
                }
            }
            temp.Points.Clear();
            WorldManifold worldManifold;

            contact.GetWorldManifold(out worldManifold);
            for (int i = 0; i < contact.Manifold.PointCount; i++)
            {
                temp.Points.Add(PhysicalConvert.ToAsdVector(worldManifold.Points[i]));
            }
        }
Exemplo n.º 15
0
 /// <summary>
 /// 初期化
 /// </summary>
 /// <param name="shapeType">物理形状タイプ</param>
 /// <param name="world">登録するワールド</param>
 public PhysicalRectangleShape(PhysicalShapeType shapeType, PhysicalWorld world)
 {
     density            = 1;
     restitution        = 0;
     friction           = 0.6f;
     groupIndex         = 0;
     categoryBits       = 0x0001;
     maskBits           = 0xffff;
     b2BodyDef          = new BodyDef();
     b2PolygonDef       = new PolygonDef();
     refWorld           = world;
     physicalShapeType  = shapeType;
     b2BodyDef.Position = PhysicalConvert.Tob2Vector(new asd.Vector2DF());
     b2PolygonDef.SetAsBox(PhysicalConvert.Tob2Single(1) / 2.0f, PhysicalConvert.Tob2Single(1) / 2.0f);
     b2Body = refWorld.B2World.CreateBody(b2BodyDef);
     b2Body.CreateFixture(b2PolygonDef);
     if (physicalShapeType == PhysicalShapeType.Dynamic)
     {
         b2Body.SetMassFromShapes();
     }
     world.Add(this);
 }