Gessle

How is game.physics.arcade.distanceBetween calculated?

Recommended Posts

I just cannot figure out is distanceBetween calculating from anchor point or upper left corner of a sprite?

Is there a easy way to calculate distance between a single point and a whole sprite, not anchor point nor upper left corner but any point of that sprite?

Share this post


Link to post
Share on other sites

Generally, the X/Y of the object is used for calculations...

Looking at the source code for this function, it is the X/Y of the object... Changing the anchor doesn't matter to the calculation at all...
https://github.com/photonstorm/phaser/blob/v2.6.2/src/physics/arcade/World.js#L1918

 

You can also use game.math.distance to get the distance between two points... It takes the individual coordinates instead of the whole object as arguments...
http://phaser.io/docs/2.6.2/Phaser.Math.html#distance

 

How do you mean "distance between a point and a whole sprite"..? Do you want a collection of distances for each pixel the sprite is taking up, or just the X/Y...?

Share this post


Link to post
Share on other sites

It calculates from obj.position, which is the anchor point. It will be the top left if the anchor is (0, 0).

For point-to-sprite-point calculations, you can use Phaser.Math#distance with x = sprite.left, sprite.centerX, sprite.right; y = sprite.top, sprite.centerY, sprite.bottom.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.