Calculate angle of rotation?

Software: Away3D 3.x

wesleysnipes, Newbie
Posted: 08 November 2011 02:25 AM   Total Posts: 20   [ # 16 ]

Wow Somokon!!!! This works! If you can, could you explain how this works? What does setting a new matrix do?!

Thanks so much for making this work! Huge huge help!

Thanks!

   

wesleysnipes, Newbie
Posted: 08 November 2011 04:53 AM   Total Posts: 20   [ # 17 ]

Sorry, I spoke too soon. This solution works great at first site, but I should have mentioned I need to tween this rotation. So it can’t get reset each time to 0,0.

Isn’t there a way to make this work with rotationX and rotationY?

When I try to apply the theta and phi, it is totally off.

I’m so confused as to how this works and why the rotate works but not rotateTo or rotationX and rotationY.

Is it just me or is this stuff really confusing?!?

I feel like what I’m trying to do is so utterly basic that it seems like I’m missing something super basic here.

I simply need to orbit to a spot on a sphere. Camera needs to look at a specific spot on a sphere. Please gods of away3d, reach down and throw this mere mortal a bone and some godly knowledge.

   

Stephen Hopkins, Sr. Member
Posted: 08 November 2011 05:13 AM   Total Posts: 110   [ # 18 ]
wesleysnipes - 07 November 2011 05:44 PM

Thanks for the reply. Really struggling with this. I’ve been digging around all night and this morning.
I cannot seem to get this thing to spin correctly. Any ideas? I’ve also tried to move the hover cam, same thing. It always goes to the wrong spot. I also found a different post on this forum with a slightly different spherical formula, the one here is using whats on the wiki.

private var _container ObjectContainer3D;
private var 
_sphere Sphere

...(Not showing, but I add this sphere to an ObjectContainer3D and setup the scene)...

var targetVector3D Vector3D _sphere.vertices[0].position;
var 
r:Number targetVector3D.length;
var 
theta:Number Math.acos(targetVector3D.r);
var 
phi:Number Math.atan2(targetVector3D.ytargetVector3D.x); 

...(I also create a test cube and place it at the vert just to visually confirm I have the correct location)...

//Spin it
_container.rotationY theta/(Math.PI/180);
_container.rotationX phi/(Math.PI/180); 

Instead of setting rotationY and rotationX, i thought you were using the hover camera?
theta is your tilt value, phi is your pan.

 Signature 

http://www-scf.usc.edu/~shopkins

   

Stephen Hopkins, Sr. Member
Posted: 08 November 2011 05:24 AM   Total Posts: 110   [ # 19 ]
wesleysnipes - 08 November 2011 04:53 AM

Sorry, I spoke too soon. This solution works great at first site, but I should have mentioned I need to tween this rotation. So it can’t get reset each time to 0,0.

Isn’t there a way to make this work with rotationX and rotationY?

When I try to apply the theta and phi, it is totally off.

I’m so confused as to how this works and why the rotate works but not rotateTo or rotationX and rotationY.

Is it just me or is this stuff really confusing?!?

I feel like what I’m trying to do is so utterly basic that it seems like I’m missing something super basic here.

I simply need to orbit to a spot on a sphere. Camera needs to look at a specific spot on a sphere. Please gods of away3d, reach down and throw this mere mortal a bone and some godly knowledge.

rotationX and rotationY are kind of weird properties and are calculated based off the current orientation of the object and I believe that these properties do not care how they got to the values they have. For example, if rotationY is already 45 degrees, and you start changing rotationX, I am unsure if rotationY or rotationZ will also be affected. For this reason, and partly because I am paranoid, I reset the rotations back to 0 every time i rotate something in more than one axis. You can still reset your rotations to 0 every while tweening.

assuming you are abandoning the hover camera, which I think actually works fine for your problem:

matrix.identity();
matrix.appendRotation(Vector3D.Y_AXIS, tweenPhiValue);
matrix.appendRotation(Vector3D.X_AXIS, tweenThetaValue);
container.transform = matrix;

I may have the syntax wrong since I haven’t used away3d in a while.

Are you trying to rotate the sphere to face towards a camera located in the positive z direction? or are you trying to hover the camera around the sphere towards a particular point. Either way, I think the ideas are similar. Sorry if I haven’t been much help.

 Signature 

http://www-scf.usc.edu/~shopkins

   

wesleysnipes, Newbie
Posted: 08 November 2011 09:07 PM   Total Posts: 20   [ # 20 ]

Hey thanks again for the help everyone.

I ended up going a different way all together.

Not dealing with rotation values at all. Instead, I move the camera to the location and on tween update, I normalize the position and scale it back again to a fixed distance. On render I re look at the center again.

Here’s the working example and code for anyone facing the same issue:

Working Example

package
{
 import away3d
.cameras.Camera3D;
 
import away3d.containers.ObjectContainer3D;
 
import away3d.containers.Scene3D;
 
import away3d.containers.View3D;
 
import away3d.loaders.Obj;
 
import away3d.primitives.Cube;
 
import away3d.primitives.Sphere;
 
import away3d.test.Button;
 
 
import com.demonsters.debugger.MonsterDebugger;
 
import com.greensock.TweenMax;
 
import com.greensock.core.PropTween;
 
 
import flash.display.Sprite;
 
import flash.events.Event;
 
import flash.events.MouseEvent;
 
import flash.geom.Matrix3D;
 
import flash.geom.Vector3D;
 
 
[SWF(width='600'height='800'backgroundColor='#e3e3e3'frameRate='30')]
 
public class TestOrbit extends Sprite
 {
  
  
//--------------------------------------
  //  PROPS
  //--------------------------------------
  
private var _view View3D;
  private var 
_scene Scene3D;
  private var 
_camera Camera3D;
  
  private var 
_container ObjectContainer3D;
  private var 
_sphere Sphere;
  
  private var 
_testButton Button;
  private var 
_lastVerticeIndex int 0;
  
  private var 
_cameraDistance Number 700;
  
  
//Stores the temporary tween values of the camera position
  
private var _cameraPosition Vector3D;
  
  
//--------------------------------------
  //  PUBLIC METHODS
  //--------------------------------------
  
public function TestOrbit()
  
{
   MonsterDebugger
.initialize(this);
   
   
setupScene();
   
createObjects();
   
createUI();
  
}
  
  
//--------------------------------------
  //  PRIVATE METHODS
  //--------------------------------------
  
private function setupScene () : void
  {
   _view 
= new View3D();
   
_scene _view.scene;
   
_camera _view.camera;
   
_cameraPosition _camera.position;
   
_cameraPosition.normalize();
   
_cameraPosition.scaleBy(_cameraDistance);
   
_camera.position _cameraPosition;
   
   
addChild(_view);
   
   
_view.stage.stageWidth/2;
   
_view.stage.stageHeight/2;
   
   
this.addEventListener(Event.ENTER_FRAMEticHandler);
  
}
  
  
private function createObjects () : void
  {
   _container 
= new ObjectContainer3D();
   
_scene.addChild(_container);
   
   
_sphere = new Sphere({radius:100});
   
_container.addChild(_sphere);
  
}
  
  
private function createUI () : void
  {
   _testButton 
= new Button("Generate Cube!"115);
   
_testButton._testButton.10;
   
_testButton.addEventListener(MouseEvent.CLICK_testButtonClickedHandler);
   
addChild(_testButton);
  
}
  
  
private function generateCube () : void
  {
   _cameraPosition 
_camera.position;
   
//Get a point on the spehere
   
var nextPosition Vector3D _sphere.vertices[_lastVerticeIndex].position;
   
   
//Generate new cube and add it
   
var cube Cube = new Cube({width:10height:10depth:10});
   
cube.position nextPosition;
   
_container.addChild(cube);
   
_lastVerticeIndex++;
   
   
TweenMax.to(_cameraPosition1{x:nextPosition.xy:nextPosition.yz:nextPosition.zonUpdate:rePositionCamera});
  
}
 
  
private function rePositionCamera () : void
  {
   _cameraPosition
.normalize();
   
_cameraPosition.scaleBy(_cameraDistance);
   
_camera.position _cameraPosition;
  
}
  
  
//--------------------------------------
  //  HANDLER METHODS
  //--------------------------------------
  
private function ticHandler Event ) : void
  {
   _camera
.lookAt(new Vector3D());
   
_view.render();
  
}
  
  
private function _testButtonClickedHandler MouseEvent ) : void
  {
   generateCube
();
  
}
 }
   

OneCoolBlerd, Newbie
Posted: 22 November 2011 05:35 PM   Total Posts: 3   [ # 21 ]

Thanks wesleysnipes. I am currently facing a similar problem. This will help alot.

   

wesleysnipes, Newbie
Posted: 22 November 2011 06:32 PM   Total Posts: 20   [ # 22 ]

Cool I’m glad it helped. I actually ended up creating a new camera.

Might help as well. In case you want to use it as is, just grab the green sock and asSignals packages.

Good luck

package com.camera
{
 import away3d
.cameras.Camera3D;
 
import away3d.core.render.Renderer;
 
 
import com.greensock.TimelineMax;
 
import com.greensock.TweenMax;
 
import com.greensock.core.TweenCore;
 
 
import flash.geom.Vector3D;
 
 
import org.osflash.signals.Signal;
 
 
/**
  * This is a custom camera that will orbit around an object at a fixed distance. 
  * 
  * 
  */
 
public class OrbitCamera extends Camera3D
 {
  
  
//--------------------------------------
  //  SIGNALS
  //--------------------------------------
  
public var focusStartedSignal Signal = new Signal();
  public var 
focusCompletedSignal Signal = new Signal();
  
  
//--------------------------------------
  //  PRIVATE VARIABLES
  //--------------------------------------
  
private var _proxyPosition Vector3D;
  private var 
_defaultDistance Number 300;
  private var 
_zoomedDistance Number 250;
  private var 
_nextTarget Vector3D;
  
  
//--------------------------------------
  //  PUBLIC METHODS
  //--------------------------------------
  /**
   * Constructor 
   * @param init
   * 
   */
  
public function OrbitCamera(init:Object=null)
  
{
   super
({x:0y:0z:-_defaultDistancelookat:"center"});
  
}
  
  
/**
   * Focus on a Vector3D. Will orbit to the point at a fixed distance
   * @param p_vector3D The point to focus on
   * 
   */  
  
public function focusVector p_vector3D Vector3D ) : void
  {
   focusStartedSignal
.dispatch();
   
   
_nextTarget p_vector3D;
   
   
//Start the tween. Each state block will handle calling the next step
   
if(position.length == _defaultDistance){
    gotoPointTween
();
   
}else{
    gotoDefaulTween
();
   
}
  }
  
  
//--------------------------------------
  //  ACCESSOR/MUTATOR METHODS
  //--------------------------------------
  /**
   * The default distance of the camera from the center 
   * @return 
   * 
   */
  
public function get defaultDistance() : Number 
  {
   
return _defaultDistance;
  
}
  
  
public function set defaultDistancep_defaultDistance Number ) : void
  {
   _defaultDistance 
p_defaultDistance;
  
}
  
  
/**
   * The zoomed distance of the camera 
   * @return 
   * 
   */  
  
public function get zoomedDistance() : Number 
  {
   
return _zoomedDistance;
  
}
  
  
public function set zoomedDistancep_zoomedDistance Number ) : void
  {
   _zoomedDistance 
p_zoomedDistance;
  
}
  
  
//--------------------------------------
  //  PRIVATE METHODS
  //--------------------------------------
  /*
  * In order to orbit the camera correctly, we normalize the position 
  * proxy <code>_cameraPosition</code> and scale up the vector to the fixed distance.
  */
  
private function rePositionCamera () : void
  {
   _proxyPosition
.normalize();
   
_proxyPosition.scaleBy(_defaultDistance);
   
   
position _proxyPosition;
  
}
  
  
private function reAlignCameraTarget () : void
  {
   lookAt
(new Vector3D()); 
  
}
  
  
//Step 1 in the tween. Goes back out to default state
  
private function gotoDefaulTween () : void
  {
   
var defaultPosition Vector3D position.clone();
   
defaultPosition.normalize();
   
defaultPosition.scaleBy(_defaultDistance);
   
TweenMax.to(this2{
    x
:defaultPosition.x
    
y:defaultPosition.y
    
z:defaultPosition.z
    
onUpdate:gotoDefaultTweenUpdate
    
onComplete:gotoDefaultTweenComplete});
  
}
  
  
//Step 2 in the tween. Orbits to the point
  
private function gotoPointTween () : void
  {
   _proxyPosition 
position.clone();
   
   
TweenMax.to(_proxyPosition2{
    x
:_nextTarget.x
    
y:_nextTarget.y
    
z:_nextTarget.z
    
onUpdate:gotoPointTweenUpdate,
    
onComplete:gotoPointTweenCompleted})
  
}
  
  
//Step 3 in tween. Zooms in on the point
  
private function focusPointTween () : void
  {
   
//Use Z-Sorting Renderer
   //this.view.renderer = Renderer.CORRECT_Z_ORDER;
   
   
var zoomedPosition Vector3D position.clone();
   
zoomedPosition.normalize();
   
zoomedPosition.scaleBy(_zoomedDistance);
   
TweenMax.to(this2{
    x
:zoomedPosition.x
    
y:zoomedPosition.y
    
z:zoomedPosition.z,
    
onUpdate:focusPointTweenUpdate,
    
onComplete:focusPointTweenCompleted});
  
}
  
  
//--------------------------------------
  //  HANDLER METHODS
  //--------------------------------------
  
  //Tween update handlers
  
private function gotoDefaultTweenUpdate () : void
  {
   reAlignCameraTarget
();
  
}
  
  
private function gotoPointTweenUpdate () : void
  {
   rePositionCamera
();
   
reAlignCameraTarget();
  
}
  
  
private function focusPointTweenUpdate () : void
  {
   reAlignCameraTarget
();
  
}
  
  
//Tween compelted handlers
  
private function gotoDefaultTweenComplete () : void
  {
   gotoPointTween
();
  
}
  
  
private function gotoPointTweenCompleted () : void
  {
   focusPointTween
();
  
}
  
  
private function focusPointTweenCompleted () : void
  {
   focusCompletedSignal
.dispatch();
  
}
  
 }
   

OneCoolBlerd, Newbie
Posted: 22 November 2011 07:39 PM   Total Posts: 3   [ # 23 ]

I will definitely check this out. The project that I am working on needs to have a gyroscope effect. I am trying to create sphere for that portion and then overlay with some static 2D image. However, the rotation will be from using a joystick-like interface so I need to figure out the angles and how to restrict them. So that only a few the portion of the sphere I want to show through overlay will be rotated. It’s almost like an attitude indicator on an airplane. I’m even newer than a newbie (just started using away3d last week haha). So any help will be much appreciated.

   

wesleysnipes, Newbie
Posted: 22 November 2011 08:00 PM   Total Posts: 20   [ # 24 ]

Hmm, I think I understand what you mean. So there will be like a 2d non away3d overlay that you peek through almost? The sphere will spin and wobble. Almost like if you were in a ship with a round window looking at the sphere as you rotate around it? The ship being controlled by the joystick like device.

If this is at all correct, it might be worth trying to move the sphere itself, or the container that the sphere is inside of. This way, you can keep the 2d piece outside of the away3D world and not have to deal with any of that logic.

Keep the camera fixed and move the sphere. The only question is, and I’m sure that’s why you are looking of the forums, is what type of rotation and wobbling does your sphere need to do?

Hopefully it’s as simple as:

private function enterFrameHandler Event ) : void
{
  _sphere
.yaw(1);

  
//Add some type of yoyo oscillation here for the pitch.
  
_sphere(1);

  
view.render();

If the roll if what you need to target, you can maybe do something with a tweenmax or timelinemax instance where you yoyo a value and use that value for your roll. If you want the full gyro effect, guess you could add _sphere.pitch(value) in there as well.

Hopefully this helps.

   

OneCoolBlerd, Newbie
Posted: 22 November 2011 08:14 PM   Total Posts: 3   [ # 25 ]

Yes!! That’s exactly what I am trying to do. I want the sphere to pitch, roll, and yaw. The 2d overlay should only rotate clockwise or counterclockwise if needed. I guess it is more like a gyroscope now that I’m thinking about it. Is there a gyroscope extension as well?? Because I didn’t know yaw, roll, and pitch were actual functions in the library. Thanks, again.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X