Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Simple movement in 3D space problem - Please Help

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2002
    Posts
    12

    Simple movement in 3D space problem - Please Help

    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

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,070

    Re: Simple movement in 3D space problem - Please Help

    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...
    Draw_fish();
    glPopMatrix();

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2002
    Posts
    12

    Re: Simple movement in 3D space problem - Please Help

    Thx for telling me I am doing on the right way.

    However, when I combined my four ideas together, I got problems.
    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...

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

    Thanks for answering

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •