Rotate point around null - help pls

On 09/06/2014 at 15:36, xxxxxxxx wrote:

I'm looking for the correct way to do this 
appreciate some help

I have two objects A and B

A is fixed (null)
B is a point

I'd like to rotate the position of B around A by X degrees

where do I start

tia

On 09/06/2014 at 19:34, xxxxxxxx wrote:

I'm not sure if I understand what you're asking for. Since B is a point.
But typically when you want to rotate an object around another object, like a moon around a planet. What you do is move the axis(matrix) to the same place as the object you want it to rotate around. But leave the points of the object (the moon) where they were.
Then when you go to rotate the moon using common rotation code. Since it's axis is offset, it will orbit around the other object (the planet).

Example:

import c4d  
def main() :  
       
  #The object (moon) you want to rotate around another object (the planet)  
  obj = doc.GetActiveObject()   
  objMtxOld = obj.GetMg()  
    
  #The other object (the Planet)  
  nullObj = doc.SearchObject("Null")  
  nullPos = nullObj.GetAbsPos()  
    
  #Move the object (in memory only) to the same location as the Null Object     
  objMtxOld.off = nullPos  
    
  #Get the matrix values at it's new position in memory   
  #And assign it's values to a brand new matrix  
  objMtxNew = obj.GetMg()    
  
  #Now actually move the matrix of the moon object (and the points too)  
  #Reminder: The last time we saved objMtxOld it was at the same position as the Null (The planet)  
  #So that's where the object will move to  
  obj.SetMg(objMtxOld)  
    
  #Get the points of the object (the moon)  
  #So we can move them back to their original positions      
  points = obj.GetAllPoints()  
  pcount = obj.GetPointCount()  
  
  for i in xrange(pcount) :  
      obj.SetPoint(i,~objMtxOld*objMtxNew*points[i]) #Set the points back where they were   
                   
  obj.Message(c4d.MSG_UPDATE)  
    
    
  #Now when you rotate obj (the moon) using code like: obj.SetAbsRot()  
  #It will rotate (orbit) around the Null Object (the planet)  
  #Because the axis for the obj is now in the same place as the Null Object  
    
  c4d.EventAdd()  
  
if __name__=='__main__':  
  main()

I hope the moon & planet thing I used here makes it easier to understand.
And not more confusing.

-ScottA

On 09/06/2014 at 23:52, xxxxxxxx wrote:

Thanks Scott

Going to take a good look at this - I think its what I was looking for - appreciated

Probably confused things (understandably) by saying 'point'

Its actually one null around another - but I currently only have the nulls position ('point' in space)
obviously need the matrix

but looks like the method will work….off to give it a  go….cheers

On 10/06/2014 at 06:29, xxxxxxxx wrote:

Edit:

Got it

import math
import c4d
  
def main() :
   
    moonobj = doc.SearchObject("Moon")
    objMtxOld = moonobj.GetMg()
    
    planetObj = doc.SearchObject("Planet") 
    planetPos = planetObj.GetAbsPos()
    
    objMtxOld.off = planetPos
    
    
   
    objMtxNew = moonobj.GetMg()
    
  
    
    moonobj.SetMg(objMtxOld)
  
    moonpos = moonobj.GetAbsPos()    
    
   
    moonobj.SetAbsPos(~objMtxOld*objMtxNew  * moonpos )  
     
    moonobj.SetRelRot(c4d.Vector(.25,0,0))
  

On 10/06/2014 at 09:24, xxxxxxxx wrote:

I didn't realize you were dealing with Nulls.
The code I posted is used commonly for changing a polygon object's axis. And to be honest I never would have thought to use that kind of matrix stuff for what you're doing. But it seems to work.
Just goes to show that there are many ways to skin a cat.

One other suggestion I have is to parent your moon under a Null Object. And then attach that null to the planet.
That way you can easily rotate the Null, and that will change the orbit without writing a lot more code.

If I had known you were dealing with Nulls. I would probably have posted this code instead.
This code gets the points along a virtual circle. And lets you move things on the circle's points.
You might also need this code one day too.

import c4d, math  
  
def circlePoint(radius, angle, origin) :  
  x = radius * math.cos(angle * math.pi/180)  
  y = radius * math.sin(angle * math.pi/180)  
  return x,y  
  
def main() :  
    
  fps = doc.GetFps()  
  frame = doc.GetTime().GetFrame(fps)  
    
  planet = doc.SearchObject("Planet")   
  planetPos = planet.GetMg().off     
    
  #The object the pytag is on      
  moon = op.GetObject()  
    
  #The Null Object that the moon is a child of  
  #This Null Object let's us change the moon's orbit easily and position easily  
  #Simply move/rotate it to alter the moon's orbit around the planet  
  moonNull = doc.SearchObject("MoonNull")  
    
  moonNull.SetAbsPos(planetPos)      
    
  #Change these values as desired  
  radius = 500  
  speed = 5  
  angle = frame * speed  
    
  #Make the moon orbit the moonNull object  
  p = circlePoint(radius, angle, planetPos)  
  moon.SetAbsPos(c4d.Vector(p[0], p[1], 0))

-ScottA

On 10/06/2014 at 11:07, xxxxxxxx wrote:

This is something I've never gotten my head around - thanks for the code snippets.

Will have a good look through this later