Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

world property not accurate on children of children and affected by position of camera #2694

Closed
ForgeableSum opened this issue Aug 17, 2016 · 3 comments

Comments

@ForgeableSum
Copy link

ForgeableSum commented Aug 17, 2016

  • A bug in the API

When you have a sprite child of another sprite, child of another sprite child of a group, child of the world, the world property no longer calculates the correct x/y.

It may not work in different hierarchies as well but this hierarchy is used commonly in my game, hence I've done a lot of testing with it. I have a muzzle flash child of a gun sprite which is the child of a unit which is the child of a group which is the child of the world. That's:

sprite (muzzleflash) > sprite (gun) > sprite (unit) > group > world.

The muzzle flash rotates to align with the gun and the gun rotates independently of the unit. The unit rotates relative to the group (which has no transforms on it) which is relative to the world. As the gun fires, I need to calculate the start x/y position (relative to the world) so I know where to start the bullet tween. Should be easy right? Just use the .world property on the muzzle flash (since the muzzle flash is right on top of where the bullet starts). Unfortunately, that doesn't work. It is affected by odd things, like movement of the camera and the world x/y never lands exactly on where the muzzle flash is. rotations might have something to do with it as well (gun is rotate, unit is rotated, etc). If I move the camera the start position of the bullet calculated literally moves to a point that seems relative to the camera. It's bizarre! Anyway, I've been trying to find workaround of this issue for the last two days, and keep going back and fourth. I've concluded there is something wrong going on inside Phaser when calculating the world x/y positions. Would appreciate it if someone could look into it.

Tested in 2.4.7, 2.6.0 and 2.6.1.

P.S. I have also tried adding the x/y of all the parent sprites to get the position relative to the world. While those results aren't affected by camera (like world.x/y), I don't think they take into account rotation as the bullets start far from the muzzle flash. If someone knows a formula that can calculate absolute x/y, taking rotation of children and parents into account, that would be immensely helpful.

@photonstorm
Copy link
Collaborator

It depends what value you want. worldPosition, worldScale and worldRotation are all updated each frame, in the updateTransform method - until that point they will be either not set, or inaccurate (depending on how old they are).

If they are deeply nested then it needs a full scene graph update before the values are correct (i.e. you need to call game.stage.updateTransform() before checking any of those properties).

If you don't believe those properties contain the correct values then I need a code sample demonstrating it please.

@ForgeableSum
Copy link
Author

Ugh this is so frustrating. A whole year later and I am still plagued by this issue.

sprite.worldPosition and sprite.world - appear to give you different coordinates but neither make any sense. They seem to be tied to the camera somehow because when you move the camera around, it alters the results.

All I want is to figure out a child's position in the world - not it's position relative to its parent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants