简体   繁体   中英

AS3 Move and Rotate a object to a point in 3D space

Hello,

I trying to move a object in stage 3D (I'm using away3d framework) to a point in space (vector 3d), its fine and working, but I want that object turn it "front face" to that point, so it will be aways "looking" and "walk" to it.

The code that move the object:

Tweener.addTween(
  mainReference.carHolder, 
  { 
    time:.1, 
    x:points[point].x, 
    y:points[point].y, 
    z:points[point].z, 
    transition:"Linear", 
    onComplete:function():void 
    { 
      moveCar(point + 1); 
    } 
  });

What I already try:

var angleX:Number = Vector3D.angleBetween(
  new Vector3D(
    mainReference.carHolder.x, 
    mainReference.carHolder.y, 
    mainReference.carHolder.z), 
  new Vector3D(
    mainReference.carHolder.x, 
    points[point].y, 
    points[point].z)
) * 180 / Math.PI;

var angleY:Number = Vector3D.angleBetween(
  new Vector3D(
    mainReference.carHolder.x, 
    mainReference.carHolder.y, 
    mainReference.carHolder.z), 
  new Vector3D(
    points[point].x, 
    mainReference.carHolder.y, 
    points[point].z)
) * 180 / Math.PI;

var angleZ:Number = Vector3D.angleBetween(
  new Vector3D(
    mainReference.carHolder.x, 
    mainReference.carHolder.y, 
    mainReference.carHolder.z), 
  new Vector3D(
    points[point].x, 
    points[point].y, 
    mainReference.carHolder.z)
) * 180 / Math.PI;

var angle:Number = Vector3D.angleBetween(
  new Vector3D(
    mainReference.carHolder.x, 
    mainReference.carHolder.y, 
    mainReference.carHolder.z), 
  new Vector3D(
    points[point].x, 
    points[point].y, 
    points[point].z)
) / 180 * Math.PI;

mainReference.carHolder.eulers = new Vector3D(
  points[point].x, 
  points[point].y, 
  points[point].z);
mainReference.carHolder.rotateTo(
  points[point].x, 
  points[point].y, 
  points[point].z);

Have you tried the lookAt() method?

From the docs:


lookAt()
public function lookAt(target:Vector3D, upAxis:Vector3D = null):void Rotates the 3d object around to face a point defined relative to the local coordinates of the parent ObjectContainer3D.

Parameters

target:Vector3D — The vector defining the point to be looked at

upAxis:Vector3D (default = null) — An optional vector used to define the desired up orientation of the 3d object after rotation has occurred


This should allow you to have your object look at the target position while traveling to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM