Weird behavior merging 2 class functions and passing variables by reference
I have the following two class functions which I'm attempting to merge into one because they are very similar:
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;
}
}
As you can see, Player
and Position
are two private variables of the class. They're data type is Vector3D
, another class.
I'm trying to merge them like this:
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 * 开发者_C百科cos(beta) * sin(alpha),
reference.y - CAMERA_ORBIT_OFFSET * sin(-beta),
reference.z - CAMERA_ORBIT_OFFSET * cos(beta) * cos(alpha)
);
} else {
target = reference;
}
}
This compiles and such but it doesn't behave the same. While the other two functions are working, when I replace those function calls to this one instead, it doesn't work as it should.
The replacements I did are these:
UpdatePlayerPosition() -> UpdateCameraOrPlayerPosition(Player, Position)
UpdateCameraPosition() -> UpdateCameraOrPlayerPosition(Position, Player)
I also tried with a pointer instead of reference, the result was the same. Am I missing something here?
You'll notice that target.SetValue uses + for Camera and - for Player when adjusting the X Y and Z coordinates.
精彩评论