• Stars
    star
    101
  • Rank 336,675 (Top 7 %)
  • Language
    C#
  • License
    MIT License
  • Created about 8 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Collision detection for Axis-Aligned-Bounding-Boxes (aka AABB) in C#.

Humper

Collision detection only for Axis-Aligned-Bounding-Boxes (aka AABB).

This library isn't a fully featured physics engine : don't expect a realistic physics simulation but it could be enough for basic game physics (platformer games, top-down games, shoot-em-ups). Though, Humper has spatial hashing for better performances and all the necessary moving and collision test tools needed for building a 2D game.

The library doesn't rely on any particular framework, its pure C# with all needed types included. It is fairly easy to integrate with existing frameworks like Monogame.

Install

Available on NuGet

NuGet

Quickstart

Schema

var world = new World(500, 300);

// Create a box of size (20,20) at position (100,100)
var body = world.Create(100, 100, 20, 20);

// Create a second box
world.Create(50, 150, 100, 100);

// Try to move the box to (100,200) with a slide movement for every other collided body of the world
var result = body.Move(170,200, (collision) => CollisionResponses.Slide);

// React to collisions
if(result.HasCollided)
{
	Debug.WriteLine("Body collided!");
}

Basic APIs

IWorld

The world is a virtual representation of your physics environnement. It manages all bodies and have a given size.

The world is subdivided in cells of 64 by default to faster collision calculation, but you can also change this parameter at instanciation.

IBox Create(float x, float y, float width, float height)

Create a new box in the world.

bool Remove(IBox box)

Remove the specified box from the world.

IBox Find(float x, float y, float width, float height)

Find the boxes contained in the given area of the world.

IHit Hit(Vector2 point, IEnumerable<IBox> ignoring = null)

Queries the world to find the nearest colliding point from a given position.

IHit Hit(Vector2 origin, Vector2 destination, IEnumerable<IBox> ignoring = null)

Queries the world to find the nearest colliding position from an oriented segment.

IHit Hit(RectangleF origin, RectangleF destination, IEnumerable<IBox> ignoring = null)

Queries the world to find the nearest colliding position from a moving rectangle.

IBox

A box represents any object of your physical world as an Axis-Aligned-Bounding-Boxes (a rectangle that cannot be rotated).

float X { get; }

The top left corner X coordinate of the box.

float Y { get; }

The top left corner Y coordinate of the box.

float Height { get; }

The height of the box.

float Width { get; }

The width of the box.

IMovement Move(float x, float y, Func<ICollision, CollisionResponses> filter)

Triggers a movement of the box in the physical world from its current position to the given one. The filters should indicate how the box reacts when colliding with another box of the world (see Responses section for more info).

IMovement Simulate(float x, float y, Func<ICollision, ICollisionResponse> filter)

Simulates the move of the box to the specified coordinates with collisition simulation (the boxe's position isn't altered at all).

IBox AddTags(params Enum[] newTags)

Add enumeration flags to the box.

bool HasTag(params Enum[] values)

Indicates whether the box has at least one of the given tags.

bool HasTags(params Enum[] values)

Indicates whether the box has all of the given tags.

object Data { get; set; }

Custom user data that can be attached to the box.

ICollision

A collision represents the result of a movement query that resulted in a collision and that need to be resolved to a response.

IBox Box { get; }

The box that moved.

IBox Other { get; }

The other box than being collided by the moving box.

RectangleF Origin { get; }

The starting position of the moving box.

RectangleF Goal { get; }

The intialy requested goal destination for the moving box.

Hit Hit { get; }

Gets information about the impact point.

IHit

An hit point represents the impact with a box of the world.

IBox Box { get; }

Gets the collided box.

Vector2 Normal { get; set; }

The normal vector of the collided box side.

float Amount { get; set; }

The amount of movement needed from origin to get the impact position.

RectangleF Position { get; set; }

The impact position.

float Remaining { get; }

The amount of movement needed from impact position to get the requested initial goal position.

Responses

When moving a box, a response should be returned through a filter to indicate how this box should react to a collision with another box.

Several CollisionResponses are included :

None

Schema

All collisions are ignored.

Touch

Schema

The box moves to the collision impact position.

Cross

Schema

The box moves through and ignore collision.

Slide

Schema

The box slides on the collided side of the other box.

Bounce

Schema

The box is reflected from the side of the other box.

Custom

A custom implementation of ICollisionResponse can also be provided if needed.

Debug layer

A debug layer is provided if you want to draw the boxes : you only have to provide basic drawing functions to the world DrawDebug method.

An example Monogame implementation :

