GaryS

Move a sprite around a circular path, only when pointer is on path

Recommended Posts

Hi all,

I'm having difficulty achieving what I'm sure should be a pretty simple input technique.

I want to drag a sprite around a circular path, with the mouse or touch (and eventually, keyboard & gamepad, but we'll get to that later) but only when the pointer is over the path itself.
So, I found an earlier forum post asking how to force the sprite to move around the circular path using sin and cos:

The various techniques listed in the post are working well, but I'm struggling with detecting that a pointer is within the path when pressed.
I've seen some examples that use pointerOver, and indeed I've had this working on a sprite - however that only seems to work with the mouse, not a touch pointer.
In any case, I found another post where Rich said pointerOver was an internal function and shouldn't be used... Is there anything I can use in conjuction with a mouseMoveCallback, that will detect whether a pointer (mouse/touch) is over a graphics object?

Here's what I've got so far:
http://phaser.io/sandbox/edit/WQVEpEOG

Any help, much apprecaited.

 

Share this post


Link to post
Share on other sites

I'm not sure about a collision detection function, but if you just want to know if a point (the mouse/finger position) is within a certain distance of the edge of a circle, you could use linear algebra to figure it out. If you're only dealing with circles, it will certainly be faster than using a less specific collision detection function. It would look something like this:
 

var dist = inputPosition.distance(circleCenterPosition);
var inputRange = 10; //ten pixels worth of distance from the nearest point on the circle
if (dist >= circleRadius - inputRange && dist <= circleRadius + inputRange)
{
    // inputPosition is within 10 pixels of the nearest point on the circle
    // Do the thing.
}

 

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.