Rotation when carrying object around

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/07/2011 at 07:11, xxxxxxxx wrote:

Hello all,

I have been trying to come up with a simple rotation such that the object is always oriented in the direction of movement.

There are two approaches: adjust the rotation based on the last known orientation
or recalculate the direction from the delta in movement.

I have done the latter in the following code, but it doesn't seem to work very well. (The rotation
is stored in the container just as well, but I couldn't figure out how to do it that way)

I'd be very grateful if someone could help me out here.

Thanks

import c4d, math
#Welcome to the world of Python

def main() :

#get the object where the tag is attached to
  obj=op.GetObject()

#get the current frame of the document
  frame=doc.GetTime().GetFrame(doc.GetFps())
  xrotglobal = frame / 10.0

bc = obj.GetData()
  lastrot = bc.GetData( 1009901 )
  lastpos = bc.GetData( 1009902 )

if lastrot != None:
      obj.SetAbsRot(lastrot)

oppos   = obj.GetAbsPos()
  oppos.y = 100                # fixed value just for now
  obj.SetAbsPos(oppos)
 
  if lastpos == None:
      lastpos = oppos
  deltapos= oppos - lastpos
  lastpos = oppos

rot     = deltapos.GetLength()/100.0   # fixed value just for now
  normdel = deltapos.GetNormalized()
 
  lastrot.x  = math.acos(normdel.x)
  obj.SetAbsRot(lastrot)

lastrot = obj.GetAbsRot()

bc.SetData( 1009901, lastrot )
  bc.SetData( 1009902, lastpos )

obj.SetData(bc)

pass  #put in your code here

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/07/2011 at 16:38, xxxxxxxx wrote:

Not sure what you are actually asking because "doesn't seem to work very well" is not really a good enough description of the actual problem you are having

However i would build a look at matrix myself to deal with what i think you are actually trying to do. There is lots of info on the net on building a lookat matrix.

Basically though i would create a direction vector from  currentpos to lastpos   then build a matrix that targets (currentpos +(direction vector(currentpos - lastpos)))

So that the objects z axis is always facing in the direction it is travelling as defined by the direction vector.

  bc = obj.GetData()  
  lastpos = bc.GetData( 1009902 )
  
  Object = op.GetObject()
objMatrix = Object.GetMg()
p1 = objMatrix.off p2 = p1 + (p1 - lastpos) v0 = p1 v3 = p2 - p1 v3.Normalize() v2 = p2-p1 v2.Cross(c4d.Vector(0,1,0)).Normalize() v1 = p2-p1 v2.Cross(c4d.Vector(0,0,1)).Normalize() m = c4d.Matrix(v0 , v1, v2 , v3) #create new instance of Matrix Rotation = c4d.utils.MatrixToHPB(m) orot = c4d.utils.MatrixToHPB(objMatrix) Rotation.z = orot.z Object.SetAbsRot(Rotation) lastpos = p1#store lastpos
  
  bc.SetData( 1009902, lastpos )  
  obj.SetData(bc) c4d.EventAdd()