a mouse controlled paddle (semicircle)

Started by
1 comment, last by theydidntnameme 13 years, 7 months ago
hi everyone. im trying to make a breakout game in c++/SDL where the paddle is a semi-circle shape (rotating around the center of the screen), and its angle o facing is controlled by moving the mouse in the direction you want it to point to. this allows for 360 degrees of freedom. everything is setup so that every frame all I have to do is calculate the new angle that the paddle should point for that frame. my function to calculate the angle is failing miserably however :)

every frame I get the relative mouse position (x, y) that the mouse moved from the last frame, then I warp the mouse back to the center of the screen. I get the correct readings for relative mouse position every frame, and its not affected by warping the mouse back to the center.

so basically everything is in place, I have all my data (current paddle's angle, mouse x and y displacement) for each frame, now what I need to do is figure out how to calculate a new angle to move the paddle.

how can I do this? my idea is if you set your X axis to the line that is perpendicular to the paddle's current facing (and y axis parallel to the paddle's current facing angle), then the only mouse displacement that will be used for determing how far to rotate the paddle will be that movement which lies on the x axis. I hope that made sense. I spent all day yesterday trying to figure this out and am failing miserably :(. for starters I just tried to set the angle of the paddle each frame to arctan( relativeMouseY / relativeMouseX ) but that doesn't seem to work either, my paddle jumps all over the place.

any help is appreciated :)
Visit my website! donny.webfreehosting.net
Advertisement
Your new paddle angle is independent of anything you've done in previous frames, so don't try to shove "current facing angle" into your calculations. atan2(y/x) is the right approach, but it assumes that a rotation of 0 degrees will cause the paddle to face the positive x-axis, and that your rotation isn't backwards. You may need to add a multiple of 90 degrees and/or negate the angle to fix things if those assumptions don't hold. Oh, and make sure that you're using radians where you need radians and degrees where you need degrees. Finally, if you're only using mouse displacement over a single frame, you may run into aliasing effects; in that case, summing displacement over the previous 2-4 frames could give better results.
Quote:Your new paddle angle is independent of anything you've done in previous frames, so don't try to shove "current facing angle" into your calculations. atan2(y/x) is the right approach, but it assumes that a rotation of 0 degrees will cause the paddle to face the positive x-axis, and that your rotation isn't backwards. You may need to add a multiple of 90 degrees and/or negate the angle to fix things if those assumptions don't hold. Oh, and make sure that you're using radians where you need radians and degrees where you need degrees. Finally, if you're only using mouse displacement over a single frame, you may run into aliasing effects; in that case, summing displacement over the previous 2-4 frames could give better results.



Thanks for the reply. i was using atan all this time which requires y/x already computed (1 argument). i had no idea atan2 existed which is what I needed (giving the angle including negative values of x and y). i used atan2 and it returns angles in the 1st and 2nd quadrants from 0 to 180, and in the 3rd and 4th quadrant from 0 to -180. I fixed that by checking to see if y < 0, and if so, add 360 to the result. actually I should mention atan2 returns everything in radians, i converted this answer to degrees then added 360 to fix the 3rd and 4th quadrant results.

another thing that was messing me up is that im using the SDL coordinate system (positive y is going downward, negative y goes up) which was screwing with my calculations. ive fixed this and now its working perfectly. i actually planned from the start to have it set up so that 0 degrees is facing the positive X axis increasing counterclockwise. i have one image of the paddle at 0 degrees (its actually facing 45 degrees but ill fix the image) and when I load the program i used SDL_gfx to rotate it 360 times and store each of the rotations in an array of SDL_Surface *'s :).

so in short everything works now and I will look into smoothing the movement over multiple frames in the future. thanks for the help!

o and the code below for anyone interested. angle is what the paddle uses to draw the correct paddle image rotation. the image in the array I use is "paddles[angle]"

bool Paddle::DoMove(int x, int y){    double angleRad = 0;    double angleDeg = 0;        //the x and y passed are SDL coordinates. arctan2    //    uses cartesian coordinates;    y = y*-1;        //no mouse movement, do nothing    if (x == 0 && y == 0)        return false;    angleRad = atan2(y, x);    angleDeg = angleRad*180/PI;        //atan2 reports angles in 3rd and 4th quadrant as negative. fix by adding 360    if (y < 0)        angleDeg += 360;            angle = angleDeg;    return true;}
Visit my website! donny.webfreehosting.net

This topic is closed to new replies.

Advertisement