[英]2D Rotation Issue C++ DirectX
因此,我試圖將一個點圍繞窗口中的另一個點旋轉,並使用DirectX進行繪制。 我的問題是旋轉狀態很奇怪:
我正在做的只是圍繞窗口中心旋轉一個點,並在這些點之間繪制線條。
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;
}
我嘗試了幾種不同的輪換方法,但似乎都沒有用。 如果有人能告訴我我在做什么錯,我將不勝感激。
關鍵線:
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;
}
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; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.