Skip to content

Entity.pos vs Body.offset #548

Description

@parasyte

I was trying to create a little test case for the recently reported world boundary collision issue on the forum. But I'm blocked on a different issue! I created a basic Entity with a default position, set the body velocity, and expected it to just work.

However, the renderable is drawn at position Entity.pos + Body.offset, which are both the same value (set to the initial entity position). This seems completely incorrect.

Here's the test: http://jsfiddle.net/Lth0bgdx/ Set a breakpoint on Entity.draw() in melonJS-1.1.0.js: 8945 to see it in action.

Activity

  1. added this to the 1.1.0 milestone on Aug 12, 2014
  2. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    @obiot Any ideas, here?

  3. obiot commented on Aug 12, 2014

    @obiot
    Member

    Yep, but let me arrive at the office first , as it too long from my mobile :)

    On 12 août 2014, at 09:01, Jay Oster notifications@github.com wrote:

    @obiot Any ideas, here?

    —
    Reply to this email directly or view it on GitHub.

  4. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    FYI, this is an issue directly related to:

    1. Not including a getShape function in the settings for the Entity constructor
    2. Not setting any specific body shape.

    So the default body shape isn't adequate.

  5. obiot commented on Aug 12, 2014

    @obiot
    Member

    so, let me clarify this thing :

  6. obiot commented on Aug 12, 2014

    @obiot
    Member

    once again, i'm not saying this is correct, and for sure needs improvements :P

  7. obiot commented on Aug 12, 2014

    @obiot
    Member

    the other issue is that me.Entity inherit from me.Renderable, it would be much more easier if entity was just a basic container for renderables and a physic body, but it would then break all the current entity rendering code...

  8. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    Isn't there also some duplication with Body inheriting from Rect, and yet that information is not used as the boundary rect?

  9. obiot commented on Aug 12, 2014

    @obiot
    Member

    indeed...... gosh maybe I kind of lost myself within my own code :(

  10. obiot commented on Aug 12, 2014

    @obiot
    Member

    i've been missing you Jason to help me proofing my code !

  11. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    body.offset is the minimum offset between body pos and all body shapes
    (https://github.com/melonjs/melonJS/blob/master/src/physics/body.js#L325)

    if no shape are defined for an entity, the entity default size (which is actually the renderable) is used to defined the body bounds : https://github.com/melonjs/melonJS/blob/master/src/entity/entity.js#L185

    This second line is actually a big problem, because it means the body.offset is no longer used for its intended purpose. In the second line, the body.offset duplicate's the entity's position within the world. If we're going to keep this, then it should instead be given the bounds of a new rectangle with position (offset from Entity) <0,0> and width/hight of the entity.

  12. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    Does it make sense to get rid of Entity entirely, and replace it with Container? I get the feeling that's kind of the direction we're going, but haven't yet got there.

  13. obiot commented on Aug 12, 2014

    @obiot
    Member

    I suppose so, but as you were saying I do not thing we are there yet... and Container is kind of a big object, is it not ?

    Else what you would then recommend we do to clean my mess, between :

    • entity.pos
    • entity.body.pos
    • entity.body.offset (offset between the body.pos and shapes, actually for now since we only have 1 shape this equal to body.shapes[].pos)
    • entity.body.shapes[].pos (relative to the entity)
  14. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    That's a lot of vectors... 😒

    What are we gaining from the Entity object, right now? It just seems to have a bunch of proxy methods for Body and Renderable.

    • Ideally, Body has a position, width, and height. (It does! It extends Rect) This rectangle defines its absolute bounds within the world.
    • Body also contains one or more shapes which give it substance to collide with other things. Right now it only supports a single shape. (That's fine) And as shapes are added, removed, or modified, the Body bounds should be changed. (I believe this is the case, so far? At least when shapes are added and removed.)
    • Renderable should be drawn relative to the body. E.g. Renderable.pos is an offset from Body.pos.
    • We should be able to use multiple Renderables, all drawn relative to the Body.

    I don't really see a use for Entity at all. Body should reference the Renderables, since they are to be drawn relative to the Body. Basically, ObjectEntity should have been renamed to Body. :)

  15. obiot commented on Aug 12, 2014

    @obiot
    Member

    I don't really see a use for Entity at all. Body should reference the Renderables, since they are to be drawn relative to the Body. Basically, ObjectEntity should have been renamed to Body. :)

    gosh you are killing me :):):)

    was the the idea behind having body as a "child" property of entity not was as well to lower the amount of properties for the entity object ?

    what about then redefining entity pos as I was suggesting before, so that it just point to body.pos ?

    defineProperty :
        Object.defineProperty(me.Entity.prototype, "pos", {
           get : function () {
                return this.body.pos;
            },
            configurable : true
        });

    not sure how it works though if we do entity.pos.x = 6;

    I guess that else a good old 'this.pos = this.body.pos` (after creating the body) works too ?

  16. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    I like that the intention was to limit the number of properties, but if they both have the same properties, it defeats the purpose. :)

    Anyway, I don't think changing the Entity.pos will address the issue as reported. Fixing it would entail changing this line: https://github.com/melonjs/melonJS/blob/master/src/entity/entity.js#L185 to:

    this.body.updateBounds(new me.Rect(0, 0, this.width, this.height));

    I know, creating an object to be garbage collected. But it fixes the issue with the multiplied draw positions for the default shape.

  17. obiot commented on Aug 12, 2014

    @obiot
    Member

    that's certainly a better way to manage it yes, and this is a one time only garbage collected object per entity creation if Tiled is not used, so I believe we can survive it :)

  18. added a commit that references this issue on Aug 12, 2014
  19. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    @obiot Done! And verified with: http://jsfiddle.net/Lth0bgdx/3/ (I love the melonjs-builds bucket!)

  20. obiot commented on Aug 12, 2014

    @obiot
    Member

    oh great, thank you Jason !

    as for entity.pos, do we keep like this for now ? we really need to clear this up as well, but maybe in 1.1.1? (that would be a nice version number btw)

  21. parasyte commented on Aug 12, 2014

    @parasyte
    CollaboratorAuthor

    I don't mind leaving it as is for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions