if ( EntityIntheArea.Count > 0 )
{
for ( int i = 0; i < EntityIntheArea.Count; i++ )
{
//Do not check collision betwen the same object
int EntityItSelfIndex = i;
for ( int j = 0; j < EntityIntheArea.Count; j++ )
{
if ( j > i )
{
if ( EntityIntheArea[j].CollisionBox.Intersects (EntityIntheArea[i].CollisionBox) )
CollisionManager.AddCollisionUnite (EntityIntheArea[j], EntityIntheArea[i]);
}
}
}
}
///
/// Update the Area
///
public void Update( GameTime gameTime )
{
/**
* in this loop we check the collision of object with other objects
* we check collision only betwen object with i index and object with i+1 index
* exemple to demonstrate the utility :
* check collision betwen Object[0] and Object[1]
* check collision betwen Object[0] and Object[2]
* check collision betwen Object[0] and Object[3]
*
* we dont need to check collision betwen Object[1] and Objetc[0] any more because its already checked when we
* was in Object[0] check for collision routine
* if we have N object, we will have : 1+2+...+n-1 calcule
* exemple : we Have 4 Objects : Obj[0],Obj[1],Obj[2],Obj[3]
* Object | Collision with
* --------------------------
* Obj[0] | Obj[1],Obj[2],Obj[3]
* Obj[1] | Obj[2],Obj[3]
* Obj[2] | Obj[3]
* Obj[3] | non need for check
*
* we have : 4 Objects thant we need : 1+2+3 = 6 collision check
* with simple collision check we will need : (4x4)-4 = 12 collision check
*
* we save 6collision check with this solution !
**/
if ( EntityIntheArea.Count > 0 )
{
for ( int i = 0; i < EntityIntheArea.Count; i++ )
{
//Do not check collision betwen the same object
int EntityItSelfIndex = i;
for ( int j = ( EntityItSelfIndex + 1 ); j < EntityIntheArea.Count; j++ )
{
if ( EntityIntheArea[i].CollisionBox.Intersects (EntityIntheArea[j].CollisionBox) )
CollisionManager.AddCollisionUnite (EntityIntheArea[i], EntityIntheArea[j]);
}
}
}
// the Entity list it will update evrey frame, we should clear it after checking all entity collision possibility
EntityIntheArea.Clear ();
}
وفي 15 يناير 2011 08:35 م، أعرب عبد اللطيف حاجي علي عن رأيه بالموقف كالآتي:
أيضاً ماذا يحدث إذا ألغيت النداء للإجراء Update? وماذا يحدث إذا ألغيت النداء للإجراء AddCollisionUnite? if ( Entity.IsVisible )
{
// Get the index of area of each corner of the sprite
Entity.AddNewAreaLocationIndex (AreaManager.GetAreaIndex (Entity.UpperLeftCorner));
AreaManager.GetArea (Entity.UpperLeftCorner).AddEntity (Entity); ;
Entity.AddNewAreaLocationIndex (AreaManager.GetAreaIndex (Entity.UpperRightCorner));
AreaManager.GetArea (Entity.UpperRightCorner).AddEntity (Entity);
Entity.AddNewAreaLocationIndex (AreaManager.GetAreaIndex (Entity.BottomLeftCorner));
AreaManager.GetArea (Entity.BottomLeftCorner).AddEntity (Entity);
Entity.AddNewAreaLocationIndex (AreaManager.GetAreaIndex (Entity.BottomRightCorner));
AreaManager.GetArea (Entity.BottomRightCorner).AddEntity (Entity);
}
و بالخصوص الدالة :AddEntity (Entity);
عندما تحذف كل شيئ يسير على مايرام. ///
/// retur the area refered by X and Y entity position
///
public static WorldArea GetArea( Rectangle NewPosition )
{
return AreaInGameWorld[(int)(
( NewPosition.X * m_EntityPositionXToAreaIndexFactor ) * m_NumberOfAreaVerticaly
+ ( NewPosition.Y * m_EntityPositionYToAreaIndexFactor )
)];
}
///
/// retur the area refered by X and Y entity position
///
public static WorldArea GetArea( Vector2 NewPosition )
{
return AreaInGameWorld[(int)(
( NewPosition.X * m_EntityPositionXToAreaIndexFactor ) * m_NumberOfAreaVerticaly
+ ( NewPosition.Y * m_EntityPositionYToAreaIndexFactor )
)];
}
///
/// retur the area in the list by referd index i,j
///
public static WorldArea GetArea( int NewIndexI, int NewIndexJ )
{
return AreaInGameWorld[( NewIndexI * m_NumberOfAreaVerticaly ) + NewIndexJ];
}
///
/// retur the area in the list by referd index AreaHashIndex
///
public static WorldArea GetArea( int NewArea )
{
return AreaInGameWorld[NewArea];
}
( NewPosition.X * m_EntityPositionXToAreaIndexFactor ) * m_NumberOfAreaVerticaly
+ ( NewPosition.Y * m_EntityPositionYToAreaIndexFactor )
///
/// retur the area refered by X and Y entity position
///
public static WorldArea GetArea( Rectangle NewPosition )
{
//int i= (int)( ( NewPosition.X * m_EntityPositionXToAreaIndexFactor ) * m_NumberOfAreaVerticaly
//+ ( NewPosition.Y * m_EntityPositionYToAreaIndexFactor ) );
return AreaInGameWorld[0];
//(int)(
// 1//( NewPosition.X * m_EntityPositionXToAreaIndexFactor ) * m_NumberOfAreaVerticaly
// + ( NewPosition.Y * m_EntityPositionYToAreaIndexFactor )
//)];
}
------------------------------------------------------
///
/// retur the area in the list by referd index AreaHashIndex
///
public static WorldArea GetArea( int NewArea )
{
return AreaInGameWorld[NewArea];
}
Entity.UpperRightCorne
Entity.UpperLeftCorner
Entity.BottomLeftCorner
Entity.BottomRightCorner
----------
///
/// The Uper Left Coordinate
///
public Vector2 UpperLeftCorner
{
get
{
return new Vector2 (CollisionBox.X, CollisionBox.Y);
}
}
///
/// The Uper Right Coordinate
///
public Vector2 UpperRightCorner
{
get
{
return new Vector2 (CollisionBox.X + CollisionBox.Width, m_Sprite.Y);
}
}
///
/// The Bottom Left Coordinate
///
public Vector2 BottomLeftCorner
{
get
{
return new Vector2 (CollisionBox.X, CollisionBox.Y+CollisionBox.Height);
}
}
///
/// The Bottom Right Coordinate
///
public Vector2 BottomRightCorner
{
get
{
return new Vector2 (CollisionBox.X + CollisionBox.Width, CollisionBox.Y + CollisionBox.Height);
}
}
public void AddEntity( DynamicSprite NewEntity )
{
if ( !( EntityIntheArea.Contains (NewEntity) ) )
EntityIntheArea.Add (NewEntity);
}
public void AddEntity( DynamicSprite NewEntity )
{
for ( int i = 0; i < NewEntity.GetAreaLocationIndex ().Count; i++ )
{
if ( NewEntity.GetAreaLocationIndex ().ElementAt (i) == m_Index )
{
EntityIntheArea.Add (NewEntity);
break;
}
}
}
Entity.UpperRightCorne
Entity.UpperLeftCorner
Entity.BottomLeftCorner
Entity.BottomRightCorner
----------
public Vector2 UpperLeftCorner
{
get { return new Vector2 (CollisionBox.X, CollisionBox.Y); }
}
public Vector2 UpperRightCorner
{
get { return new Vector2 (CollisionBox.X + CollisionBox.Width, m_Sprite.Y); }
}
public Vector2 BottomLeftCorner
{
get { return new Vector2 (CollisionBox.X, CollisionBox.Y+CollisionBox.Height); }
}
public Vector2 BottomRightCorner
{
get
{
return new Vector2
(CollisionBox.X + CollisionBox.Width, CollisionBox.Y + CollisionBox.Height);
}
}
m_UpperLeftCorner.X = CollisionBox.X;
m_UpperLeftCorner.Y = CollisionBox.Y;
m_UpperRightCorner.X = CollisionBox.X + CollisionBox.Width;
m_UpperRightCorner.Y = CollisionBox.Y;
m_BottomLeftCorner.X = CollisionBox.X;
m_BottomLeftCorner.Y = CollisionBox.Y+CollisionBox.Height;
m_BottomRightCorner.X= CollisionBox.X + CollisionBox.Width;
m_BottomRightCorner.Y = CollisionBox.Y + CollisionBox.Height;