what I'm trying to do is make the paddle follow the mouse position(red dot).
I know how to rotate the paddle relatively to the center of the screen, but I'm not sure how to determinate the coordinates.
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
From what I understand, you want to have the paddle be at a point on the circle nearest the mouse cursor. Here's the general way of calculating that point:
angle = atan2(perpDot(v1, v2), dot(v1, v2)). Using atan2, we get an angle in the range (-pi, pi].
The simplest way would be to make your paddles origin at the center of the circle. Then you just need to calculate the angle and you're done (your paddle origin, or center position should be at the circle center and the paddle itself should be to the right of the circle as starting position).
Something like this:
// calculate delta from circle center to mouse position var dx:Number = mouseX - circleCenterX; var dy:Number = mouseY - circleCenterY; // get the rotation (in radians) var radians:Number = Math.atan2(dy, dx); // then we can simply apply the rotation to the paddle, // but we have to convert to degrees first paddle.rotation = radians * 180 / Math.PI;
If you want to have the paddle origin at the center of the paddle itself, then you can do the following to get a point that's always on the circle (the code to obtain the rotation will be the same as above):
// using the delta variables from the code snippet above var point:Point = new Point(dx, dy); // normalizing the vector and scaling it to the circle radius will give us // a point lying on the circle point.normalize(circleRadius); // add the circle center to the point to get "world coordinates" point.x += circleCenterX; point.y += circleCenterY;