private void DrawCell(int x, int y, int w, int h, float alpha)
{
	spriteBatch.Draw(pixelTexture, pixelTexture.Bounds, new Rectangle(x,y,w,h), new Color(Color.White,alpha));
}

private void DrawBox(IBox box)
{
	spriteBatch.Draw(pixelTexture, pixelTexture.Bounds, box.Bounds.ToRectangle(), Color.Green);
}

private void DrawString(string message, int x, int y, float alpha)
{
	var size = this.font.MeasureString(message);
	spriteBatch.DrawString(this.font, message, new Vector2( x - size.X / 2, y - size.Y / 2), new Color(Color.White, alpha));
}
protected override void Draw(GameTime gameTime)
{
	graphics.GraphicsDevice.Clear(Color.Black);
	spriteBatch.Begin(blendState: BlendState.NonPremultiplied);
	var b = world.Bounds;
	world.DrawDebug((int)b.X, (int)b.Y, (int)b.Width, (int)b.Height, DrawCell, DrawBox, DrawString);
	spriteBatch.End();
	base.Draw(gameTime);
}

Samples

Video

Check the samples if you wish to implement a :

  • Top-down
  • Platformer

Ideas / Roadmap

  • Add resizing
  • Improve documentation
  • Optimize code

Thanks

Contributions

Contributions are welcome! If you find a bug please report it and if you want a feature please report it.

If you want to contribute code please file an issue and create a branch off of the current dev branch and file a pull request.

License

MIT © Aloïs Deniel

More Repositories

1

MarkdownView

Native markdown rendering on top of Xamarin.Forms & Markdig.
C#
125
star
2

Xamarin.Animations

Animate your views with simple shareable animation declarations.
C#
101
star
3

Microcharts.Samples

A set of sample applications that use Microcharts.
C#
91
star
4

Comora

A simple 2D camera for Monogame.
C#
83
star
5

SkiaSharp.Components

Rendered components for an easier declaration of SkiaSharp rendering.
C#
70
star
6

Wires

Light binding library for Xamarin
C#
32
star
7

Spritesheet

Simple helper for creating sprite based animations for Monogame.
C#
32
star
8

Transform

Base Monogame objects for managing relative transforms.
C#
23
star
9

Mvvmicro

Minimalist MVVM framework for .NET.
C#
22
star
10

Standard.SpecialFolders

Just listing all .NETStandard special folder paths.
C#
16
star
11

Xam.Forms.QRCode

A QRCode renderer based on SkiaSharp.
C#
16
star
12

Assetxport

Resize UWP, Xamarin.Android and Xamarin.iOS assets automatically.
C#
15
star
13

PagerViewController

Tabs at the top of the screen for Xamarin.iOS.
C#
14
star
14

Faker.Portable

C# faked data generation for testing and prototyping purpose.
C#
13
star
15

AutoFindViews

Have you ever counted the number of times your wrote 'FindViewById' in your Xamarin.Android project?
C#
13
star
16

Yoga.Parser

Declare your Yoga layouts in XML or JSON from .NET.
C#
11
star
17

Xam.Forms.NavigationExtensions

Helpers for Xamarin.Forms application navigation system : passing arguments, restoration.
C#
10
star
18

GridView

A Xamarin helper layout view that uses C# operator override.
C#
10
star
19

CodeBuilder

A set of helper classes for generating code.
C#
9
star
20

Profiler

A small in-app profiler for Xamarin and UWP applications.
C#
8
star
21

Xamarin.Plugins

Cross platform xamarin and windows plugins for PCLs.
C#
8
star
22

Transmute

Basic data conversion for .NET.
C#
7
star
23

Orkester

Lightweight framework for common centralized synchronization scenarios.
C#
5
star
24

Meetup.Xamarin.France.Demo

A sample base for the various conferences presented at Xamarin Meetups in France.
C#
4
star
25

StaticBind

Generated and compiled data binding for .NET (Xamarin.iOS, Xamarin.Android,...)
C#
3
star
26

Micon

Small application for generating mobile application default icons.
C#
2
star
27

hml

Hierarchy Markup Language, or less verbose xml.
C#
2
star
28

Mwm

MVVM & XAML for the web
C#
2
star
29

Mobile.Prerelease

Prepare your mobile release (BundleId, Name, Version, ...) from a simple CLI.
C#
2
star
30

CommonExtensions

All the common C# objects extensions methods I often use.
C#
1
star
31

Xam.NibLocalizers

Localize your storyboards and xibs automatically from Resx or LocalizedStrings.
C#
1
star
32

Tween

A little tweening library for .NET.
C#
1
star