二维旋转问题 C++ DirectX
2D Rotation Issue C++ DirectX
所以我试图围绕 window 中的另一个点旋转一个点,用 DirectX 绘制它。我的问题是旋转的形状很奇怪:
我所做的只是围绕 window 的中心旋转一个点并在这些点之间画线。
vec2_t vecCenter1 { gui.iWindowSize[ 0 ] / 2.f, gui.iWindowSize[ 1 ] / 2.f };
for ( float i { 0.f }; i < 360.f; i += 2.f )
{
vec2_t vecLocation { vecCenter1.x, vecCenter1.y - 100.f };
static vec2_t vecOldLocation = vecLocation;
vecLocation.Rotate( i, vecCenter1 );
if ( i > 0.f )
Line( vecOldLocation, vecLocation, 2, true, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
vecOldLocation = vecLocation;
}
这是我的轮换:
void vec2_t::Rotate( float flDegrees, vec2_t vecSubtractVector )
{
flDegrees = ToRadian( flDegrees );
float flSin = sin( flDegrees );
float flCos = cos( flDegrees );
*this -= vecSubtractVector;
x = x * flCos - y * flSin;
y = x * flSin + y * flCos;
*this += vecSubtractVector;
}
我尝试了几种不同的旋转方法,其中 none 似乎有效。如果有人能告诉我我做错了什么,我将不胜感激。
关键行:
x = x * flCos - y * flSin;
y = x * flSin + y * flCos; << problem
第二行使用 x
的 修改后的 值,而它应该使用原始值。您必须在更新之前缓存两个坐标(或至少 x
):
void vec2_t::Rotate( float flDegrees, vec2_t vecSubtractVector )
{
float flRadians = ToRadian( flDegrees );
float flSin = sin( flRadians );
float flCos = cos( flRadians );
// cache both values + pre-subtract
float xOld = x - vecSubtractVector.x;
float yOld = y - vecSubtractVector.y;
// perform the rotation and add back
x = xOld * flCos - yOld * flSin + vecSubtractVector.x;
y = xOld * flSin + yOld * flCos + vecSubtractVector.y;
}
- 要去掉 for-loop 中的 if-statement,只需计算循环外的第一个点,并从增量值而不是零开始
- 不要使用
static
因为它可能会导致线程安全问题(尽管对你的情况并不重要)- 只需在循环外声明它
您似乎缺少一条线段 - 条件需要为 <= 360.f
(最好加上一个 epsilon)
vec2_t vecCenter1 = { gui.iWindowSize[ 0 ] / 2.f, gui.iWindowSize[ 1 ] / 2.f };
const float delta_angle = 2.f;
vec2_t vecOldLocation = { vecCenter1.x, vecCenter1.y - 100.f };
for ( float i = delta_angle; i <= 360.f; i += delta_angle ) // complete cycle
{
vec2_t vecLocation = { vecCenter1.x, vecCenter1.y - 100.f };
vecLocation.Rotate( i, vecCenter1 );
Line( vecOldLocation, vecLocation, 2, true, // no if statement
D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
vecOldLocation = vecLocation;
}
所以我试图围绕 window 中的另一个点旋转一个点,用 DirectX 绘制它。我的问题是旋转的形状很奇怪:
我所做的只是围绕 window 的中心旋转一个点并在这些点之间画线。
vec2_t vecCenter1 { gui.iWindowSize[ 0 ] / 2.f, gui.iWindowSize[ 1 ] / 2.f };
for ( float i { 0.f }; i < 360.f; i += 2.f )
{
vec2_t vecLocation { vecCenter1.x, vecCenter1.y - 100.f };
static vec2_t vecOldLocation = vecLocation;
vecLocation.Rotate( i, vecCenter1 );
if ( i > 0.f )
Line( vecOldLocation, vecLocation, 2, true, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
vecOldLocation = vecLocation;
}
这是我的轮换:
void vec2_t::Rotate( float flDegrees, vec2_t vecSubtractVector )
{
flDegrees = ToRadian( flDegrees );
float flSin = sin( flDegrees );
float flCos = cos( flDegrees );
*this -= vecSubtractVector;
x = x * flCos - y * flSin;
y = x * flSin + y * flCos;
*this += vecSubtractVector;
}
我尝试了几种不同的旋转方法,其中 none 似乎有效。如果有人能告诉我我做错了什么,我将不胜感激。
关键行:
x = x * flCos - y * flSin;
y = x * flSin + y * flCos; << problem
第二行使用 x
的 修改后的 值,而它应该使用原始值。您必须在更新之前缓存两个坐标(或至少 x
):
void vec2_t::Rotate( float flDegrees, vec2_t vecSubtractVector )
{
float flRadians = ToRadian( flDegrees );
float flSin = sin( flRadians );
float flCos = cos( flRadians );
// cache both values + pre-subtract
float xOld = x - vecSubtractVector.x;
float yOld = y - vecSubtractVector.y;
// perform the rotation and add back
x = xOld * flCos - yOld * flSin + vecSubtractVector.x;
y = xOld * flSin + yOld * flCos + vecSubtractVector.y;
}
- 要去掉 for-loop 中的 if-statement,只需计算循环外的第一个点,并从增量值而不是零开始
- 不要使用
static
因为它可能会导致线程安全问题(尽管对你的情况并不重要)- 只需在循环外声明它 您似乎缺少一条线段 - 条件需要为
<= 360.f
(最好加上一个 epsilon)vec2_t vecCenter1 = { gui.iWindowSize[ 0 ] / 2.f, gui.iWindowSize[ 1 ] / 2.f }; const float delta_angle = 2.f; vec2_t vecOldLocation = { vecCenter1.x, vecCenter1.y - 100.f }; for ( float i = delta_angle; i <= 360.f; i += delta_angle ) // complete cycle { vec2_t vecLocation = { vecCenter1.x, vecCenter1.y - 100.f }; vecLocation.Rotate( i, vecCenter1 ); Line( vecOldLocation, vecLocation, 2, true, // no if statement D3DCOLOR_ARGB( 255, 255, 255, 255 ) ); vecOldLocation = vecLocation; }