setHpr relate to another node

Hi,
I try to rotate mynode relate to another node,but it always rotate around self X axis, not specified x axis.

#! /usr/bin/env python
#coding=utf-8
from direct.directbase import DirectStart
from panda3d.core import Filename,InternalName
from panda3d.core import GeomVertexArrayFormat, GeomVertexFormat
from panda3d.core import Geom, GeomNode, GeomTrifans, GeomTristrips, GeomTriangles
from panda3d.core import GeomVertexReader, GeomVertexWriter
from panda3d.core import GeomVertexRewriter, GeomVertexData
from panda3d.core import Vec2,Vec3, Vec4, Point3,NodePath,Point2,Camera

import random, sys, os, math

def makeSquare(x1,y1,z1, x2,y2,z2):
	format=GeomVertexFormat.getV3n3cpt2()
	vdata=GeomVertexData('square', format, Geom.UHDynamic)

	vertex=GeomVertexWriter(vdata, 'vertex')
	#normal=GeomVertexWriter(vdata, 'normal')
	#color=GeomVertexWriter(vdata, 'color')
	texcoord=GeomVertexWriter(vdata, 'texcoord')
	
	#make sure we draw the sqaure in the right plane
	if x1!=x2:
		vertex.addData3f(x1, y1, z1)
		vertex.addData3f(x2, y1, z1)
		vertex.addData3f(x2, y2, z2)
		vertex.addData3f(x1, y2, z2)

		#print vertex.getVertexData()
		'''
		normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
		normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y1-1, 2*z1-1)))
		normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
		normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y2-1, 2*z2-1)))
		'''
	else:
		vertex.addData3f(x1, y1, z1)
		vertex.addData3f(x2, y2, z1)
		vertex.addData3f(x2, y2, z2)
		vertex.addData3f(x1, y1, z2)
		'''
		normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1)))
		normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z1-1)))
		normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1)))
		normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z2-1)))
		'''
	'''
	#adding different colors to the vertex for visibility
	color.addData4f(1.0,0.0,0.0,1.0)
	color.addData4f(0.0,1.0,0.0,1.0)
	color.addData4f(0.0,0.0,1.0,1.0)
	color.addData4f(1.0,0.0,1.0,1.0)
	
	'''
	texcoord.addData2f(0.0, 1.0)
	texcoord.addData2f(0.0, 0.0)
	texcoord.addData2f(1.0, 0.0)
	texcoord.addData2f(1.0, 1.0)
	
	#quads arent directly supported by the Geom interface
	#you might be interested in the CardMaker class if you are
	#interested in rectangle though
	tri1=GeomTriangles(Geom.UHDynamic)
	tri2=GeomTriangles(Geom.UHDynamic)

	tri1.addVertex(0)
	tri1.addVertex(1)
	tri1.addVertex(3)

	tri2.addConsecutiveVertices(1,3)

	tri1.closePrimitive()
	tri2.closePrimitive()

	square=Geom(vdata)
	square.addPrimitive(tri1)
	square.addPrimitive(tri2)
		
	return square

def onUpdate(task):
	detX = 0
	detY = 0
	if cmdInfo["cam-left"] == 1:
		detX = 0.1
	elif cmdInfo["cam-right"] == 1:
		detX = -0.1
		   
	if cmdInfo["cam-up"] == 1:
		detY = -0.1
	elif cmdInfo["cam-down"] == 1:
		detY = 0.1
		   
	npRot = nodepath
		
	npRot.setH(npParent,npRot.getH(npParent)+detX)
	npRot.setP(npParent,npRot.getP(npParent)+detY)
	
	
	return task.cont

def setKey(key, value):
	   cmdInfo[key] = value

base.disableMouse()
y = 0
square0=makeSquare(-1,y,-1, 1,y, 1)

snode=GeomNode('square')

snode.addGeom(square0)
nodepath = render.attachNewNode(snode)
nodepath.setTwoSided(True)



filename = "texture/crossLine.bmp"
tex = loader.loadTexture(filename)
nodepath.setTexture(tex)

npParent = loader.loadModel("models/yup-axis")
npParent.reparentTo(render)

nodepath.reparentTo(npParent)

cmdInfo = {"getCamHpr":0,"cam-up":0,"cam-down":0,
					   "cam-left":0,"cam-right":0}
camera.setPos ( 20, -20, 20)          
        
		
#base.setBackgroundColor(0, 0, 0)    
#base.toggleBackface()
#base.toggleWireframe()

base.accept("arrow_up", setKey, ["cam-up", 1])
base.accept("arrow_up-up", setKey, ["cam-up", 0])
	  
base.accept("arrow_down", setKey, ["cam-down", 1])
base.accept("arrow_down-up", setKey, ["cam-down", 0])
	  
base.accept("arrow_left", setKey, ["cam-left", 1])
base.accept("arrow_left-up", setKey, ["cam-left", 0])
	  
base.accept("arrow_right", setKey, ["cam-right", 1])
base.accept("arrow_right-up", setKey, ["cam-right", 0])

base.accept("escape", sys.exit)

taskMgr.add(onUpdate, "myUpdate")

camera.lookAt(nodepath)

run()

It might help if you were to post only the relevant portions of your code, rather than what looks like the whole file – it makes it easier for readers to figure out where your problem is, and makes it more likely that they’ll respond.

In this specific case, am I correct in guessing that the problem is with the lines

npRot.setH(npParent,npRot.getH(npParent)+detX)
npRot.setP(npParent,npRot.getP(npParent)+detY) 

in “onUpdate”?

If so, then that doesn’t look correct at all.

If I’m not much mistaken, calling “setH” with a relative node specified sets your node to a value relative to the “relative node”'s H; that is to say that it doesn’t set your node’s H in the “relative node”'s coordinate system, which is, I take it, what you want.

What you more likely want to do is to rotate the relative node (or a sub-node between that node and your desired node).

Operations such as setH and setP are always in the parent’s coordinate space, so those calls are a bit silly.

hello,
In the simplifed code,I rotate my code around another Node(yup-axis model).It rotate around y axis of yup-axis model is ok,but rotate around x axis is wrong.Here the “parent node” maybe bring some confusion,Please take it as “another node”.I think It is easy to understand if you could run the code once.

In which case, perhaps it might help to temporarily reparent your NodePath (let us call it A) to the NodePath the axis of which you want to use (let us call it B), and then rotate B as desired. That done, you should be able to reparent A back to its original parent and reset B’s HPR, if so desired.

Thanks,the roateion of parent Node could solved my problem,However,I want to know more is the right use(understanding) of setHpr(other,h,p,r).In this case,It seemed not related to z axis of “other” node,but setPos(other,pos) is ok and if I set it rotate around self is also ok.
Best Regards