Coder Social home page Coder Social logo

axelei / apos.spatial Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apostolique/apos.spatial

0.0 0.0 0.0 62 KB

Spatial partitioning library for MonoGame.

Home Page: https://apostolique.github.io/Apos.Spatial/

License: MIT License

C# 100.00%

apos.spatial's Introduction

Apos.Spatial

Spatial partitioning library for MonoGame.

Thanks!

Special thanks to RandyGaul for providing me the AABBTree algorithm.

Documentation

Coming soon!

Build

NuGet NuGet

Features

  • Fast spatial partition algorithm. Great for 2D culling.

Usage samples

First of all, your entities must implement the IEqualityComparer' interface. So if your entity class is Thing` you will have:

public class Thing : IEqualityComparer<Thing>

Your entities should have an identifier, int Id (or any class that you can do HashCode) and another integer, int Leaf. It will explained later on. Then implement the methods in the interface. You will end up with something in the likes of: (note that I use a Guid for the Id, it can be an integer or other hashable class)

    public int Leaf;

    public bool Equals(Thing x, Thing y)
    {
        if (x == null && y == null) return true;
        if (x == null || y == null) return false;
        return x.Id.Equals(y.Id);
    }

    public int GetHashCode(Thing obj)
    {
        return Id.GetHashCode();
    }

We are redy to use our entity in our game loop. The most important thing we need to declare is our AABBTree collection in which we will store our entities. Like this:

private readonly AABBTree<Thing> _things = new (1024, 0, 1024);

The parameters are optional and are only necessary if you want to do optimizations. First parameter is initial capacity, and it is described in the source code as the amount of nodes the tree can hold before it needs to be resized. Second is the expand constant, which expands the items that are added so that they don't need to be updated as often. Defaults to 2f, but you can be fine with 0 if you don't want to optimize this. Last one is the move constant, which expands the items that are moved so that they don't need to be updated as much. Defaults to 4f.

Now we can add the entities to our aabb tree, like for instance an enemy:

enemy.Leaf = _things.Add(enemy.HitBox, enemy);

First parameter is the enemy hit box, second the enemy entity itself. Note that it returns a leaf, which is the identifier within the aabb tree. We need to save it when we want to refer to the entity inside the aabb tree.

We need to update the entities inside the aabb tree (unless they have not moved). We do it like so:

_things.Update(Player.Leaf, Player.HitBox);

In this case we are updating the player entity. The first parameter in the Update is the entity leaf, which we stored when inserting. Second is the hit box.

We are ready now to be able to calculate collisions. This is as simple as: (for our Thing thing)

        foreach (Thing otherThing in _things.Query(thing.HitBox)) // We query the aabb tree instead of iterating it
        {
            if (otherThing.Id == thing.Id) continue; // We don't want an entity to collide with itself
            // Collision logic takes place here.
        }

Other projects you might like

apos.spatial's People

Contributors

apostolique avatar axelei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.