Jump to content

Detecting a sprite being dropped on a polygon


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.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


  • Recently Browsing   0 members

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