Esempio n. 1
0
        public void AddBody(VoltBody body)
        {
            if (this.count >= this.bodies.Length)
            VoltUtil.ExpandArray(ref this.bodies);

              this.bodies[this.count] = body;
              body.ProxyId = this.count;
              this.count++;
        }
Esempio n. 2
0
    void Awake()
    {
        VoltWorld world = VolatileWorld.Instance.World;
        IEnumerable<VoltShape> shapes = this.shapes.Select((s) => s.PrepareShape(world));

        Vector2 position = transform.position;
        float radians = Mathf.Deg2Rad * transform.eulerAngles.z;

        if (this.isStatic == true)
          this.body = world.CreateStaticBody(position, radians, shapes.ToArray());
        else
          this.body = world.CreateDynamicBody(position, radians, shapes.ToArray());

        this.lastPosition = this.nextPosition = transform.position;
        this.lastAngle = this.nextAngle = transform.eulerAngles.z;
    }
Esempio n. 3
0
        private void AddBodyInternal(VoltBody body)
        {
            this.bodies.Add(body);
              if (body.IsStatic)
            this.staticBroadphase.AddBody(body);
              else
            this.dynamicBroadphase.AddBody(body);

              body.AssignWorld(this);
              if ((this.HistoryLength > 0) && (body.IsStatic == false))
            body.AssignHistory(this.AllocateHistory());
        }
Esempio n. 4
0
        /// <summary>
        /// Updates a single body, resolving only collisions with that body.
        /// If a frame number is provided, all dynamic bodies will store their
        /// state for that frame for later testing.
        /// 
        /// Note: This function is best used with dynamic collisions disabled, 
        /// otherwise you might get symmetric duplicates on collisions.
        /// </summary>
        public void Update(VoltBody body, bool collideDynamic = false)
        {
            if (body.IsStatic)
              {
            VoltDebug.LogWarning("Updating static body, doing nothing");
            return;
              }

              body.Update();
              this.dynamicBroadphase.UpdateBody(body);
              this.BroadPhase(body, collideDynamic);

              this.UpdateCollision();
              this.FreeManifolds();
        }
Esempio n. 5
0
        /// <summary>
        /// Removes a body from the world. The body will be partially reset so it
        /// can be added later. The pointer is still valid and the body can be
        /// returned to the world using AddBody.
        /// </summary>
        public void RemoveBody(VoltBody body)
        {
            VoltDebug.Assert(body.World == this);

              body.PartialReset();

              this.RemoveBodyInternal(body);
        }
Esempio n. 6
0
        /// <summary>
        /// Removes a body from the tree.
        /// </summary>
        public void RemoveBody(VoltBody body)
        {
            int proxyId = body.ProxyId;
              VoltDebug.Assert((0 <= proxyId) && (proxyId < this.nodeCapacity));
              VoltDebug.Assert(this.nodes[proxyId].IsLeaf);

              this.RemoveLeaf(proxyId);
              this.FreeNode(proxyId);

              body.ProxyId = TreeBroadphase.NULL_NODE;
        }
Esempio n. 7
0
 private bool Filter(VoltBody body)
 {
     if (this.ignoreBody != null)
       return body != this.ignoreBody.Body;
     return true;
 }
Esempio n. 8
0
        private void RemoveBodyInternal(VoltBody body)
        {
            this.bodies.Remove(body);
              if (body.IsStatic)
            this.staticBroadphase.RemoveBody(body);
              else
            this.dynamicBroadphase.RemoveBody(body);

              body.FreeHistory();
              body.AssignWorld(null);
        }
Esempio n. 9
0
 public static VoltBodyFilter FilterExcept(VoltBody exception)
 {
     return ((body) => body != exception);
 }
Esempio n. 10
0
 public static bool FilterDynamic(VoltBody body)
 {
     return body.IsStatic;
 }
Esempio n. 11
0
 public static bool FilterAll(VoltBody body)
 {
     return false;
 }
Esempio n. 12
0
   private static void WorldToBody(
 VoltBody body,
 Vector2[] worldVertices, 
 Vector2[] bodyVertices, 
 int count)
   {
       for (int i = 0; i < count; i++)
       bodyVertices[i] = body.WorldToBodyPointCurrent(worldVertices[i]);
   }
Esempio n. 13
0
 internal void Reset()
 {
     this.aabb = default(VoltAABB);
     this.left = NULL_NODE;
     this.right = NULL_NODE;
     this.height = 0;
     this.parentOrNext = NULL_NODE;
     this.body = null;
 }
