简体   繁体   English

旋转功能奇怪的行为

[英]Rotation function weird behaviour

I've been working on a rotation function. 我一直在研究旋转功能。 The points move sligthly to the rotation center with each cycle. 每个循环,这些点随着滑动中心移动到旋转中心。 It depends on a function to get the distance between the center and the point to rotate: 它取决于获取中心和旋转点之间距离的函数:

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; 
}

The actual rotation function, that does work: 实际的旋转功能, 确实有效:

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;
}

The function that does not work: 工作的功能:

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;
}

I have no clue what logical difference there is between these two. 我不知道这两者之间存在什么逻辑差异。 Thank your for any advice. 感谢您的任何建议。

In your second function when you update point->x you are changing the properties of point . 在更新point->x第二个函数中point->x您正在更改point的属性。 Therefore when you call the same function again later during setting of point->z , the value of point->x has already changed. 因此,当您在设置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;
}

In the first function, you store the values you require in separate variables before modifying point . 在第一个函数中, 修改point 之前 ,将所需的值存储在单独的变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM