Basic Collision

One of the basic building blocks of any game is collision detection and response.  Otter provides a bunch of basic Colliders to use in your game: BoxCollider, CircleCollider, LineCollider, PixelCollider, PointCollider, PolygonCollider, and GridCollider.  This basic example will use BoxCollider and CircleCollider.

Colliders can be added to Entities much like Graphics. During the game's Update a collider can be used to check for collisions with other colliders, or just simply check for an overlap with other colliders.

Colliders mostly are to be used with Tags.  A tag can be an int or an enum.  For example you can tag a bullet type object with "1" or "Tags.Bullet" and then other Colliders can check for collisions with the tag "1" or the tag "Tags.Bullet."  You can also check for collisions and overlaps with specific Entities if that fits your needs better.

Here's some example code to check out featuring a simple player object that can grab collectables in a Scene.

using Otter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicCollision {
  class Program {
    static void Main(string[] args) {
      // Create a Game.
      var game = new Game("Basic Collision");

      // Create a Scene.
      var scene = new Scene();
      // Add the Player to the Scene at the center.
      scene.Add(new Player(game.HalfWidth, game.HalfHeight));

      // Add 50 collectables in random locations to the Scene.
      for (int i = 0; i < 50; i++) {
        // Check out the Rand class for random generation!
        var x = Rand.Float(game.Width);
        var y = Rand.Float(game.Height);
        // Add the Collectable at the randomized position.
        scene.Add(new Collectable(x, y));
      }

      // Start the Game.
      game.Start(scene);
    }
  }

  // The collision tags that will be used.
  enum Tags {
    Player,
    Collectable
  }

  class Player : Entity {
    // Keep track of how many Collectables are picked up by the Player.
    public static int Score = 0;

    // Create a simple white rectangle to use for the image.
    Image image = Image.CreateRectangle(30);
    // Create a BoxCollider to pick up collectables with (and give it the Player tag)
    BoxCollider collider = new BoxCollider(30, 30, Tags.Player);

    public Player(float x, float y) : base(x, y) {
      // Add the image for rendering.
      AddGraphic(image);
      // Center the origin of the image.
      image.CenterOrigin();

      // Add the collider. Must be added or it cant check for collision!
      AddCollider(collider);
      // Center the origin of the collider so it aligns with the image.
      collider.CenterOrigin();
    }

    public override void Update() {
      base.Update();

      // Basic movement with WASD.
      if (Input.KeyDown(Key.W)) {
        Y -= 3;
      }
      if (Input.KeyDown(Key.S)) {
        Y += 3;
      }
      if (Input.KeyDown(Key.A)) {
        X -= 3;
      }
      if (Input.KeyDown(Key.D)) {
        X += 3;
      }
    }

    public override void Render() {
      base.Render();

      // Uncomment the following line to see the collider.
      //collider.Render();
    }
  }

  class Collectable : Entity {

    // Create a basic yellow circle.
    Image image = Image.CreateCircle(10, Color.Yellow);
    // Create a CircleCollider and tag it with the Collectable tag.
    CircleCollider collider = new CircleCollider(10, Tags.Collectable);

    public Collectable(float x, float y) : base(x, y) {
      // Add the image for rendering.
      AddGraphic(image);
      // Center the origin of the image.
      image.CenterOrigin();

      // Add the collider so that it can check for collisions.
      AddCollider(collider);
      // Center the origin of the collider so it aligns with the image.
      collider.CenterOrigin();
    }

    public override void Update() {
      base.Update();

      // Use the collider to check for an overlap with anything tagged as a Player.
      if (collider.Overlap(X, Y, Tags.Player)) {
        // Remove itself from the scene when collected.
        RemoveSelf();
        // Increase the player score, yahoo!
        Player.Score++;
      }
    }

    public override void Render() {
      base.Render();

      // Uncomment the following line to see the collider.
      //collider.Render();
    }
  }
}

This example uses the enum Tags for checking with collisions.  You could also use ints, or check for specific Entities.  Keep in mind that a collider must be added to an Entity in order for it to properly function and be able to check for collisions.

Examples