Original Post
Hello again everybody! I am making a 3D terrain engine and so far things are going pretty well. Thanks in no small part to you all, I now have objects showing up in the right spots, independant of the terrain and the camera, and that much is good. However, now my problem has to do with 3 dimensional movement.
A given object has the following variables:
float x, y, z;
float roll, pitch, yaw;
As we all know, planes don''t generally change their yaw directly; they roll to one side, pitch upward to turn, and then roll back to have changed their yaw. Therefore, if I am to be modeling the movements of a plane, I will need it where you can affect the yaw while only using controls that effect roll and pitch. I have made a function that does this:
void Pitch(OBJECT * obj, int code, float dist)
{
float roll;
float pitch;
float yaw;
switch (code)
{
case UP:
roll = obj->roll * 3.14159f / 180.0f;
pitch = (float)(cos(roll) * dist);
yaw = (float)(sin(roll) * dist);
break;
case DOWN:
roll = (obj->roll + 180) * 3.14159f / 180.0f;
pitch = (float)(cos(roll) * dist);
yaw = (float)(sin(roll) * dist);
break;
default: break;
}
obj->pitch += pitch;
obj->yaw += yaw;
}
...basically how this works is as such: when you hit left or right, you decrease or increase the roll. When you hit up or down, you call the above function with the described movement code, to update your pitch and yaw accordingly. If you see any problems with the above, let me know.
Now, in my project, I am moving the plane along forward in whatever direction it''s facing, and I make the camera at the same position as the ship, but a bit behind it and above it, relative to the roll, pitch, and yaw of the plane. I am setting it up, at least for right now, such that wherever you turn, the plane will always be on the same spot on the screen as if it isn''t moving, but that everything else is; as if the camera and the ship are glued together in a sense. I don''t quite have that yet.
Now, my current forward movement code is:
x_move = (float)(sin(yaw) * cos(pitch) * dist);
y_move = (float)(cos(yaw) * cos(pitch) * dist);
z_move = -(float)(sin(pitch) * dist);
As you may notice, I work with yaw and pitch but not roll for this. I figure that roll would not effect where you are going if you are going straight forward. This seems to work.
However, if you are going in an upward direction, roll would very much effect things, but yaw wouldn''t - right?
My formulae for moving upwards is as follows:
pitch = (obj->pitch - 90.0f) * 3.14159f / 180.0f;
x_move = (float)(sin(roll) * sin(pitch) * dist);
y_move = (float)(cos(roll) * cos(pitch) * dist);
z_move = -(float)(cos(roll) * sin(pitch) * dist);
As you can see, I take the temporary pitch value and manipulate it so that I''m not going forward, but rotated 90 degrees to go upward...
Well, the thing is, it all works fine if I start out and only manipulate the yaw, or if I start out and only manipulate the roll, or if I start out and only manipulate the pitch. Then it is 100% fine. However, when I start pitching AND rolling, or whatever, things get very funky and then my ship isn''t still on my screen. And then naturally I am very annoyed, and start to regret picking this project for my class, since it''s going to be due in a matter of weeks. Oh well.
Please do help me, I would very much appreciate it. I just want it to where you can turn every which way and where the movement works as it should. Thank you for your time.
- Hai, watashi no chichi no kuruma ga oishii deshita!
...or, in other words, "Yes, my dad's car was deliscious!"