alphard

Phaser world to screen (local to global)

Recommended Posts

Good day to all! 
 
I have a question on Phaser's world-to-screen transformations, and I've googled almost nothing on it :wacko:
Is there a way to transform a sprite's world coordinates into the screen space (and visa versa)? 
It is a very useful feature and I can't beleave nobody still interested in it!
 
I found poor documented Sprite.worldTransform.tx, Sprite.worldTransform.ty,
but I'm not sure these fields always work properly (to be honest, I am sure they don't).
 
Thank you!

 

 

Share this post


Link to post
Share on other sites

This post contains a fix to add it: http://www.html5gamedevs.com/topic/8360-sprite-collides-with-nested-groups/?p=50189 - you basically just add together the positions of all of an object's parents to get its global position.

 

I believe Phaser is holding off until the next version as pixi (its underlying rendering engine) has just had helpers for this functionality (global to local and local to global) added, so they'll likely be included in the next release.

Share this post


Link to post
Share on other sites

Every Phaser sprites and images have a world property that gives you access to the sprite's coordinates relative to the game canvas and not the parent container.

(sprite.world.x and sprite.world.y)

I'm not sure if this is what you're looking for ?

Share this post


Link to post
Share on other sites

This doesn't work as advertised Alvin - the world.x and world.y coordinates only check one level deep, so if your object is nested in a group within a group and both groups are positioned, the value is no longer correct. Here's the code from Sprite.prototype.preUpdate where it's calculated:

this.world.setTo(this.parent.position.x + this.position.x, this.parent.position.y + this.position.y);

Edit: Come to think of it, as the scene graph is processed top to bottom (i.e. parents then children) surely if this bit of code used this.parent.world.x and .y then it should fix it? Can anyone think as to a reason why this wouldn't work?

 

Edit2: I should really check my assumptions. It turns out world positioning is correct, however obviously only correct after preUpdate, so if you move a sprite then try to check its world position instantly, it won't be correct. http://jsfiddle.net/lewster32/ajqwb6se/

Edited by lewster32

Share this post


Link to post
Share on other sites

Does anyone know if this has changed at any time recently? 

 

I see PIXI has toGlobal and toLocal methods on the Stage class.  However, when I try to use these, I get a transform error. 

I am basically pretty deep inside a toolbar structure, and need to get the global position of a group. 

 

I rarely use Sprites, but mostly groups and images, I tried to hack it by putting an empty sprite inside my required group and then later calling "sprite.world.x" but it always returns zero (even after keeping in mind Lewster's response).

Cheers!

 

Edited:

 

So I ended up doing this for now.  I know at this point that I have a display list. In my case, I have stage > gameui > toolbarsystem > toolbar > toolbarButton > toolbarTooltip. and all of them have positions. 

 

As soon as I add the tooltip to the display list for the toolbarButton then I do:

var tx: number = 0;var ty: number = 0;var target: PIXI.DisplayObjectContainer = this.toolTip;while (target) {    tx += target.x;    ty += target.y;    target = target.parent;}
This seems to yield the correct world X/Y position but probably does not work for everyone.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.