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 9 of 9

Thread: scene rolls instead of turning. glRotatef used

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    9

    scene rolls instead of turning. glRotatef used

    I want to be able to rotate the model, to turn up and down, left and right. However, i start rolling instead of turning. In my code i use thetahor (theta horizontal) to represent the angle on the xy-plane, and thetaver (theta vertical) to represent the vertical inclination. here is my code. i'm using the z-axis as up.

    glRotatef(thetahor, 0,0,1)
    glRotatef(thetaver,math.cos(thetahor/180*math.pi),math.sin(thetahor/180*math.pi),0)

    the first line is supposed to rotate the object around the z-axis, the up down axis, and control the side to side motion. so if i want to turn right, i'll rotate around the x axis 90 degrees or so
    the second line is supposed to control the up-down vertical motion. when my thetahor is at zero degrees, math.cos(thetahor/180*math.pi) becomes 1 and math.sin(thetahor/180*math.pi) becomes zero, which means the scene is rotated around the x-axis (as it should). when thetahor is 90 degrees, the reverse happens.

    However, there is a problem with my code. the first line doesn't work as expected. Instead of turning as i envisioned it, the scene rolls instead.
    This image should illustrate the problem: http://i.imgur.com/9USwKJ8.png
    instead of turning, as a car would, my scene rolls.
    Why is this the case and how can i fix it?

  2. #2
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    65
    could be because opengl uses another set of axis. Think of it as extending a 2d x,y plain (y up/down, x left/right) .. adding z as the in-out of that 2d plane. In that sense your rotation obeys the view: rotates around the in/out vector.
    umm .. that would be notes from a standard example...
    You would have to have a setup of model and camera, right? Have you 'composed' your camera right?
    Last edited by CarstenT; 12-19-2013 at 01:08 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    9
    if x is the direction a car is going, and y is perpendicular to that, and z is vertical with the ground, i want the car to turn. instead, it rolls, as if it's rotating on the x-axis. rotating the z axis should turn the car instead of rolling it, right?

  4. #4
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    167
    Take 20 minutes to make a simple triad of 3 line segments - x (red), y (green), and z (blue). Place the triad at the center of your car. Now try your rotations. I think you'll find the problem.

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    9
    i did it with three pencils. if i'm understanding you correctly, you mean to say that axes rotate with each other. that is, if the x-axis rotates, the z-axis will correspondingly move with it.

    however, if this is true, then i shouldn't have to rotate the z-axis at all. just by changing the x and y axis i should be able to cause a turning effect. for example, just by rolling the helicopter to the left or right (rotating the x-axis), and pointing it upwards or downwards (y-axis), i should be able to control the axis that is perpendicular to the floor of the helicopter (the z-axis).

    however, i tried commenting out the line that rotated the z-axis and it does not work. it rotates in a strange manner.

    i'm not sure how to proceed. what is the normal way to do camera rotation?

  6. #6
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    65
    kolkon,
    get your code on the table.
    I'm not too confident in fixed functionality, but it's very basic.
    You are probably rotating the camera where you expect to rotate the object ..

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    9
    sure.

    http://pastebin.com/0dwNu9D5

    lines 72 to 99 is the camera control part

    Code :
    a, b, = window.width/2, window.height/2 #this stores mouse position
    k = 0.01 #sensitivity
    gx = 0 #this stores the angle
    gy = 0 #this also stores the angle
     
    @window.event
    def on_mouse_press(x, y, button, mod): #No effect?
        global a, b
        a = x
        b = y
        return pyglet.event.EVENT_HANDLED
     
    @window.event
    def on_draw():
        global a, b, k, gx, gy
        display()
        reshape()
        window.clear()
        #
        glMatrixMode(GL_MODELVIEW)
        gx += -k*(a-window.width/2) #if mouse on right side of screen, gx will be positive. If left, gx is negative
        gy += -k*(b-window.height/2) #if mouse on top of sceen, gy will ne positive. If bottom, gy is negative
        a, b, = window.width/2, window.height/2 #reset positions so scene does not move
        #
        glRotatef(-gx, 0,0,1)#side to side
        glRotatef(-gy,math.cos(-gx/180*math.pi),math.sin(-gx/180*math.pi),0) #up down
        #
        world.draw()

  8. #8
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    65
    hi Kolkon,
    Sorry for my absence.
    This haphazard tut
    http://www.loria.fr/~roegel/cours/iu...gl/addison.pdf
    uses
    glRotatef(spin, 0.0, 0.0, 1.0);
    and gets a rolling motion, like you.
    You may have a wrong perception of what's supposed to happen.

  9. #9
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    9
    thank you for that. i copied one of the examples from the link you showed, rearranged the axes (since i have z as up), and it now works perfectly!

Posting Permissions

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