Rotating objects with python

Help bring our custom Ages to life! Share tips and tricks, as well as code samples with other developers.

Rotating objects with python

Postby boblishman » Thu May 08, 2008 11:22 am

I have been successful in making objects move in any direction (x,y, or/and z axis) using the python "door template" (and help from a great programmer ;) ) ... however, I have no idea how to make an object rotate ... is it possible, and if so, what would the coding be?

Any help would be gratefully recieved ...
when it comes to Age creation ... "DOH" seems to be my middle name...
User avatar
boblishman
 
Posts: 882
Joined: Fri Oct 05, 2007 4:47 pm
Location: Spain

Re: Rotating objects with python

Postby Trylon » Thu May 08, 2008 12:58 pm

Oh, I made a python class for that a while back...

Here it is:
Code: Select all
from Plasma import *
from PlasmaTypes import *
from PlasmaNetConstants import *
from xPsnlVaultSDL import *
import math
import time



class ScnObjManipulator:
    def __init__(self,object,agename):
        try:
            self.scnobj = PtFindSceneobject(object, agename)
        except:
            self.scnobj = None
           
        self.initialMtx = self.getMatrix()

    # resets the object to the initial position and scale
    def reset(self):
        self.setMatrix(self.initialMtx)
       
    def getMatrix(self):
        return self.scnobj.getLocalToWorld()
       
    def setMatrix(self,l2w):
        # this is a weird way of calculating an inverse matrix, the getInverse() function
        # actually stores the inverse matrix into the matrix that is used as an argument.
        w2l = ptMatrix44()
        l2w.getInverse(w2l)

        self.scnobj.setTransform(l2w,w2l)
   
    # Scales an object - relative to current scale
    def scale(self,scale):
        l2w = self.getMatrix()
        self.setScale(scale,l2w)
   
    # Scales an object - relative to initial scale
    def setScale(self,scale,initialmtx=None):
        if initialmtx is None:
            initialmtx = self.initialMtx

        scalemtx = ptMatrix44()
        scalemtx.makeScaleMat(scale)
       
        initialmtx *= scalemtx
        self.setMatrix(initialmtx)

    # Moves an object - relative to current position
    def move(self,vector):
        l2w = self.getMatrix()
        self.setMove(vector,l2w)
   
    # Moves an object - relative to initial position
    def setMove(self,vector,initialmtx=None):
        if initialmtx is None:
            initialmtx = self.initialMtx

        trltmtx = ptMatrix44()
        trltmtx.makeTranslateMat(vector)
       
        initialmtx *= trltmtx
        self.setMatrix(initialmtx)
   
    # Rotates an object - relative to current rotation
    def rotate(self,anglevector,radians):
        l2w = self.getMatrix()
        self.setRotation(anglevector,radians,l2w)
   
    # Rotates an object - relative to initial rotation
    def setRotation(self,anglevector,radians,initialmtx=None):
       
        if initialmtx is None:
            initialmtx = self.initialMtx
           

        fullrotmtx = ptMatrix44()
       
        # now, apply relative transformations for the anglevector
        anglevector.normalize() # normalize before using it for scale
       
        # for the X part
        xfactor = anglevector.getX()
        rotmat = ptMatrix44()
        rotmat.makeRotateMat(0,radians*xfactor)

        fullrotmtx *= rotmat

        # for the Y part
        yfactor = anglevector.getY()
        rotmat = ptMatrix44()
        rotmat.makeRotateMat(2,radians*yfactor)

        fullrotmtx *= rotmat

        # for the Z part
        zfactor = anglevector.getZ()
        rotmat = ptMatrix44()
        rotmat.makeRotateMat(1,radians*zfactor)

        fullrotmtx *= rotmat

        initialmtx *= fullrotmtx
        self.setMatrix(initialmtx)


The following example shows how to use it. I've stripped it down to only the most neccesary lines...
Code: Select all
class FuncChk01(ptResponder,):
    __module__ = __name__

    def __init__(self):
        ptResponder.__init__(self)
       self.RotTestObj = none
    def OnServerInitComplete(self):
        # Obtain the piston objects

        self.RotTestObj = ScnObjManipulator("Rotate1",self.AgeName)

        # Register a timer callback for moving the piston objects
        PtAtTimeCallback(self.key, self.MovePistonInterval, self.MovePistonTimerId)

    def OnTimer(self, id):
        # Check for the piston timer
        if (id == self.MovePistonTimerId):
            # Calculate the height offset of the pistons, based on the time
            currentTime = time.clock()

            # Warp the pistons to the new height

            # Rotational object
            rotOffset = (math.sin(2.0*currentTime) *0.05) + 1.0

            angle = ptVector3(0.0,0.0,1.0)
            self.RotTestObj.rotate(angle,rotOffset);       

            # Register for another piston update
            PtAtTimeCallback(self.key, self.MovePistonInterval, self.MovePistonTimerId)


Ok, it mayt not be all that clear, I admit, but basically, you can do the following things with the sceneobjects through this wrapper class:
- Do relative rotation
- Set absolute rotation (relative to the original position of the object)
- Do relative scaling
- Set absolute scales (relative to the original position of the object)
- Move
- Reposition

The comments in the code file should help you get the correct function

Note:
"Relative to the original position of the object" means that it can scale it relative to when the wrapper class was constructed.
If you create the wrapper object again each time you want to do an operation, you can't reset it to the original settings.
It's best to create the object just once - in the initialization - and just reference it from there (just like in the example code)
One day I ran through the cleft for the fiftieth time, and found that uru held no peace for me anymore.
User avatar
Trylon
 
Posts: 1446
Joined: Fri Sep 28, 2007 11:08 pm
Location: Gone from Uru

Re: Rotating objects with python

Postby boblishman » Thu May 08, 2008 11:56 pm

thanks Trylon ... now, if I only understood what I was supposed to do to rotate an object 45 degrees on the z axis ... :( ...
when it comes to Age creation ... "DOH" seems to be my middle name...
User avatar
boblishman
 
Posts: 882
Joined: Fri Oct 05, 2007 4:47 pm
Location: Spain

Re: Rotating objects with python

Postby Trylon » Fri May 09, 2008 5:56 am

Hmm, first you specify the axis:

Code: Select all
#                         x.x,y.y,Z.Z
angle = ptVector3(0.0,0.0,1.0)


Then
Code: Select all
myobject.rotate(angle,math.pi/2)


Should do the trick.

Mind you, this is an instantaneous effect.
To produce the effect as an animation, I'd have to do some more thinking than I can do at the moment, I may get back to you on that.
One day I ran through the cleft for the fiftieth time, and found that uru held no peace for me anymore.
User avatar
Trylon
 
Posts: 1446
Joined: Fri Sep 28, 2007 11:08 pm
Location: Gone from Uru

Re: Rotating objects with python

Postby boblishman » Fri May 09, 2008 5:58 am

yes...it was the "animation" effect I was after ... thanks... (no rush)
when it comes to Age creation ... "DOH" seems to be my middle name...
User avatar
boblishman
 
Posts: 882
Joined: Fri Oct 05, 2007 4:47 pm
Location: Spain


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests

cron