PDA

View Full Version : Simple movement in 3D space problem - Please Help



takumi
10-03-2002, 03:27 AM
Hello everyone.

I am now implementing a small fish tank project, which the fish can swim freely in the tank.

My problem is that I don't know how to implement the movement in 3D space and quite confused on it.

I have think the following stuff for implementation:
1) use a vector varialbe to store the direction which the fish is facing.
eg: direction = CVector(0, 0, 0); means face to the north


2) use another vector variable to store the speed of the fish.
eg: speed = CVector(0.0, 0.0, 1.0); means the swimming speed in z-axis = 1.0.


3) use this function to calculate the direction that the fish is facing by passing the pitch and yaw angle.
mPitch = up & down angle
mYaw = left & right angle

rX += cos(mPitch / 180 * M_PI) * sin(mYaw / 180 * M_PI);
rY += sin(mPitch / 180 * M_PI);
rZ += cos(mPitch / 180 * M_PI) * cos(mYaw / 180 * M_PI);

direction.x = rX;
direction.y = rY;
direction.z = rZ;


4) use this function to calculate the new position of the fish and then render it.
newposition = position + (speed * deltaTime);


Actually, I just want my fish first rotate to a desired direction and then start swimming forward to that direction is enough.

My concept maybe wrong, I hope someone can make some sense out of it because I can't.

Please tell me anything I have left?

Or you can give me some suggestions or tutorials to reference.

Thx you very much http://www.opengl.org/discussion_boards/ubb/smile.gif

nexusone
10-03-2002, 05:04 AM
Look's like you have good start.

Now just draw your fish with the head looking north.

glPushMatrix();
glTranslatef( .......); location of fish.
glRotatef( x_angle, 1.0, 0.0, 0.0); fish looking right or left.
glRotatef( y_angle, 0.0, 1.0, 0.0); fish looking up or down.
glRotatef( z_angle, 0.0, 0.0, 0.0); fish rotate z..... z_angle = 90 fish on side laying dead at top of fish bowl... http://www.opengl.org/discussion_boards/ubb/smile.gif
Draw_fish();
glPopMatrix();

takumi
10-03-2002, 08:00 AM
Thx for telling me I am doing on the right way.

However, when I combined my four ideas together, I got problems. http://www.opengl.org/discussion_boards/ubb/frown.gif
Here are the codes...


// Rotate fish by passing pitch & yaw angle
void turn(float v_angle, float h_angle)
{
rx += (float)(cos(DEG2RAD(v_angle)) * sin(DEG2RAD(h_angle)));
ry += (float)(sin(DEG2RAD(v_angle)));
rz += (float)(cos(DEG2RAD(v_angle)) * -cos(DEG2RAD(h_angle)));

direction.x = rx;
direction.y = ry;
direction.z = rz;
}


// Swimming forward in the direction of the fish facing
void Swim(scalar_t deltaTime)
{
speed = CVector(0.0, 0.0, 1.0);

position = position + (speed * deltaTime);
}


void Draw()
{
glPushMatrix();

glTranslatef(position.x, position.y, position.z);

glRotatef(direction.x, 0.0, 1.0, 0.0); //yaw
glRotatef(direction.y, 1.0, 0.0, 0.0); //pitch

AnimateModel(0, 1, deltaT * animSpeed); // Draw the fish model

glPopMatrix();
}


void RenderScene()
{
...

turn(pitchangle, yawangle);

Swim(elapsedSec);

Draw();
}


I am now able to rotate my fish in the right direction by pressing the left, right, up, down keys.

However, after the rotation of the fish, it still don't know swimming in the direction it face, it will just keep moving out in the Z-axis only... http://www.opengl.org/discussion_boards/ubb/frown.gif

How should I correct my code in ordering to let the fish swims in the direction it faces?

Thanks for answering http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by takumi (edited 10-03-2002).]