views:

432

answers:

2

Hello, I'm making a 2D game. I want to be able to render a texture on the screen after rotating it a certain amount around a centre point. Basically this is for a level rotation around a player. The player position being the rotation point and the direction of the player as the angle. This code wont work:

def draw_texture(texture,offset,size,a,rounded,rotation,point):
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity() #Loads model matrix
    glColor4f(1,1,1,float(a)/255.0)
    glTranslatef(point[0],point[1],0)
    glRotatef(rotation,0,0,1)
    glBindTexture(GL_TEXTURE_2D, texture)
    if rounded == 0:
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 0.0)
        glVertex2i(*offset) #Top Left
        glTexCoord2f(0.0, 1.0)
        glVertex2i(offset[0],offset[1] + size[1]) #Bottom Left
        glTexCoord2f(1.0, 1.0)
        glVertex2i(offset[0] + size[0],offset[1] + size[1]) #Bottom, Right
        glTexCoord2f(1.0, 0.0)
        glVertex2i(offset[0] + size[0],offset[1]) #Top, Right
        glEnd()
    else:
        #Nothing important here
    glEnd()

Any way to get it working? Thank you.

+1  A: 

try reversing

glTranslatef(point[0],point[1],0)

and

glRotatef(rotation,0,0,1)

you're translating to the player, but then rotating about the origin (not the player)

Illustration from the red book: rotate

cobbal
It doesn't seem to allow me to move the texture to the correct position like it was. When I try to move the player, it gets stuck. Odd.Thank you for the reply.I might use trigonometry to do this. I would simply prefer a simpler method.
Matthew Mitchell
one way to debug: start everything at the origin with 0 rotations, then gradually re-add the translations and rotations and making sure they're where you expect.
cobbal
A: 

Unless you have a good reason to do otherwise, I'd leave the drawing code alone, and just change the camera angle. Probably the easiest way to do that is use gluLookAt. In your case, you'll apparently be looking at the player's position, and just change the "up direction", which is given in the last two parameters.

Jerry Coffin
How would I do this? I do not understand that function at all.I understand there is 3 sets of coordinates but I don't know what for.I'll have a gamble and think the eye coordinates should be at the players coordinates and so should the center ones. The up coordinates should then be a coordinate which is at the angle I want from the player. I guess a tiny bit of trigonometry would do for that.
Matthew Mitchell
A big failure:gluLookAt(point[0],point[1],0,point[0],point[1],0,point[0] + maths.sin(rotation),point[1] - maths.cos(rotation),0)
Matthew Mitchell
gluLookAt is probably not what you want, as you're positioning a model and not a camera. While it would be possible, it wouldn't make much sense.
cobbal
At least if memory serves, the three sets of coordinates are for the position of the camera, the position of the "center point" it's looking at, and the third is which direction should be "up". If you don't want to use that, you can also `glMatrixMode(GL_PROJECTION)`, and manipulate the projection matrix about like you normally would the model matrix.
Jerry Coffin
Well, I decided that I'd try out my own rotation. I've made a successful coordinate rotation function, I just need to use it properly to rotate the quad how I want. To complicate matters, The y-axis is reversed to how graphs are made so I need to keep that in mind. I'm going to see what I can do to tomorrow.Thank you for everyone's help.
Matthew Mitchell