Jump to content

What do toWorldFrame and toLocalFrame methods do?!


jloa
 Share

Recommended Posts

It's even kinda funny to ask such a thing, but...
Phaser.Physics.P2.Body class has two useful methods to convert points to local/global metrics (docs).
For instance it's said

[i][b]toWorldFrame(out, localPoint)[/b][/i]/* Transform a local point to world frame. */// out:Array - The vector to store the result in.// localPoint:Array - The input local vector.

Ok, fine. I've created a new body with a (0.5,0.5) anchor, moved it to 100:100 px, then i try to find out what will be the (0,0) local body point in global coordinates - obviously the point is 100:100.
 
Let's check:

// create a spritevar s = game.add.sprite(0, 0, '');// position at 100:100s.x = s.y = 100;// enable p2 to create the bodygame.physics.p2.enable(s, true);// now try to find out the local 0,0 in world metrics// holds the global point (dunno why, but it's an array rather than Point)var v = [];// relative 0:0 point in global coordinates -- should be 100:100s.body.toWorldFrame(v, [0,0]);// traceconsole.error('result', v);// wtf is this?! O_o// result [-5, -5]

 
Same with the toLocalFrame method.
 
So:
- what do these methods calculate?! O.o
- are there properly working localToGlobal(p:Point) and globalToLocal(p:Point) methods in phaser or i should implement them myself?

Link to comment
Share on other sites

Pixi has just implemented those helpers, but for now an object's world position can be found by checking its world.x and world.y properties. Be advised however that these coordinates are updated during preUpdate, so if you move an object and then try to query its world position straight away, it will not be correct until either the next update call, or by calling sprite.preUpdate() manually first.

 

The code that determines the world position is here:

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

So you may be able to use that to get the values without calling preUpdate.

Link to comment
Share on other sites

p2 units are converted to phaser pixels by multiplying with -20. -5, -5 in p2 coordinates equals 100, 100 in pixels.

 

O.o so when i grab any number from p2 object like body i should multyply it by -20? Or ti's done automatically?

Link to comment
Share on other sites

Pixi has just implemented those helpers, but for now an object's world position can be found by checking its world.x and world.y properties. Be advised however that these coordinates are updated during preUpdate, so if you move an object and then try to query its world position straight away, it will not be correct until either the next update call, or by calling sprite.preUpdate() manually first.

 

The code that determines the world position is here:

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

So you may be able to use that to get the values without calling preUpdate.

 

Ye, thanks i know how to make globalToLocal/localToGlobal (btw parent might be not the top lvl) :)

But, still what do toWorldFrame / toLocalFrame methods do?

Link to comment
Share on other sites

O.o so when i grab any number from p2 object like body i should multyply it by -20? Or ti's done automatically?

 

If you grab it from the Phaser body, it's done automatically, if you go to the p2 body (phaserbody.data), it's all in p2 units. There are a couple of helper functions that make it a little easier: read the docs on InversePointProxy :)

Link to comment
Share on other sites

If you grab it from the Phaser body, it's done automatically, if you go to the p2 body (phaserbody.data), it's all in p2 units. There are a couple of helper functions that make it a little easier: read the docs on InversePointProxy :)

 

Ye, that's what i thought until i dived into the source of phaser. For example the p2.Body.velocity property. They made a proxy point class in order to manage the metrics convertation which is nice, but it works only one-way.

See the source http://docs.phaser.io/InversePointProxy.js.html#sunlight-1-line-25

 

So basically as you work with pixels in phaser, when you set velocity.x = some_value it's converted to meters inverted (why did they invert the values??), but if you get the velocity.x it won't be converted back from inverted meters to pixels. I had to override this class' getters, to make it work properly.

// @FIX convert back to pixelsget: function (){    return this.world.mpx(this.destination[0]);},

Also the forces are applied inverted. Which is also weird. If i apply a [10, 0] force to a object i expect the object to move to right, but not in phaser :-)

http://docs.phaser.io/Body.js__.html#sunlight-1-line-419

 

I don't understand why they used pxmi() method to invert the values. Why? It's obvious that the force would move the object in the same direction, not the opposite.

I've already overriden this method too with pxm() - now works properly. Lots of work to do with overriding still to go... :-(

 

UPD: btw my game physics are finally working after my InversePointProxy getters patch. Spent 3 days trying to figure out what was wrong until i decided to look at the phaser's source.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...