VG:Solid Collision

From MyProjectFun
Jump to: navigation, search

If you want a sprite to block the player sprite (such that the sprite cannot pass through it), you'll need to attach this behavior to your walls:

/// <summary>
/// Keep the player from passing through the attaching sprite
/// </summary>
public static void SolidCollisionFN()
{
  // Find the player sprite
  Sprite Player = This.Sprite.CollisionWithSprite("Player" );
 
  // If we have the player
  if (Player != null)
  {
    // Find out where our player is
    bool PlayerOnLeft =
    Player.Position.X + Player.GetWidth() - Player.Velocity.X < This.Sprite.Position.X;
    bool PlayerOnRight =
    Player.Position.X - Player.Velocity.X > This.Sprite.Position.X + This.Sprite.GetWidth();
    bool PlayerOnTop =
    Player.Position.Y + Player.GetHeight() - Player.Velocity.Y < This.Sprite.Position.Y;
    bool PlayerOnBottom =
    Player.Position.Y - Player.Velocity.Y > This.Sprite.Position.Y + This.Sprite.GetHeight();
 
    // If the player is on the left
    if (PlayerOnLeft)
    {
      // Place the player to our left and stop him
      Player.Position.X = This.Sprite.Position.X - Player.GetWidth() - 1;
      Player.Velocity.X = 0;
    }
    // If the player is on the right
    else if (PlayerOnRight)
    {
      // Place the player to our right and stop him
      Player.Position.X = This.Sprite.Position.X + This.Sprite.GetWidth() + 1;
      Player.Velocity.X = 0;
    }
    // If the player is on the top
    else if (PlayerOnTop)
    {
      // Place the player above us and stop him
      Player.Position.Y = This.Sprite.Position.Y - Player.GetHeight();
      Player.Velocity.Y = 0;
    }
    // If the player is on the bottom
    else if (PlayerOnBottom)
    {
      // Place the player below us and stop him
      Player.Position.Y = This.Sprite.Position.Y + This.Sprite.GetHeight();
      Player.Velocity.Y = 0;
    }
  }
}

Another form of this that's very useful is one that will keep sprites from passing through a wall (instead of the wall keeping a sprite from passing through itself). This is a function you can attach to your enemies or bullets to keep them from passing through a sprite named Wall:

/// <summary>
/// Keep a sprite from passing through a sprite named Wall
/// </summary>
public static void SolidCollisionForEnemyFN()
{
  // Find the wall sprite
  Sprite Wall = This.Sprite.CollisionWithSprite("Wall" );
 
  // If we have the wall
  if (Wall != null)
  {
    // Find out where our wall is
    bool WallOnLeft =
    Wall.Position.X + Wall.GetWidth() < This.Sprite.Position.X - This.Sprite.Velocity.X;
    bool WallOnRight =
    Wall.Position.X > This.Sprite.Position.X + This.Sprite.GetWidth() - This.Sprite.Velocity.X;
    bool WallOnTop =
    Wall.Position.Y + Wall.GetHeight() < This.Sprite.Position.Y - This.Sprite.Velocity.Y;
    bool WallOnBottom =
    Wall.Position.Y > This.Sprite.Position.Y + This.Sprite.GetHeight() - This.Sprite.Velocity.Y;
 
    // If the wall is on the left
    if (WallOnLeft)
    {
      // Place us to the wall's right and stop
      This.Sprite.Position.X = Wall.Position.X + Wall.GetWidth() + 1;
      This.Sprite.Velocity.X = 0;
    }
    // If the wall is on the right
    else if (WallOnRight)
    {
      // Place us to the wall's left and stop
      This.Sprite.Position.X = Wall.Position.X - This.Sprite.GetWidth() - 1;
      This.Sprite.Velocity.X = 0;
    }
    // If the wall is on the top
    else if (WallOnTop)
    {
      // Place us below the wall and stop
      This.Sprite.Position.Y = Wall.Position.Y + Wall.GetHeight() + 1;
      This.Sprite.Velocity.Y = 0;
    }
    // If the wall is on the bottom
    else if (WallOnBottom)
    {
      // Place us below the wall and stop
      This.Sprite.Position.Y = Wall.Position.Y - This.Sprite.GetHeight() - 1;
      This.Sprite.Velocity.Y = 0;
    }
  }
}
Personal tools
Namespaces
Variants
Actions
Navigation
Engineering
Game Design
Multimedia Production
VG Programming
Downloads
Toolbox