Vizard 7 » Tutorials & Examples » Example scripts » Events » Event class timer
7.6

Event class timer

\examples\eventHandlingClasses\classtimer.py

This script demonstrates how use class timers. This example creates a class timer that moves an object in a circle. The same class timer is being used for all objects.

"""
This script demonstrates how to use class timers.
This example creates a class timer that moves an object in a circle.
The same class timer is being used for all objects
"""
import viz
import math
import vizinfo

viz.setMultiSample(4)
viz.fov(60)
viz.go()

vizinfo.InfoPanel()

#Create a class that inherits from viz.EventClass
class Circle(viz.EventClass):
    """This class will animate an object moving in a circle"""
    def __init__(self,object):
        #Initialize the base class
        viz.EventClass.__init__(self)

        #Store the object that we will be moving in a circle
        self.object = object

        #Initialize the offset
        self.offset = [0,0,0]

        #Initialize the radius
        self.radius = 1

        #Initialize the speed (deg/sec)
        self.speed = 90

        #Initialize the starting angle
        self.angle = 0

        #Create a callback to our own event class function
        self.callback(viz.TIMER_EVENT,self.mytimer)

        #Start a perpetual timer for this event class
        self.starttimer(0,0.01,-1)

    def mytimer(self,num):

        #Increment the angle
        self.angle += self.speed * viz.elapsed()

        #Calculate the new position
        x = math.sin(viz.radians(self.angle)) * self.radius
        z = math.cos(viz.radians(self.angle)) * self.radius

        #Translate the object to the new position, accounting for the offset
        self.object.setPosition([self.offset[0]+x,self.offset[1],self.offset[2]+z])

#Add a ground plane
viz.addChild('ground.osgb')

#Add a ball
ball1 = viz.addChild('beachball.osgb')
#Create a new Circle class for the ball
circle1 = Circle(ball1)

#Add a ball
ball2 = viz.addChild('beachball.osgb')
#Create a new Circle class for the ball
circle2 = Circle(ball2)
#Set the speed and radius of the Circle class
circle2.speed = 180
circle2.radius = 2

#Add a ball
ball3 = viz.addChild('beachball.osgb')
#Create a new Circle class for the ball
circle3 = Circle(ball3)
#Set the speed and radius of the Circle class
circle3.speed = -180
circle3.radius = 3

#Add a ball
ball4 = viz.addChild('beachball.osgb')
#Create a new Circle class for the ball
circle4 = Circle(ball4)
#Set the speed, radius, and offset of the Circle class
circle4.speed = -90
circle4.radius = 2
circle4.offset = [0,1,0]

#Move the viewpoint back 7 meters
viz.MainView.move([0,0,-7])

#We don't need to do anything else. The class timers will animate themselves