Nez.Collider.collidesWith C# (CSharp) Method

collidesWith() public method

checks to see if this Collider with motion applied (delta movement vector) collides with collider. If it does, true will be returned and result will be populated with collision data.
public collidesWith ( Collider collider, Vector2 motion, CollisionResult &result ) : bool
collider Collider Collider.
motion Vector2 Motion.
result CollisionResult Result.
return bool
		public bool collidesWith( Collider collider, Vector2 motion, out CollisionResult result )
		{
			// alter the shapes position so that it is in the place it would be after movement so we can check for overlaps
			var oldPosition = shape.position;
			shape.position += motion;

			var didCollide = shape.collidesWithShape( collider.shape, out result );
			if( didCollide )
				result.collider = collider;

			// return the shapes position to where it was before the check
			shape.position = oldPosition;

			return didCollide;
		}

Same methods

Collider::collidesWith ( Collider collider, CollisionResult &result ) : bool

Usage Example

Example #1
0
        void IUpdatable.update()
        {
            if (isImmovable)
            {
                velocity = Vector2.Zero;
                return;
            }

            if (shouldUseGravity)
            {
                velocity += Physics.gravity * Time.deltaTime;
            }

            entity.transform.position += velocity * Time.deltaTime;

            CollisionResult collisionResult;

            // fetch anything that we might collide with at our new position
            var neighbors = Physics.boxcastBroadphaseExcludingSelf(_collider, _collider.collidesWithLayers);

            foreach (var neighbor in neighbors)
            {
                // if the neighbor collider is of the same entity, ignore it
                if (neighbor.entity == entity)
                {
                    continue;
                }

                if (_collider.collidesWith(neighbor, out collisionResult))
                {
                    // if the neighbor has an ArcadeRigidbody we handle full collision response. If not, we calculate things based on the
                    // neighbor being immovable.
                    var neighborRigidbody = neighbor.entity.getComponent <ArcadeRigidbody>();
                    if (neighborRigidbody != null)
                    {
                        processOverlap(neighborRigidbody, ref collisionResult.minimumTranslationVector);
                        processCollision(neighborRigidbody, ref collisionResult.minimumTranslationVector);
                    }
                    else
                    {
                        // neighbor has no ArcadeRigidbody so we assume its immovable and only move ourself
                        entity.transform.position -= collisionResult.minimumTranslationVector;
                        var relativeVelocity = velocity;
                        calculateResponseVelocity(
                            ref relativeVelocity,
                            ref collisionResult.minimumTranslationVector,
                            out relativeVelocity);
                        velocity += relativeVelocity;
                    }
                }
            }
        }