所以,这里是我的2D点类的代码旋转:雪碧旋转偏移不会保留在它所属的位置。 (SDL)
float nx = (x * cos(angle)) - (y * sin(angle));
float ny = (y * cos(angle)) + (x * sin(angle));
x = nx;
y = ny;
x和y是在点类的局部变量。
这里是我的精灵类的旋转代码:
//Make clip
SDL_Rect clip;
clip.w = width;
clip.h = height;
clip.x = (width * _frameX) + (sep * (_frameX) + osX);
clip.y = (height * _frameY) + (sep * (_frameY) + osY);
//Make a rotated image
col bgColor = image->format->colorkey;
//Surfaces
img *toEdit = newImage(clip.w, clip.h);
img *toDraw = 0;
//Copy the source into the workspace
drawRect(0, 0, toEdit->w, toEdit->h, toEdit, bgColor);
drawImage(0, 0, image, toEdit, &clip);
//Edit the image
toDraw = SPG_Transform(toEdit, bgColor, angle, xScale, yScale, SPG_NONE);
SDL_SetColorKey(toDraw, SDL_SRCCOLORKEY, bgColor);
//Find new origin and offset by pivot
2DVec *pivot = new xyVec(pvX, pvY);
pivot->rotate(angle);
//Draw and remove the finished image
drawImage(_x - pivot->x - (toDraw->w/2), _y - pivot->y - (toDraw->h/2), toDraw, _destination);
//Delete stuff
deleteImage(toEdit);
delete pivot;
deleteImage(toDraw);
代码使用精灵的起源中心。如果我将枢轴放在(0,0)处,它可以正常工作,但如果我将它移动到其他地方,例如角色的肩膀,它会开始让Sprite像Spirograph一样旋转,而不是在枢轴上停留在人物的肩膀。
图像旋转函数来自SPriG,它是一个用于在SDL中绘制图元和变换图像的库。由于枢轴来自图像的中心,我认为由旋转产生的修剪表面的新尺寸应该不重要。
[编辑] 我弄乱了代码。通过减慢速度,我发现,由于某种原因,矢量旋转的速度比图像快60倍,尽管我没有将任何东西乘以60.因此,我试图将输入除以60,直到现在,它是出现所有生涩,不旋转之间的任何60倍数之间的任何东西。
我发现在这个网站上的矢量旋转代码,人们一再证实它的工作原理,为什么它只是以60为增量旋转?