2015-11-04 149 views
1

我一直在研究旋轉函數。每個週期點都會輕微移動到旋轉中心。這取決於一個函數來獲取中心與點之間的距離旋轉:旋轉函數奇怪的行爲

struct Vector3F 
{ 
    float x, y, z; 
}; 

inline float GetDistance3FX_inline(Vector3F* v1, Vector3F* v2) 
{ 
    return v1->x - v2->x; 
} 

inline float GetDistance3FZ_inline(Vector3F* v1, Vector3F* v2) 
{ 
    return v1->z - v2->z; 
} 

實際的旋轉功能,即確實工作:

void Rotate3FY(Vector3F* point, Vector3F* center, float rad) 
{ 
    float x = GetDistance3FX_inline(point, center); 
    float z = GetDistance3FZ_inline(point, center); 
    point->x = x * cos(rad) - z * sin(rad) + center->x; 
    point->z = z * cos(rad) + x * sin(rad) + center->z; 
} 

功能工作:

void Rotate3FY(Vector3F* point, Vector3F* center, float rad) 
{ 
    point->x = GetDistance3FX_inline(point, center) * cos(rad) - GetDistance3FZ_inline(point, center) * sin(rad) + center->x; 
    point->z = GetDistance3FZ_inline(point, center) * cos(rad) + GetDistance3FX_inline(point, center) * sin(rad) + center->z; 
} 

我不知道這兩者之間有什麼邏輯差異。 感謝您的任何建議。

+1

什麼不行?錯誤的結果? – Downvoter

回答

3

在更新point->x的第二個函數中,您正在更改point的屬性。因此,如果在設置point->z時稍後再次調用相同的功能,則值point->x已經更改。

void Rotate3FY(Vector3F* point, Vector3F* center, float rad) 
{ 
    point->x = GetDistance3FX_inline(point, center) * cos(rad) - GetDistance3FZ_inline(point, center) * sin(rad) + center->x; 
    // point is now different to what it was at the start because you just 
    // changed is x member!!!!!!! 
    point->z = GetDistance3FZ_inline(point, center) * cos(rad) + GetDistance3FX_inline(point, center) * sin(rad) + center->z; 
} 

在第一個功能,你修改point之前存儲在單獨的變量需要的值。

+0

好的,那時我全力阻滯。非常感謝 – tomatocake