• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JoryRFerrell

Why are my cubes randomly rotating a few seconds into animation?

4 posts in this topic


Why are my cubes randomly rotating near the end of their animation? Also, why is the flat, stationary plane drawing with one edge"bending" into the origin?

 


SCREEN_SIZE = (800, 600)

import sys
from math import radians

from OpenGL.GL import *
from OpenGL.GLU import *

import pygame
from pygame.locals import *

from gameobjects.matrix44 import *
from gameobjects.vector3 import *
import random

def resize(width, height):

    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90.0, float(width)/height, 0.1, 10000.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


def init():

    glEnable(GL_DEPTH_TEST)

    glShadeModel(GL_FLAT)
    glClearColor(1.0, 1.0, 1.0, 1.0)

    glEnable(GL_COLOR_MATERIAL)

    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glLight(GL_LIGHT0, GL_POSITION,  (10, 10, 10, 0))


class ground(object):
    def __init__(self):
        self.position = (0.0, 0.0, 0.0)
        self.color = tuple([random.uniform(0.0,1.5), random.uniform(0.0,1.5) , random.uniform(0.0,1.5)])

    def render(self):
        glColor(0.0, 1.0, 1.0)



        glBegin(GL_QUADS)

        glNormal(0.0, 0.0, 1.0)
        glVertex3f(1.000000, 0.000000, 1.000000)
        glVertex3f(-1.000000, 0.000000, 1.000000)
        glVertex3f(1.000000, 0.000000, -1.000000)
        glVertex3f(-1.000000, 0.000000, -1.0000000)

        glEnd()








class Cube_Projectile(object):


    def __init__(self, position):

        self.position = (0.0, 0.0, 0.0)
        self.color = tuple([random.uniform(0.0,1.0), random.uniform(0.0,1.0) , random.uniform(0.0,1.0)])
        self.life = 1000

    def render(self):

        if self.life > 0:

            glColor(self.color)

            # Adjust all the vertices so that the cube is at self.position
            # vertices = [tuple(Vector3(v) + self.position) for v in self.vertices]

            # Draw all 6 faces of the cube

            self.life -= 1


            glTranslatef(self.position[0], self.position[1], self.position[2])
            glRotate(30, 1, 1, 0)

            glBegin(GL_QUADS)

            glNormal3f(1.000000, 0.000000, 0.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)

            glNormal3f(0.000000, 0.000000, 1.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)

            glNormal3f(-1.000000, 0.000000, -0.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)

            glNormal3f(0.000000, 0.000000, -1.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)

            glNormal3f(0.000000, -1.000000, 0.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)

            glNormal3f(0.000000, 1.000000, 0.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)

            glEnd()

            glRotate(-30, -1, -1, 0)
            glTranslatef(-self.position[0], -self.position[1], -self.position[2])


def run():

    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)

    resize(*SCREEN_SIZE)
    init()

    clock = pygame.time.Clock()

    glMaterial(GL_FRONT, GL_AMBIENT, (0.1, 0.1, 0.1, 1.0))
    glMaterial(GL_FRONT, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))

    # Camera transform matrix
    camera_matrix = Matrix44()
    camera_matrix.translate = (10.0, 2.0, 20.0)


    # Initialize speeds and directions
    rotation_direction = Vector3()
    rotation_speed = radians(90.0)
    movement_direction = Vector3()
    movement_speed = 5.0

    objects_Render = []
    ground_1 = ground()
    objects_Render.append(ground_1)

    mouse_pos = pygame.mouse.get_pos()

    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                return
            if event.type == KEYUP and event.key == K_ESCAPE:
                return


        # Clear the screen, and z-buffer

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        time_passed = clock.tick()

        time_passed_seconds = time_passed / 1000.

        pressed = pygame.key.get_pressed()

        # Reset rotation and movement directions

        rotation_direction.set(0.0, 0.0, 0.0)

        movement_direction.set(0.0, 0.0, 0.0)

        # Modify direction vectors for key presses
        if pressed[K_LEFT]:
            rotation_direction.y = +1.0
        elif pressed[K_RIGHT]:
            rotation_direction.y = -1.0
        if pressed[K_UP]:
            rotation_direction.x = +1.0
        elif pressed[K_DOWN]:
            rotation_direction.x = -1.0
        if pressed[K_d]:
            rotation_direction.z = -1.0
        elif pressed[K_a]:
            rotation_direction.z = +1.0
        if pressed[K_w]:
            movement_direction.z = -5.0
        elif pressed[K_s]:
            movement_direction.z = +5.0


        if pygame.mouse.get_pressed()[0]:
            print pygame.mouse.get_pressed()[0]
            new_mouse_pos = pygame.mouse.get_pos()

            if new_mouse_pos != mouse_pos:

                # rotation_direction.z = (float(new_mouse_pos[0]) - (mouse_pos[0]))/2.0
                if new_mouse_pos > mouse_pos:
                    rotation_direction.z = ((1.0*new_mouse_pos[0]) - (1.0*mouse_pos[0]))/360

                elif new_mouse_pos < mouse_pos:
                    rotation_direction.z = -((1.0*mouse_pos[0]) - (1.0*new_mouse_pos[0]))/360

                if new_mouse_pos[0] > mouse_pos[0]:
                    rotation_direction.x = SCREEN_SIZE[1]/60.0 * .1

                elif new_mouse_pos[0] > mouse_pos[0]:
                    rotation_direction.x = SCREEN_SIZE[1]/60.0 * -.1

            mouse_pos = new_mouse_pos

        mouse_pos = (0, 1)


        if pressed[K_n]:

            # Beginning of objects_Render.append()
            objects_Render.append(Cube_Projectile((0.0,0.0,0.0))) # End of objects_Render.append()

        # Calculate rotation matrix and multiply by camera matrix
        rotation = rotation_direction * rotation_speed * time_passed_seconds
        rotation_matrix = Matrix44.xyz_rotation(*rotation)
        camera_matrix *= rotation_matrix

        # Calcluate movment and add it to camera matrix translate
        heading = Vector3(camera_matrix.forward)
        movement = heading * movement_direction.z * movement_speed
        camera_matrix.translate += movement * time_passed_seconds

        # Upload the inverse camera matrix to OpenGL
        glLoadMatrixd(camera_matrix.get_inverse().to_opengl())

        # Light must be transformed as well
        glLight(GL_LIGHT0, GL_POSITION,  (0, 1.5, 1, 7))


        print event.type



        for obj in objects_Render:

            """
            print "Object: ", objects_Render.index(obj)
            print "Object Position: ", obj.position, "\n"
            """

            tup = []
            tup.append(obj.position[0] + random.uniform(-0.70,0.50))
            tup.append(obj.position[1] + random.uniform(-0.70,0.50))
            tup.append(obj.position[2] + random.uniform(-0.70,0.50))
            obj.position = tuple(tup)

            obj.render()



        # Show the screen
        pygame.display.flip()