Esempio n. 14
0
        /// <summary>
        /// Updates a body's position. If the body has moved outside of its
        /// expanded AABB, then the body is removed from the tree and re-inserted.
        /// Otherwise the function returns immediately.
        /// </summary>
        public void UpdateBody(VoltBody body)
        {
            int proxyId = body.ProxyId;
              VoltDebug.Assert((0 <= proxyId) && (proxyId < this.nodeCapacity));

              Node proxyNode = this.nodes[proxyId];
              VoltDebug.Assert(proxyNode.IsLeaf);

              if (proxyNode.aabb.Contains(body.AABB))
            return;
              this.RemoveLeaf(proxyId);

              // Extend AABB
              VoltAABB expanded =
            VoltAABB.CreateExpanded(body.AABB, VoltConfig.AABB_EXTENSION);

              // Predict AABB displacement and sweep the AABB
              //Vector2 sweep = VoltConfig.AABB_MULTIPLIER * displacement;
              //VoltAABB swept = VoltAABB.CreateSwept(expanded, sweep);
              //this.nodes[proxyId].aabb = swept;

              proxyNode.aabb = expanded;
              this.InsertLeaf(proxyId);
              return;
        }
Esempio n. 15
0
        /// <summary>
        /// Identifies collisions for a single body. Does not keep track of 
        /// symmetrical duplicates (they could be counted twice).
        /// </summary>
        private void BroadPhase(VoltBody query, bool collideDynamic = false)
        {
            VoltDebug.Assert(query.IsStatic == false);

              this.reusableBuffer.Clear();
              this.staticBroadphase.QueryOverlap(query.AABB, this.reusableBuffer);
              if (collideDynamic)
            this.dynamicBroadphase.QueryOverlap(query.AABB, this.reusableBuffer);

              this.TestBuffer(query);
        }
Esempio n. 16
0
 public static bool FilterNone(VoltBody body)
 {
     return true;
 }
Esempio n. 17
0
 private void FreeBody(VoltBody body)
 {
     this.bodyPool.Deallocate(body);
 }
Esempio n. 18
0
 public static bool FilterStatic(VoltBody body)
 {
     return (body.IsStatic == false);
 }
Esempio n. 19
0
        private void TestBuffer(VoltBody query)
        {
            for (int i = 0; i < this.reusableBuffer.Count; i++)
              {
            VoltBody test = this.reusableBuffer[i];
            bool canCollide =
              query.CanCollide(test) &&
              test.CanCollide(query) &&
              query.AABB.Intersect(test.AABB);

            if (canCollide)
              for (int i_q = 0; i_q < query.shapeCount; i_q++)
            for (int j_t = 0; j_t < test.shapeCount; j_t++)
              this.NarrowPhase(query.shapes[i_q], test.shapes[j_t]);
              }
        }
Esempio n. 20
0
   /// <summary>
   /// Adds a body to the world. Used for reintroducing bodies that 
   /// have been removed. For new bodies, use CreateBody.
   /// </summary>
   public void AddBody(
 VoltBody body,
 Vector2 position,
 float radians)
   {
       #if DEBUG
         VoltDebug.Assert(body.IsInitialized);
       #endif
         VoltDebug.Assert(body.World == null);
         this.AddBodyInternal(body);
         body.Set(position, radians);
   }
Esempio n. 21
0
 public void UpdateBody(VoltBody body)
 {
     // Do nothing
 }
Esempio n. 22
0
        /// <summary>
        /// Removes a body from the world and deallocates it. The pointer is
        /// invalid after this point.
        /// </summary>
        public void DestroyBody(VoltBody body)
        {
            VoltDebug.Assert(body.World == this);

              body.FreeShapes();

              this.RemoveBodyInternal(body);
              this.FreeBody(body);
        }
Esempio n. 23
0
        public void RemoveBody(VoltBody body)
        {
            int index = body.ProxyId;
              VoltDebug.Assert(index >= 0);
              VoltDebug.Assert(index < this.count);

              int lastIndex = this.count - 1;
              if (index < lastIndex)
              {
            VoltBody lastBody = this.bodies[lastIndex];

            this.bodies[lastIndex].ProxyId = -1;
            this.bodies[lastIndex] = null;

            this.bodies[index] = lastBody;
            lastBody.ProxyId = index;
              }

              this.count--;
        }
Esempio n. 24
0
        /// <summary>
        /// Adds a body to the tree.     
        /// </summary>
        public void AddBody(VoltBody body)
        {
            VoltDebug.Assert(body.ProxyId == TreeBroadphase.NULL_NODE);

              int proxyId;
              Node proxyNode = this.AllocateNode(out proxyId);

              // Expand the aabb
              proxyNode.aabb =
            VoltAABB.CreateExpanded(
              body.AABB,
              VoltConfig.AABB_EXTENSION);

              proxyNode.body = body;
              proxyNode.height = 0;

              this.InsertLeaf(proxyId);
              body.ProxyId = proxyId;
        }