2014-10-18 11 views
0

所以,这里是我的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为增量旋转?

回答

1

我很久没有碰到SPriG的来源,但我可以给你一些信息。

如果SPriG存在旋转中心问题,那么迁移到SDL_gpu可能会更快,更容易(我建议使用SDL 2.0)。这样你得到一个类似的API,但性能要好得多(它使用图形卡)。

我可以猜测,矢量不是旋转比图像快60倍,而更像是57倍更快!这是因为你用sin()和cos()来旋转矢量,它接受以弧度表示的值。图像正在以度数旋转一个角度。弧度转换为度数为180/pi,大约为57. SPriG可以使用度数或弧度,但默认使用度数。使用SPG_EnableRadians(1)切换该行为。或者,您可以通过将参数乘以sin()和cos()pi/180来坚持角度变量中的度数测量。