cykod / Quintus

HTML5 Game Engine
http://html5quintus.com
GNU General Public License v2.0
1.41k stars 401 forks source link

viewport.follow does not center on followed object #139

Closed mrpetrocket closed 9 years ago

mrpetrocket commented 9 years ago

From the documentation found at http://www.html5quintus.com/guide/2d.md, I gather that the code below should create a viewport that centers on aSprite.

stage.add("viewport")
       .follow(aSprite);

In practice, the viewport seems to follow the selected sprite, but is not quite centered on said sprite. In \lib\quintus_2d.js, it appears that the viewport is centered on the bottom-right corner of the sprite. The following code comes from line 47 of quintus_2d.js:

      this[first === true ? 'centerOn' : 'softCenterOn'](
                    followX ?
                      this.following.p.x + this.following.p.w/2 - this.offsetX :
                      undefined,
                    followY ?
                     this.following.p.y + this.following.p.h/2 - this.offsetY :
                     undefined
                  );

According to the documentation at http://www.html5quintus.com/guide/sprites.md, the p.x and p.y properties are equal to the center of a sprite object. Adding half the sprite width and height will push the viewport to center on the bottom-right corner.

I see three possibilities: (1) I am stupid and missing something obvious. (2) This is intentional behavior and should not be changed. Changing the offsetX and offsetY properties of the viewport can be used as a workaround. (3) This is a real issue and can be fixed by removing the sprite width and height from the follow calculations.

Any thoughts?

cykod commented 9 years ago

This is legacy and just wasn't updated when Quintus switched to a full scene graph - it should be changed just p.x, p.y

mrpetrocket commented 9 years ago

Made change and created pull request.