Ok, I have a 3D cursor that shows up where the cursor ray collides with a mesh. It works real swell, but now I want to constrain the max distance that the 3D cursor can travel from the camera.
So basically,
If 3D cursor distance > bound:
Relocate to distance equal to bound
Else:
Set location to collision point as usual
Here is the code and I’m pretty sure that theoretically, it should work. I don’t know any simple 3D formulas, so I calculate the hypotenuse of the triangle in the XY plane plus it’s angle, then calculate the angle for the “HZ” (H=hypotenuse of tri. in XY plane) triangle using the Hypotenuse of the XY triangle. I then use that information and calculate the new coordinates of the 3D cursor within the bound.
Right now, the cursor pretty much disappears from the view when it passes the bound…
Here is my script:
def mousePlacer(self,task):
bound=15 #Max dist of 3D cursor from camera
mpos=base.mouseWatcherNode.getMouse()
pickerRay.setFromLens(base.camNode,
mpos.getX(),
mpos.getY())
myTraverser.traverse(render)
if myHandler.getNumEntries() > 0:
collisionPoint = myHandler.getEntry(0).getSurfacePoint(render)
# Make x,y,z coordinates relative to cam pos
camPos = camera.getPos()
x=collisionPoint[0]-camPos[0]
y=collisionPoint[1]-camPos[1]
z=collisionPoint[2]-camPos[2]
h=math.sqrt(x**2+y**2) #Find hypotenuse in XY plane
aXY=math.atan(y/x) #Find angle in XY plane
aHZ=math.atan(z/h) #Find angle on HZ plane
distCursor=math.sqrt(x**2+y**2+z**2)
#Calculate new coordinates within 'bound' from cam
if distCursor>bound:
z=bound*math.sin(aHZ)
h=bound*math.cos(aHZ)
y=h*math.sin(aXY)
x=h*math.cos(aXY)
self.axis.setPos(x,y,z)
else:
self.axis.setPos(collisionPoint[0],
collisionPoint[1],
collisionPoint[2])
return task.cont
I know this is my 3rd post in the last few days, but I honestly won’t post unless I have slayed over the issue for hours.