4 Replies Latest reply on May 29, 2006 6:45 AM by Mazuho

    Contrain PointAt to z-axis of object

    Mazuho Level 1
      I'm trying to use the point at method but I only want the model to point at the target with one axis. How can I do this?

        • 1. Re: Contrain PointAt to z-axis of object
          Mazuho Level 1
          I solved it partly, by setting the .x and .y parameters of object.transform.rotation to 0.

          But it still doesn't work just right... When I drive past the object it gets rotated 180 degrees in z, resulting in the back pointing at the target instead of the front.

          Anyone experienced the same?
          • 2. Re: Contrain PointAt to z-axis of object
            James Newton, ACP Level 3
            Have you looked at the pointAtOrientation property of 3D objects?

            Alternatively you can do some 3D mathematics, and rotate the model by hand. To rotate your model, you can use the syntax:

              myModel.rotate(vPosition, vAxis, vAngle, #world)

            You'll need to work out vAxis and vAngle from the current orientation of the model:

            vPosition = myModel.worldPosition
            vZAxis = myModel.getWorldTransform().zAxis -- current

            vPointToPointAt = <some vector>
            vDirection - vPointToPointAt - vPosition -- desired

            vAxis = vZAxis.cross(vDirection)
            vAngle = angleBetween(vZAxis, vDirection)

            If vAxis has a magnitude of 0, but vAngle is not 0, then your model is pointing in diametrically the opposite direction: vAngle will be 180. Rotating your model about any axis of rotation will make it point in the right direction, but it will probably be tilted or upside down. You will need to choose your rotatition vector with care to make sure that your model ends up the right way up.
            • 3. Re: Contrain PointAt to z-axis of object
              nick_kang Level 1
              I usually use a utility node (in this case, a SW3D group) for something like this:

              -- limit pointAt() to y-axis rotation
              -- assumptions:
              -- +y is Up
              -- pointAt() orients nodes along local negative z-axis
              gUtilGroup.transform.position = pEntityModel.transform.position
              gUtilGroup.transform.position.y = pTargetPosition.y
              gUtilGroup.pointAt(pTargetPosition, gUp)
              pEntityModel.transform.rotation = gUtilGroup.transform.rotation

              -- global dependencies (need to be defined during scene setup):
              gUtilGroup = gSW3D.newGroup("UtilGroup")
              gUp = vector(0,1,0)

              Another thing to watch out for, pointAt() will generate an "Invalid Point" error if the node's position coincides with the target position.

              • 4. Contrain PointAt to z-axis of object
                Mazuho Level 1
                Thank you guys, you really helped me out.

                Edit: To Openspark: Yes, I checked the pointAtOrientation but to no avail. I experimented with it but couldn't get it to work.