Nez.Portable/Physics/SpatialHash.cs
/// <summary>
/// returns true if the hits array gets filled. cell must not be null!
/// </summary>
/// <returns><c>true</c>, if ray intersection was checked, <c>false</c> otherwise.</returns>
/// <param name="ray">Ray.</param>
/// <param name="cellX">Cell x.</param>
/// <param name="cellY">Cell y.</param>
/// <param name="cell">Cell.</param>
/// <param name="hits">Hits.</param>
/// <param name="hitCounter">Hit counter.</param>
returns true if the hits array gets filled. cell must not be null!
returns true if the hits array gets filled . cell must not be null!
[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool checkRayIntersection(int cellX, int cellY, List<Collider> cell)
        {
            float fraction;
            for (var i = 0; i < cell.Count; i++)
            {
                var potential = cell[i];
                if (_checkedColliders.Contains(potential))
                    continue;
                _checkedColliders.Add(potential);
                if (potential.isTrigger && !Physics.raycastsHitTriggers)
                    continue;
                if (!Flags.isFlagSet(_layerMask, potential.physicsLayer))
                    continue;
                var colliderBounds = potential.bounds;
                if (colliderBounds.rayIntersects(ref _ray, out fraction) && fraction <= 1.0f)
                {
                    if (potential.shape.collidesWithLine(_ray.start, _ray.end, out _tempHit))
                    {
                        if (!Physics.raycastsStartInColliders && potential.shape.containsPoint(_ray.start))
                            continue;
                        _tempHit.collider = potential;
                        _cellHits.Add(_tempHit);
                    }
                }
            }
            if (_cellHits.Count == 0)
                return false;
            _cellHits.Sort(compareRaycastHits);
            for (var i = 0; i < _cellHits.Count; i++)
            {
                _hits[hitCounter] = _cellHits[i];
                hitCounter++;
                if (hitCounter == _hits.Length)
                    return true;
            }
            return false;
        }
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] public bool checkRayIntersection ( int cellX , int cellY , List < Collider > cell ) { float fraction ; for ( var i = 0 ; i < cell . Count ; i ++ ) { var potential = cell [ i ] ; if ( _checkedColliders . Contains ( potential ) ) continue ; _checkedColliders . Add ( potential ) ; if ( potential . isTrigger && ! Physics . raycastsHitTriggers ) continue ; if ( ! Flags . isFlagSet ( _layerMask , potential . physicsLayer ) ) continue ; var colliderBounds = potential . bounds ; if ( colliderBounds . rayIntersects ( ref _ray , out fraction ) && fraction <= 1.0f ) { if ( potential . shape . collidesWithLine ( _ray . start , _ray . end , out _tempHit ) ) { if ( ! Physics . raycastsStartInColliders && potential . shape . containsPoint ( _ray . start ) ) continue ; _tempHit . collider = potential ; _cellHits . Add ( _tempHit ) ; } } } if ( _cellHits . Count == 0 ) return false ; _cellHits . Sort ( compareRaycastHits ) ; for ( var i = 0 ; i < _cellHits . Count ; i ++ ) { _hits [ hitCounter ] = _cellHits [ i ] ; hitCounter ++ ; if ( hitCounter == _hits . Length ) return true ; } return false ; }
returns true if the hits array gets filled.
returns true if the hits array gets filled .
// manage which colliders we already processed // only hit triggers if we are set to do so // make sure the Collider is on the layerMask // TODO: is rayIntersects performant enough? profile it. Collisions.rectToLine might be faster // TODO: if the bounds check returned more data we wouldnt have to do any more for a BoxCollider check // first a bounds check before doing a shape test // check to see if the raycast started inside the collider if we should excluded those rays // TODO: make sure the collision point is in the current cell and if it isnt store it off for later evaluation // this would be for giant objects with odd shapes that bleed into adjacent cells // _hitTesterRect.X = cellX * _cellSize; // _hitTesterRect.Y = cellY * _cellSize; // if( !_hitTesterRect.Contains( _tempHit.point ) ) // all done processing the cell. sort the results and pack the hits into the result array // increment the hit counter and if it has reached the array size limit we are done
None
cellX
int
None
cellY
int
None
cell
List<Collider>
None
ray
None
hits
None
hitCounter
None