All Projects → dotnet-ad → Humper

dotnet-ad / Humper

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

Projects that are alternatives of or similar to Humper

Pioneer
A game of lonely space adventure
Stars: ✭ 979 (+1241.1%)
Mutual labels:  game, physics
Classic Pool Game
Classic 8 Ball pool game written in JavaScript
Stars: ✭ 177 (+142.47%)
Mutual labels:  game, physics
Buoyancysystem
A system for buoyancy and boat physics in Unreal Engine 4.
Stars: ✭ 87 (+19.18%)
Mutual labels:  game, physics
Quantum Game
Quantum Game (old version) - a puzzle game with real quantum mechanics in a browser
Stars: ✭ 294 (+302.74%)
Mutual labels:  game, physics
Bounce
Bounce is a 3D physics engine for games.
Stars: ✭ 300 (+310.96%)
Mutual labels:  game, physics
Glportal
🎮 Open Source teleportation based first person puzzle-platformer
Stars: ✭ 297 (+306.85%)
Mutual labels:  game, physics
Dustracing2d
Dust Racing 2D is a traditional top-down car racing game including a level editor.
Stars: ✭ 199 (+172.6%)
Mutual labels:  game, physics
Stuntrally
The main repository containing Stunt Rally sources and game data. A 3D racing game based on VDrift and OGRE with track editor.
Stars: ✭ 314 (+330.14%)
Mutual labels:  game, physics
Game Dogfight
Air to air combat game, created in Python 3 using HARFANG 3D.
Stars: ✭ 41 (-43.84%)
Mutual labels:  game, physics
Sisl
Scientific Python toolbox for large scale tight-binding and electronic structure calculations (DFT and NEGF analysis)
Stars: ✭ 67 (-8.22%)
Mutual labels:  physics
Hellspawner
The OpenDiablo2 toolset
Stars: ✭ 69 (-5.48%)
Mutual labels:  game
Java Mvc Swing Monopoly
模仿大富翁游戏,使用Java Swing (GUI) 实现的单机游戏,遵循MVC设计模式。Created in Java. Using GUI developed with Swing, with a MVC design pattern.
Stars: ✭ 67 (-8.22%)
Mutual labels:  game
Pharaohstroy
A maplestory IDE which can develop the multi-platform maplestory game
Stars: ✭ 69 (-5.48%)
Mutual labels:  game
Starlight
spaceships in space
Stars: ✭ 71 (-2.74%)
Mutual labels:  game
Super mario sunshine uhd texture pack
Upscales and retextures for Super Mario Sunshine.
Stars: ✭ 68 (-6.85%)
Mutual labels:  game
Engine
C++11 and SDL2 lightweight 2d game framework
Stars: ✭ 71 (-2.74%)
Mutual labels:  game
Spirit
Atomistic Spin Simulation Framework
Stars: ✭ 67 (-8.22%)
Mutual labels:  physics
Genesis
🤖 Warframe Discord Cephalon
Stars: ✭ 67 (-8.22%)
Mutual labels:  game
Poco
A cross-engine test automation framework based on UI inspection
Stars: ✭ 1,177 (+1512.33%)
Mutual labels:  game
Minesweeper Rs
Simple minesweeper in Rust
Stars: ✭ 71 (-2.74%)
Mutual labels:  game

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].