# Help with angleToPointer

## Recommended Posts

Heya, im doing the angle to pointer thing, but i need to restrict the rotation of the sprite so that it can only go within specific degrees. I was hoping someone could help me out here?

I'd be greatful. And sorry if it's a very obvious thing.

##### Share on other sites

Quick google search will throw up about a million hits for finding the angle between two points, and a fair number for angles between vectors.

This gist seems to be about what you want.

All you need to do is transform your pointer from screen space into world space and you're all set, that's normally trivial.

##### Share on other sites

game.physics.arcade.angleBetween(player, game.input.activePointer); gives me a rather odd number ranging from about 3 to 0,111111. Is this because my pointer is in screen space?

##### Share on other sites

Probably radians? Try multiplying by 180/pi.

##### Share on other sites

Thank you both, this turned out nicely.

##### Share on other sites

Well, i've hit another snag. I've given my player sprite a child, this is the gun and it tracks the mouse. Now if i turn left, and use scale.x -1 to flip the sprite the gun gets inverted aswell.

I've got this so far

if (cursorPos > 140) {
arrow.rotation = game.physics.arcade.angleToPointer(player) - Math.PI; };

The gun is aimed to the left side but the rotation is inverted of the mouse position.

Hope anyone could point me in the right way again?

##### Share on other sites

I'm not an expert with Phaser or grouping but sounds like those elements should not be grouped together like that, or, more specifically, you'd want to only flip your player sprite and not the gun sprite attached to them.

Maybe you could group them so they are siblings and move their container around? That way they both share a common parent which tracks movement etc but you can only flip the actual player sprite inside when necessary. The gun would rotate as you currently are doing, presumably the gun flips too when the angle is within a certain range.

• ### Recently Browsing   0 members

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