0
votes

I've been working on my rotation code for around 3 hours now and there's still something wrong with it but I'm unable to localise the issue... What I'm trying to achieve is rotating an image around its own origin about an angle determined by the mouse location and object location. Visually, I want its left bottom vertex to stay in 1 place all the time while applying the angle.

This is how I calculate the rotation:

                    //object.pos_x and object.pos_y are coordinates of left bottom vertex
                    double height = mouse_y - object.pos_y;
                    double width = mouse_x - object.pos_x;
                    double tg = height / width;
                    double angle = 0;
                    double starting_angle = 0; // the quarter of rotation
                    double fix = 0; //this helps in calculating the angle from the origin of quarter to the (mouse_x, mouse_y) point

                    if(width < 0 && height >= 0)
                    {
                        starting_angle = -90;
                        fix = -90;
                    }
                    else if(width < 0 && height < 0)
                        starting_angle = -180;
                    else if(width >= 0 && height < 0)
                    {
                        starting_angle = -270;
                        fix = -90;
                    }

                    angle = starting_angle * PI / 180 + (fix)*PI/180 - (-atan(tg));
                    object.angle = angle;
                    object.pivotPointX = object.constframeWidth/2;
                    object.pivotPointY = object.constframeHeight/2;

and here my actual rotation:

//the base position of each object is such that the center of object is at the same place as point (0,0) in global coordinates
            RECT base;
            base.left = -object.constframeWidth/2;
            base.right = object.constframeWidth/2;
            base.top = object.constframeHeight/2;
            base.bottom = -object.constframeHeight/2;

            float x1 = object.base.left;
            float x2 = object.base.right;
            float y1 = object.base.bottom;
            float y2 = object.base.top;

            //These points will contain the vertices after applying rotation
            Point one = new PointF(x1, y2);
            Point two = new PointF(x1, y1);
            Point three = new PointF(x2, y1);
            Point four = new PointF(x2, y2);

            float s = (float) sin(angle);
            float c = (float) cos(angle);

            x1 += object.pivotPointX;
            y1 += object.pivotPointY;
            x1 += object.pivotPointX;
            y1 += object.pivotPointY;
            x2 += object.pivotPointX;
            y2 += object.pivotPointY;
            x2 += object.pivotPointX;
            y2 += object.pivotPointY;
            // Then we rotate each point
            one.x = x1 * c - y2 * s;
            one.y = x1 * s + y2 * c;
            two.x = x1 * c - y1 * s;
            two.y = x1 * s + y1 * c;
            three.x = x2 * c - y1 * s;
            three.y = x2 * s + y1 * c;
            four.x = x2 * c - y2 * s;
            four.y = x2 * s + y2 * c;

            object.pivotPointX*=-1;
            object.pivotPointY*=-1;
            one.x += object.pivotPointX;
            one.y += object.pivotPointY;
            two.x += object.pivotPointX;
            two.y += object.pivotPointY;
            three.x += object.pivotPointX;
            three.y += object.pivotPointY;
            four.x += object.pivotPointX;
            four.y += object.pivotPointY;
            object.pivotPointX*=-1;
            object.pivotPointY*=-1;

            // Finally we translate the sprite to its real correct position.
            one.x += object.translation.x;
            one.y += object.translation.y;
            two.x += object.translation.x;
            two.y += object.translation.y;
            three.x += object.translation.x;
            three.y += object.translation.y;
            four.x += object.translation.x;
            four.y += object.translation.y;

I wanted the object to be rotated around its left bottom vertex but it's rotated around some more distant point and the rotation looks really weird when the mouse coordinates are lower than sprite center global cooridnates. It's hard to precise its behaviour then. It is flickering simultaneously on 2 different positions until i stop moving the mouse...

I would be really grateful for any help in the code above or advice on how to do such a rotation better!

1

1 Answers

0
votes

Nevermind... I have made a really stupid mistake and couldn't find it so far... The problem was with

            x1 += object.pivotPointX;
            y1 += object.pivotPointY;
            x1 += object.pivotPointX;
            y1 += object.pivotPointY;
            x2 += object.pivotPointX;
            y2 += object.pivotPointY;
            x2 += object.pivotPointX;
            y2 += object.pivotPointY;

I guess it's my absentmindedness becouse each addition here should occur only once to each coordinate...