2017-03-05 21 views
6

私は描く前に回転するイメージを持っています。画像は六角形の角度だけ回転します。換言すれば、画像は基本的に六角形の個々のエッジを「強調」する。この回転した画像の中でマウスがクリックされたかどうかを検出する必要があります。回転していない画像の中でマウスのクリックを検出するのは簡単ですが、回転した点でクリックを検出する方法はわかりません。回転後に画像のコーナーのポイントを取得する方法があるので、画像の上に不可視のポリゴンを置き、Polygon.contains()を使用できますか?Slick2Dで回転した画像のクリックを検出するにはどうすればよいですか?

    Image highlightEdge = new Image("assets/img/highlightEdge.png"); 
        if(angle == 90){ 
         highlightEdge.setCenterOfRotation(highlightEdge.getWidth(), 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(testPoint.x - 56, testPoint.y); 
        } else if(angle == 210) { 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x - 72, lastSettlement.y - 32); 
        } else if(angle == 330){ 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x - 8, lastSettlement.y - 32); 
        } else if(angle == 30){ 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x-8, lastSettlement.y); 
        } else if(angle == 150){ 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x-72, lastSettlement.y); 
        } else { 
         highlightEdge.setCenterOfRotation(0, 0); 
         highlightEdge.rotate(new Float(angle)); 
         highlightEdge.draw(lastSettlement.x-40, lastSettlement.y - 48); 
        } 

答えて

0

あなたは正確にImageの形状に合わせてShapeを作成し、マウスがクリックされた内部のかどうかを検出するために、その方法containsを使用することができます。

Imageのローテーションを考慮に入れて、対応するローテーションTransformShapeに適用することができます。

これを行う方法shapeFromImageを作成しました。それはImageとその位置を受信し、対応するShapeを返します。

float positionX; 
float positionY; 

if (angle == 90) { 
    highlightEdge.setCenterOfRotation(highlightEdge.getWidth(), 0); 
    highlightEdge.rotate(new Float(angle)); 

    positionX = testPoint.x - 56; 
    positionY = testPoint.y; 

    highlightEdge.draw(positionX, positionY); 
} 

... 

// you can now use this Shape to use its method "contains" 
imageShape = shapeFromImage(highlightEdge, positionX, positionY); 

/** 
* Returns the Shape of an Image considering its rotation 
* @param image 
* @param x the x position of the Image 
* @param y the y position of the Image 
*/ 
public static Shape shapeFromImage(Image image, float x, float y) { 

    // create a rectangle with same position and size of the image 
    Shape imageShape = new Rectangle(x, y, image.getWidth(), image.getHeight()); 

    // get the rotation angle of the image 
    float angle = image.getRotation(); 

    // if the image is rotated, we also need to rotate our shape 
    if (angle != 0.f) { 

     // convert the rotation angle in radians to use in Transform 
     float angleInRadians = (float) Math.toRadians(angle); 

     // get the point of rotation to use in Transform. 
     // image.getCenterOfRotation returns a point relative to the image. 
     // for Transform we need an absolute point, so we add the image position to it 
     float rotationX = image.getCenterOfRotationX() + x; 
     float rotationY = image.getCenterOfRotationY() + y; 

     // create the rotation Transform to match the image rotation 
     Transform rotationTransform = Transform.createRotateTransform(angleInRadians, rotationX, rotationY); 

     // apply the rotation Transform to our shape 
     imageShape = imageShape.transform(rotationTransform); 

    } 

    return imageShape; 
} 

あなたの例では、このようにそれを使用することができます

関連する問題