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()