Is it possible to define more accurate custom shape collision box rather than rectangle for an Entity?

StackOverflow https://stackoverflow.com/questions/21727300

  •  10-10-2022
  •  | 
  •  

سؤال

Is it possible to define more accurate custom shape collision box rather than rectangle for an Entity? Please let me know if it is possible and how?

هل كانت مفيدة؟

المحلول

Of course it is possible, and there are multiple possible solutions:

However, Box2D is a bit heavy if you only want to do some custom shape collision.

  • Custom Entity Collision Check: ImpactJS is a great game engine, it lets you easily extend any of its modules, and for custom entity collision check there are two possible methods:
    • extend the ig.Game.checkEntities, this allows you to loop through all entities in the game and check their collisions the way you want to.
    • extend the ig.Entity.check, for lazy people like myself, and only when the custom shape is contained within the entity's rectangle, you can do the entity shape collision check within this function (after entity rectangle collision has already occurred).

ig.Entity.check example:

MyEntity = ig.Entity.extend({
    customShape: 'circle',
    // custom shape definition...
    customShapeProperties: {radius: 0},
    check: function(other) {
        // custom shape collision check...
        if (...) {
           this.customCheck(other);
        }
    },
    customCheck: function(body) {}
});
// now if all your entities inherit MyEntity,
// they will have customCheck called only when the custom shape collision occur.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top