Baffled by the springCam

Software: Away3D 4.x

John Wilson, Member
Posted: 22 November 2011 01:08 PM   Total Posts: 62

I imagine I’m probably missing something obvious, but I just can’t get the springCam to work.

I’m using the latest GitHub download and the following code compiles but when I move the cube, the camera does not follow it.

It would really make my day if someone could give me a clue.

package {
 import away3d
.cameras.*;
 
import away3d.containers.View3D;
 
import away3d.core.base.Object3D;
 
import away3d.materials.ColorMaterial;
 
import away3d.primitives.*;

 
import flash.display.Sprite;
 
import flash.events.Event;
 
import flash.events.KeyboardEvent;
 
import flash.geom.Vector3D;
 
import flash.ui.Keyboard;

 
[SWF(backgroundColor="#000000"frameRate="60"width="640"height="480")]
 
 
public class SpringCamTest extends Sprite {
  
//
  
private var view View3D;
  private var 
springCam:SpringCam = new SpringCam(null);
  private var 
c:Cube = new Cube();

  public function 
SpringCamTest() {
   
if (stage{
    init
();
   
else {
    addEventListener
(Event.ADDED_TO_STAGEinit);
   
}
  }

  
private function init(Event null) : void {
   removeEventListener
(Event.ADDED_TO_STAGEinit);
   
view = new View3D();
   
view.antiAlias 4;
   
this.addChild(view);
   
   
springCam.positionOffset = new Vector3D(0,5,-50);
   
springCam.lookOffset = new Vector3D(0210);
   
view.camera springCam;
   
   
c.depth 100;
   
c.material = new ColorMaterial(0xff0000);
   
c.height 100;
   
c.width 100;
   
view.scene.addChild(c);
   
   
springCam.target c;
   
   
stage.addEventListener(KeyboardEvent.KEY_DOWNkeyDownHandler);
   
stage.addEventListener(Event.ENTER_FRAMEhandleEnterFrame);
  
}

  
private function keyDownHandler(event KeyboardEvent) : void {
   
switch(event.keyCode{
    
case Keyboard.UP:
     
c.+= 10;
     break;
    case 
Keyboard.DOWN:
     
c.-= 10;
     break;
    case 
Keyboard.LEFT:
     
c.-= 10;
     break;
    case 
Keyboard.RIGHT:
     
c.+= 10;
     break;
   
}
  }

  
  
private function handleEnterFrame(Event) : void {
   view
.render();
  
}[/size][/size]
  
 }
   

opie, Newbie
Posted: 22 November 2011 09:47 PM   Total Posts: 2   [ # 1 ]

I don’t know how you can tell if it’s being followed in this code as there’s no static reference point.  Anyway, that’s not your issue.  You just have the offset too close so your cam is springing into the center of your cube.

Try this instead;

springCam.positionOffset = new Vector3D(0,50,-150); 
   

Stephen Hopkins, Sr. Member
Posted: 22 November 2011 10:39 PM   Total Posts: 110   [ # 2 ]

I think this may be a bug. I recently updated my project’s away3d reference and my springcamera no longer follows target.

For whatever reason, the public override function get viewProjection():Matrix3D

only gets called once at the very beginning of my game, and then does nothing else. Not really sure what api changes went on behind the scenes,

Just did some searching, has SpringCam been officially removed? It’s still in the trunk, but it no longer seems to work. Richard made a comment about the new Controller System.

https://github.com/away3d/away3d-core-fp11/pull/57
https://github.com/away3d/away3d-core-fp11/tree/master/src/away3d/controllers

Are there plans to make a SpringCam Controller or whatever needs to be done? I would like to use the newest version of away3d but my project depends on the springcam, and reverting back requires fixing api changes (although not too many).

 Signature 

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

   

Stephen Hopkins, Sr. Member
Posted: 22 November 2011 11:49 PM   Total Posts: 110   [ # 3 ]

heres a temporary SpringCamController I made, although I was disappointed that my SpringCamController did not behave exactly the same as the old SpringCam… I had to adjust the stiffness and mass, but its still not quite the same :(

To use it, create a camera:Camera3D, and a target:Object3D

var springCamController:SpringCamController = new SpringCamController(camera);
springCamController.cameraTarget target;
springCamController.autoUpdate false;
//springCamController contains all the properties that the orignal SpringCam has, such as stiffness, damping, etc..
in your enterFrame Loop
springCamController
.update(); 
package{
 import away3d
.controllers.ControllerBase;
 
import away3d.core.base.Object3D;
 
import away3d.core.math.Matrix3DUtils;
 
import away3d.entities.Entity;
 
 
import flash.geom.Matrix3D;
 
import flash.geom.Vector3D;
 
 public class 
SpringCamController extends ControllerBase{
  
public var cameraTarget:Object3D;
  
/**
   * [optional] Target object3d that camera should follow. If target is null, camera behaves just like a normal Camera3D.
   */
  //spring stiffness
  /**
   * Stiffness of the spring, how hard is it to extend. The higher it is, the more "fixed" the cam will be.
   * A number between 1 and 20 is recommended.
   */
  
public var stiffness:Number 1;  
  
/**
   * Damping is the spring internal friction, or how much it resists the "boinggggg" effect. Too high and you'll lose it!
   * A number between 1 and 20 is recommended.
   */
  
public var damping:Number 4;
  
/**
   * Mass of the camera, if over 120 and it'll be very heavy to move.
   */
  
public var mass:Number 40
  
/**
   * Offset of spring center from target in target object space, ie: Where the camera should ideally be in the target object space.
   */
  
public var positionOffset:Vector3D = new Vector3D(0,5,-50);
  
  
/**
   * offset of facing in target object space, ie: where in the target object space should the camera look.
   */
  
public var lookOffset:Vector3D = new Vector3D(0,2,10);
  
  
//zrot to apply to the cam
  
private var _zrot:Number 0;
  
  
//private physics members
  
private var _velocity:Vector3D = new Vector3D();
  private var 
_dv:Vector3D = new Vector3D();
  private var 
_stretch:Vector3D = new Vector3D();
  private var 
_force:Vector3D = new Vector3D();
  private var 
_acceleration:Vector3D = new Vector3D();
  
  
//private target members
  
private var _desiredPosition:Vector3D = new Vector3D();
  private var 
_lookAtPosition:Vector3D = new Vector3D();
  
  
//private transformed members
  
private var _xPositionOffset:Vector3D = new Vector3D();
  private var 
_xLookOffset:Vector3D = new Vector3D();
  private var 
_xPosition:Vector3D = new Vector3D();
  
  private var 
_viewProjection Matrix3D = new Matrix3D();
  private var 
_up:Vector3D = new Vector3D();
  public function 
SpringCamController(targetObject:Entity=null){
   super
(targetObject);
   
  
}

  
public override function update():void{
   
if(cameraTarget != null){   
    _xPositionOffset 
cameraTarget.transform.deltaTransformVector(positionOffset);
    
_xLookOffset cameraTarget.transform.deltaTransformVector(lookOffset);
    var 
p:Vector3D cameraTarget.position;
    
_desiredPosition p.add(_xPositionOffset);
    
_lookAtPosition p.add(_xLookOffset);
    
    
_stretch this.targetObject.position.subtract(_desiredPosition);
    
_stretch.scaleBy(-stiffness);
    
_dv _velocity.clone();
    
_dv.scaleBy(damping);
    
_force _stretch.subtract(_dv);
    
    
_acceleration _force.clone();
    
_acceleration.scaleBy(1.0/mass);
    
_velocity _velocity.add(_acceleration);
    
    
_xPosition targetObject.position.add(_velocity);
    
targetObject._xPosition.x;
    
targetObject._xPosition.y;
    
targetObject._xPosition.z;

    
targetObject.lookAt(_lookAtPosition);
 
       
       if(
Math.abs(_zrot) > 0)
   
rotate(Vector3D.Z_AXIS_zrot);
    
   
}
  }
 }
 Signature 

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

   

John Wilson, Member
Posted: 23 November 2011 08:28 AM   Total Posts: 62   [ # 4 ]

Hi,

Thanks for the advice and and thanks very much for the sample code.  I was also a little confused as to whether springCam.as should still be in the trunk or not. 

   

John Wilson, Member
Posted: 23 November 2011 09:39 AM   Total Posts: 62   [ # 5 ]

Hi Stephen,

Sorry to be a pain I can only get your SpringCamController to work if I comment out the call to rotate() as follows:

if(Math.abs(_zrot) > 0){
 
//rotate(Vector3D.Z_AXIS, _zrot);

If I don’t comment it out I get: “Error: Call to a possibly undefined method rotate.”

   

John Wilson, Member
Posted: 24 November 2011 08:37 AM   Total Posts: 62   [ # 6 ]

I seem to have fixed the problem by changing the call to rotate() from:

rotate(Vector3D.Z_AXIS_zrot); 

to

targetObject.rotate(Vector3D.Z_AXIS_zrot); 

Hooray!

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X