Jump to content

Detecting a sprite being dropped on a polygon


iambaz
 Share

Recommended Posts

I am making a wheel of fortune type game. The (static) wheel has 6 triangular hotspots which the user drags sprites on to. I need to detect which hotspot on the wheel the sprite has been dropped on. 

I create the hotspots like so:

    for(i = 0; i < 6; i++){

        poly = new Phaser.Polygon([ new Phaser.Point(0, 0), new Phaser.Point(360, 0), new Phaser.Point(230, 230), new Phaser.Point(130, 230), new Phaser.Point(0, 0) ]);

        graphics = game.add.graphics(0, 0);

        graphics.beginFill(0xFF33ff);
        graphics.drawPolygon(poly.points);
        graphics.endFill();
        graphics.angle = i * 60;
        graphics.pivot.y = 320;
        graphics.pivot.x = 180;

    }

So this gives me six nice triangle type panels in a circle next to each other. I hoped now I could use the poly.contains() method to work out if the coordinates of the mouse are within the poly boundaries at when I stop dragging the sprite, however, it only detects the first (un-rotated) triangle and none of the others in the loop.

Obviously the problem is that the poly.contains() method is looking at the original polygon coordinates and not the transformed coordinates within the shape we created with them. I hoped I would easily be able to rotate the Polygon object but this is not possible. 

I looked into using arcade physics but that does not support polygon boundaries. I tried P2JS physics, but it seemed completely over the top for what I need and I still had the same issues where by even when I added rotation to physics bodies, it was still not transforming the original Polygon object that I call .contains() on. 

Any advice?

Link to comment
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...
 Share

  • Recently Browsing   0 members

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