0
傳遞變量我有我試圖合併成一個下面的兩個類功能,因爲他們都非常相似:怪異的行爲合併2類功能,並通過引用
void Camera::UpdateCameraPosition(void) {
if(cameraMode == CAMERA_MODE_THIRD_PERSON) {
float alpha = Math::DegreesToRadians(Rotation.y);
float beta = Math::DegreesToRadians(Rotation.x);
Position.SetValue(
Player.x + CAMERA_ORBIT_OFFSET * cos(beta) * sin(alpha),
Player.y + CAMERA_ORBIT_OFFSET * sin(-beta),
Player.z + CAMERA_ORBIT_OFFSET * cos(beta) * cos(alpha)
);
} else {
Position = Player;
}
}
void Camera::UpdatePlayerPosition(void) {
if(cameraMode == CAMERA_MODE_THIRD_PERSON) {
float alpha = Math::DegreesToRadians(Rotation.y);
float beta = Math::DegreesToRadians(Rotation.x);
Player.SetValue(
Position.x - CAMERA_ORBIT_OFFSET * cos(beta) * sin(alpha),
Position.y - CAMERA_ORBIT_OFFSET * sin(-beta),
Position.z - CAMERA_ORBIT_OFFSET * cos(beta) * cos(alpha)
);
} else {
Player = Position;
}
}
正如你所看到的,Player
和Position
是這個類的兩個私有變量。他們的數據類型是Vector3D
,另一類。
我試圖把它們合併這樣的:
void Camera::UpdateCameraOrPlayerPosition(Vector3D &target, Vector3D reference) {
if(cameraMode == CAMERA_MODE_THIRD_PERSON) {
float alpha = Math::DegreesToRadians(Rotation.y);
float beta = Math::DegreesToRadians(Rotation.x);
target.SetValue(
reference.x - CAMERA_ORBIT_OFFSET * cos(beta) * sin(alpha),
reference.y - CAMERA_ORBIT_OFFSET * sin(-beta),
reference.z - CAMERA_ORBIT_OFFSET * cos(beta) * cos(alpha)
);
} else {
target = reference;
}
}
這編譯等,但它不具有相同的行爲。當另外兩個函數正在工作時,當我將這些函數調用替換爲這個函數調用時,它不會像它應該那樣工作。
我做的替代品是這些:
UpdatePlayerPosition() -> UpdateCameraOrPlayerPosition(Player, Position)
UpdateCameraPosition() -> UpdateCameraOrPlayerPosition(Position, Player)
我也試圖與一個指針,而不是引用,結果是一樣的。我在這裏錯過了什麼嗎?
您更改了'CAMERA_ORBIT_OFFSET'從+到 - 在UpdateCameraPosition() - > UpdateCameraOrPlayerPosition(位置,播放器)的情況下' – 2011-04-13 00:33:22
我是一個白癡......並且盲目:S – 2011-04-13 00:42:45