PDA

View Full Version : scene rolls instead of turning. glRotatef used



kolkon
12-18-2013, 08:59 PM
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?

CarstenT
12-19-2013, 12:50 PM
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?

kolkon
12-19-2013, 01:08 PM
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?

Carmine
12-19-2013, 04:29 PM
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.

kolkon
12-21-2013, 08:04 PM
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?

CarstenT
12-22-2013, 06:44 AM
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 ..

kolkon
12-22-2013, 06:28 PM
sure.

http://pastebin.com/0dwNu9D5

lines 72 to 99 is the camera control part


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()

CarstenT
12-24-2013, 03:24 AM
hi Kolkon,
Sorry for my absence.
This haphazard tut
http://www.loria.fr/~roegel/cours/iut/opengl/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.

kolkon
12-31-2013, 08:33 AM
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!