render coord to aspect2d

Hey all,

I know this issue has been covered quite a bit, but I’m still having a few issues converting a 3d point to aspect2d coords (see [url]Mapping a point in render to aspect2d]).

This works fine most of the time, unless the window is resized so that it is taller than it is wide. I thought some sort of easy hack like taking the aspect y value and multiplying by[1] / 30 would work, but it doesn’t seem to completely solve the issue.

Am I trying something not supported, or is there an easy fix for this? Any help appreciated!

It seems to me that either code snippet in that thread should work regardless of the aspect ratio of the window. Can you give more information?


Hopefully this snippet will illustrate what I’m seeing:

from direct.showbase.ShowBase import ShowBase, Point2, Point3
from direct.task import Task
from direct.directtools.DirectGrid import DirectGrid

base = ShowBase()

def Update( renderNp, aspectNp ):
    # Convert the point to the 3-d space of the camera
    p3 = render, renderNp.getPos() )
    # Convert it through the lens to render2d coordinates
    rp2 = Point2()
    if base.camLens.project( p3, rp2 ):
        ap2 = aspect2d.getRelativePoint( render2d, Point3(rp2[0], rp2[1], 0)  ) 
        aspectNp.setPos( ap2.x, 0, ap2.y )
    return Task.cont
grid = DirectGrid( parent=render, planeColor=(0.5, 0.5, 0.5, 0.5) )
box1Np = loader.loadModel( 'box.egg.pz' )
box1Np.reparentTo( render )
box1Np.setColor( 1, 0, 0, 0 )
box1Np.setPos( 0, 0, 30 )
box1Np.setScale( 4 )
box2Np = loader.loadModel( 'box.egg.pz' )
box2Np.reparentTo( aspect2d )
box2Np.setColor( 0, 0, 1, 0 )
box2Np.setScale( .05 )
taskMgr.add( Update, 'task', extraArgs=[box1Np, box2Np] )

base.disableMouse() 0, -150, 0 )

The red box is in render space while the blue box is in aspect2d space. They overlap when the window is resized, until the window is taller than it is wide - then they drift apart. Is my implementation off?

Using Panda 1.7.2 Win7.

You are scaling by the y coordinate in aspect2d, but the vertical coordinate is the z coordinate. Replace the middle code with this:

    if base.camLens.project( p3, rp2 ):
        ap2 = aspect2d.getRelativePoint( render2d, Point3(rp2[0], 0, rp2[1])  )
        aspectNp.setPos( ap2.x, 0, ap2.z )


Ack! Should have spotted that one. Thanks David!!