Search code examples
rotationquaternionseuler-anglespanda3d

Panda3D: How to rotate object so that its X-axis points to a location in space?


I have an object (say fromObj) stored in NodePath at a 3D point location (say fromPoint). Its heading-pitch-roll (HPR) is (0,0,0). I want to rotate it such that its X-axis points to another point toPoint in space. I just want to compute the HPR that achieves this rotation and use it for some other purpose.

I tried fromObj.lookAt(toPoint), but this points its Y-axis at toPoint. I want its X-axis to point at toPoint.

How do I compute the HPR that will rotate an object such that its X-axis points to a given location in space?

Note: I am aware of this question on StackOverflow: Calculate rotations to look at a 3D point? However, I am looking for a solution that uses existing Panda3D APIs and I want the result in the Panda3D HPR format.


Solution

  • Thanks to the helpful folks on Panda3D forums, I got an easier solution:

    def getHprFromTo( fromNP, toNP ):
        # Rotate Y-axis of *from* to point to *to*
        fromNP.lookAt( toNP ) 
    
        # Rotate *from* so X-axis points at *to*
        fromNP.setHpr( fromNP, Vec3( 90, 0, 0 ) )
    
        # Extract HPR of *from* 
        return fromNP.getHpr()
    

    If you only have point locations, create dummy nodePaths for the two points to do this calculation.