I’ve built a wall and when I call setH on it, instead of the wall rotating on its own axis, it rotates in an orbit. The problem disappears when I build the wall using first point of [0,0] and any for the second point (build([0,0],[1,1]). That’s the furthest I was able to diagnose the problem.
from panda3d.core import *
from direct.showbase.ShowBase import ShowBase
from direct.gui.DirectGui import *
import math
class Core(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.cam.setP(-90)
self.cam.setPos(0, 0, 45)
wall = self.build([-3,1],[-1,1])
def change():
wall.setH(slider['value'])
slider = DirectSlider(range=(0, 360), pos=(0, 0, -0.5), value=0, command=change)
def makeSquare(self,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')
if x1 != x2:
vertex.addData3f(x1, y1, z1)
vertex.addData3f(x2, y1, z1)
vertex.addData3f(x2, y2, z2)
vertex.addData3f(x1, y2, z2)
normal.addData3f(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1))
normal.addData3f(Vec3(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1))
normal.addData3f(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1))
normal.addData3f(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(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1))
normal.addData3f(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1))
normal.addData3f(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1))
normal.addData3f(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1))
color.addData4f(1.0, 1.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)
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 build(self,pt1, pt2=[]):
cs = 0.2
if pt1[1] == pt2[1]:
# wall is left right
if pt1[0] - pt2[0] < 0:
p1, p2 = pt1, pt2
pt1 = p2
pt2 = p1
main1pt = [pt1[0], pt1[1] - cs / 2, 0]
main1pt1 = [pt2[0], pt2[1] + cs / 2, 0]
main1pt2 = [pt1[0], pt1[1] + cs / 2, 3]
main1pt3 = [pt2[0], pt2[1] - cs / 2, 3]
main2pt = [pt2[0], pt2[1] + cs / 2, 3]
main2pt1 = [pt1[0], pt1[1] - cs / 2, 3]
main2pt2 = [pt2[0], pt2[1] - cs / 2, 0]
main2pt3 = [pt1[0], pt1[1] + cs / 2, 0]
if pt1[0] == pt2[0]:
# wall is up down
main1pt = [pt1[0] - cs / 2, pt1[1], 0]
main1pt1 = [pt2[0] + cs / 2, pt2[1], 0]
main1pt2 = [pt1[0] + cs / 2, pt1[1], 3]
main1pt3 = [pt2[0] - cs / 2, pt2[1], 3]
main2pt = [pt2[0] + cs / 2, pt2[1], 3]
main2pt1 = [pt1[0] - cs / 2, pt1[1], 3]
main2pt2 = [pt2[0] - cs / 2, pt2[1], 0]
main2pt3 = [pt1[0] + cs / 2, pt1[1], 0]
else:
rotated = True
xdiff = pt1[0] - pt2[0]
ydiff = pt1[1] - pt2[1]
newx = pt1[0] - math.sqrt(abs(xdiff) ** 2 + abs(ydiff) ** 2)
angle = math.degrees(math.atan(ydiff / xdiff))
main1pt = [pt1[0], pt1[1] - cs / 2, 0]
main1pt1 = [newx, pt1[1] + cs / 2, 0]
main1pt2 = [pt1[0], pt1[1] + cs / 2, 3]
main1pt3 = [newx, pt1[1] - cs / 2, 3]
main2pt = [newx, pt1[1] + cs / 2, 3]
main2pt1 = [pt1[0], pt1[1] - cs / 2, 3]
main2pt2 = [newx, pt1[1] - cs / 2, 0]
main2pt3 = [pt1[0], pt1[1] + cs / 2, 0]
square0 = self.makeSquare(*(main1pt + main1pt1))
square1 = self.makeSquare(*(main1pt + main1pt2))
square2 = self.makeSquare(*(main1pt + main1pt3))
square3 = self.makeSquare(*(main2pt + main2pt1))
square4 = self.makeSquare(*(main2pt + main2pt2))
square5 = self.makeSquare(*(main2pt + main2pt3))
snode = GeomNode('')
snode.addGeom(square0)
snode.addGeom(square1)
snode.addGeom(square2)
snode.addGeom(square3)
snode.addGeom(square4)
snode.addGeom(square5)
cube = render.attachNewNode(snode)
cube.setTwoSided(True)
cube.setColor(1,0,0,1)
return cube
core = Core()
core.run()
I understand from previous posts that I need to reset the origin of the object as it is always at world 0,0