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: Moving around in 3d space with input

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2000
    Location
    Austin, TX, usa
    Posts
    3

    Moving around in 3d space with input

    HI,
    I am trying to get user input from the client and move around in 3D space. I thought it would be an easy task buy doing something like this

    case 'f' :
    {
    veiwerPos.z += 1;
    } break;
    case 'r':
    {
    veiwerPos.z -=1;
    } break;
    case 'g':
    {
    veiwerPos.x += 1;
    } break;
    case 'h' :
    {
    veiwerPos.x -= 1;
    } break;

    vgvec2 pos(event->mouseDeltaX, event>mouseDeltaY);
    angleX += pos.x;
    angleY += pos.y;
    if(angleY > 45.0f)
    angleY = 45.0f;
    if(angleY < -45.0f)
    angleY = -45.0f;
    axis = pos.Normalize();
    glRotatef(angleX, 0, axis, 0);
    glRotatef(angleY, axis, 0, 0);
    glTranslatef(veiwerPos.x, veiwerPos.y, veiwerPos.z )

    I am not saying this works but it is not how I want it to work. If any one that knows what I am doing wrong or can help me out please e-mail me or respond to this post
    Thanks
    David Gholami
    You must learn how to crawl before you can walk

  2. #2
    Junior Member Newbie
    Join Date
    Feb 2000
    Posts
    22

    Re: Moving around in 3d space with input

    you have to think of a sphere.
    so, i will give you the math, you will write the code:


    x = sin(angle_one_in_radians)
    *cos(angle_two_in_radians)

    z = cos(angle_one_in_radians)
    *cos(angle_two_in_radians)

    y = cos(angle_one_in_redians)
    *sin(angle_two_in_radians)


    where angle_one is the angle starting looking down the z-axis and rotating around the y-axis, and angle_two is rotating around the x-axis.

    this i called the equation for a sphere, pretty standard stuff, this gets the unit change, you can multiply x and z by any scalar to increase the step amount.

    finally it will look like this

    for walking forward:

    player_pos_x += player_pos_x + (x*scalar)
    player_pos_z += player_pos_z + (z*scalar)

    walking backward just subtracts tehm.

    and for turning:

    cam_pos_x += cam_pos_x + (x*scalar)
    cam_pos_x += cam_pos_x + (z*scalar)
    cam_pos_y += cam_pos_y + (y*scalar)

  3. #3
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,453

    Re: Moving around in 3d space with input

    I'm not sure what the axis = pos.Normalize()means. Normally I would expect something like
    glRotatef(angleX, 1.0f, 0.0f, 0.0f);
    glRotatef(angleY, 0.0f, 1.0f, 0.0f);
    for the x- and y-axis (or maybe in the other order, first yaw then pitch?)

    Don't forget to start with the same matrix all the time, so add glLoadIdentity() before or glPushMatrix(); glPopMatrix() around the transformations. Otherwise the rotations and transformations will concatenate at each loop (far out...).

Posting Permissions

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