run()

 

0

Share this post


Link to post
Share on other sites

First, when you draw you call



glRotatef(30, 1, 1, 0)

 

And after drawing you call:



glRotatef(-30, -1, -1, 0)

 

. To reverse the rotation, you only need to change the sign of the degrees, not the axis, so it should be:



 glRotatef(-30, 1, 1, 0);

 

To avoid this (and to make it easier), instead of doing this:





glTranslatef(self.position[0], self.position[1], self.position[2])
glRotate(30, 1, 1, 0)

... draw ...

glRotate(-30, 1, 1, 0)
glTranslatef(-self.position[0], -self.position[1], -self.position[2])

do





glPushMatrix();
glTranslatef(self.position[0], self.position[1], self.position[2])
glRotate(30, 1, 1, 0)

... draw ...

glPopMatrix();

 

This way, between glPushMatrix() and glPopMatrix(), you can do whatever transformations you want, that it won't affect the previous matrix.

 

I don't know if this is exactly your problem, but when you draw the cubes, you're translating them first and then rotating.

In this case, the farther away the cube is from the origin, the longer will be the rotation, because it will be rotating around the axis and not around itself.

Again, i don't know if this is intentional or not, if it is then forget it.

 

I don't really know Python, but this seems to be a problem with OpenGL, so i hope it helps.

 

Also, unless the OpenGL spec is different for Python, shouldn't the glRotate() call be glRotatef() (or glRotated() if your using doubles)?

Edited by __SKYe
1

Share this post


Link to post
Share on other sites

Also, unless the OpenGL spec is different for Python, shouldn't the glRotate() call be glRotatef() (or glRotated() if your using doubles)?

 

Python doesn't really make a distinction between float and double, being dynamically typed, so I'd assume it chooses the proper function under the hood and casts as needed. Having glRotatef and glRotated exposed wouldn't be pythonic smile.png

Edited by Bacterius
1

Share this post


Link to post
Share on other sites

First, when you draw you call



glRotatef(30, 1, 1, 0)

 

And after drawing you call:



glRotatef(-30, -1, -1, 0)

 

. To reverse the rotation, you only need to change the sign of the degrees, not the axis, so it should be:



 glRotatef(-30, 1, 1, 0);

 

To avoid this (and to make it easier), instead of doing this:





glTranslatef(self.position[0], self.position[1], self.position[2])
glRotate(30, 1, 1, 0)

... draw ...

glRotate(-30, 1, 1, 0)
glTranslatef(-self.position[0], -self.position[1], -self.position[2])

do





glPushMatrix();
glTranslatef(self.position[0], self.position[1], self.position[2])
glRotate(30, 1, 1, 0)

... draw ...

glPopMatrix();

 

This way, between glPushMatrix() and glPopMatrix(), you can do whatever transformations you want, that it won't affect the previous matrix.

 

I don't know if this is exactly your problem, but when you draw the cubes, you're translating them first and then rotating.

In this case, the farther away the cube is from the origin, the longer will be the rotation, because it will be rotating around the axis and not around itself.

Again, i don't know if this is intentional or not, if it is then forget it.

 

I don't really know Python, but this seems to be a problem with OpenGL, so i hope it helps.

 

Also, unless the OpenGL spec is different for Python, shouldn't the glRotate() call be glRotatef() (or glRotated() if your using doubles)?

 

Cool beans. You answered my question and you helped me a lot with the info on Push and Pop funcs. I had read about them but I didn't really pay attention. Patience is not my strong suit. :P As for calling rotation before translation, it wasn't intentional, but I didn't really care. I have read in the bookmthat provided a large part of this code, that you should rotate before translation because trans affects rotation, but for now, I was just worried about fooling around with rots. As for the diff between rotatef and rotated,

I have noticed no I'll effect's in the program.

 

TY for your help! biggrin.png

0

Share this post


Link to post
Share on other sites

Also, as for the ground_1 object rendering weirdly....I screwed up the order of the verts. Instead of writing them in the order they appeared in the face, I simply wrote each verts in the order they appeared in list of all verts. The result was that the last 2 verts are actually supposed to be switched, but I failed to do so. So IT WAS rendering properly, just not how I expected it to. :P

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0