Here is an example code, of how to enable mouse and keyboard interaction within your papervision actionscript file.
I know I was lazy, and have not put any comments in the source code, however if you need any extra help or explanations, please leave a comment, and I WILL get back to you
May promise a better full tutorial later on….
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.geom.Point;
//Import Camera, Scene, etc..
import org.papervision3d.view.BasicView;
//Mouse Movements
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
//rotation calculations
import org.papervision3d.core.math.Matrix3D;
import org.papervision3d.core.math.Number3D;
/*
*@Author: Thierry Zoghbi
*@Date: 2009.
*/
[SWF(width="800", height="600", backgroundColor="#0066F", frameRate="25")]
public class Mainsack extends BasicView
{
private var container : DisplayObject3D;
private var previousMousePoint : Point = new Point();
private var targetScale : Number = 1;
private static const FORWARD:Number3D = new Number3D(0, 0, 1);
public function Mainsack()
{
var yourobject = new Collada("yourDAEfile.dae");
this.stage.doubleClickEnabled = true;
this.stage.addEventListener( MouseEvent.DOUBLE_CLICK, onDoubleClick );
this.stage.addEventListener( MouseEvent.MOUSE_DOWN, whenMouseDown );
this.stage.addEventListener( MouseEvent.MOUSE_WHEEL, whenMouseWheel );
this.stage.addEventListener( KeyboardEvent.KEY_DOWN, whenKeyDown );
addChild(yourobject);
resetView();
}
private function whenKeyDown( event : KeyboardEvent ) : void
{
trace( event.keyCode );
switch ( event.keyCode )
{
case 187: //
case 107: //
zoom( 3 );
break;
case 189: // –
case 109: // –
zoom( -3 );
break;
case 82: // r (reset)
resetView();
break;
}
}
private function whenMouseWheel( event : MouseEvent ) : void
{
zoom( event.delta );
}
private function onDoubleClick( event : MouseEvent ) : void
{
zoom( 10 );
}
private function whenMouseDown( event : Event ) : void
{
this.startRendering();
previousMousePoint = new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
this.stage.addEventListener( MouseEvent.MOUSE_MOVE, whenMouseMove );
this.stage.addEventListener( MouseEvent.MOUSE_UP, whenMouseUp );
}
private function whenMouseUp( event : Event ) : void
{
this.stopRendering();
this.stage.removeEventListener( MouseEvent.MOUSE_MOVE, whenMouseMove );
this.stage.removeEventListener( MouseEvent.MOUSE_UP, whenMouseUp );
}
private function whenMouseMove( event : Event ) : void
{
var currentMousePoint:Point = new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
var difference:Point = currentMousePoint.subtract(previousMousePoint);
var vector:Number3D = new Number3D(difference.x, difference.y, 0);
var rotationAxis:Number3D = Number3D.cross(vector, FORWARD);
rotationAxis.normalize();
var distance:Number = Point.distance(currentMousePoint, previousMousePoint);
var rotationMatrix:Matrix3D = Matrix3D.rotationMatrix(rotationAxis.x, -rotationAxis.y, rotationAxis.z, distance/(600*Math.pow(container.scale, 5)));
container.transform.calculateMultiply3x3(rotationMatrix, container.transform);
//this line used to apply transform to actual rotation values, so that if you change scale, the changes are persisted
container.copyTransform(container);
previousMousePoint = currentMousePoint
trace( container.rotationX, container.rotationY, container.rotationZ, container.scale );
}
private function zoom( delta : Number ) : void
{
targetScale = targetScale (delta * .01);
targetScale = Math.max( targetScale, .5 );
targetScale = Math.min( targetScale, 1.6 );
Tweener.addTween( this, {sceneScale:targetScale, time:1, transition:"easeOutQuart"} )
}
public function resetView() : void
{
Tweener.addTween( container, {time:3, rotationX:0, rotationY:0, rotationZ:0, transition:"easeOutQuart"} )
//Tweener.addTween( container, {time:3, rotationX:-45, rotationY:0, rotationZ:0, transition:"easeOutQuart"} )
Tweener.addTween( this, {sceneScale:1, time:3, transition:"easeOutQuart"} )
}
public function set sceneScale( value : Number ) : void
{
container.scale = value;
singleRender();
}
public function get sceneScale() : Number
{
return container.scale;
}
}
}
I am getting a syntax error on this line:
container.transform.calculateMultiply3×3(rotationMatrix, container.transform);
Great tutorials other than that! Thanks
what is the error exactly?
it says
1084: Syntax error: expecting rightbrace before 3.
make sure you dont have any extra brackets in your code either before or after the line.
as this sometimes can cause a problem. also make sure you have your semicolons put in the right places!
the code above should work as i have used it my self, just try double checking your code.
the code i am using IS your code. the only thing i changed was the dae file name. This stuff always happens to me haha!
Same problem I’m having now…
I have just viewed your live example where you can rotate the dae file, zoom in and out etc and I love it. I want to create a similar thing but I just tried this tutorial and Im getting an error on line 100…
1084: Syntax error: expecting rightbrace before 3.
1093: Syntax error.
Please help me out mate. Thanks
That syntax error is a basic problem with semicolons or other mistypes.
This is the annoying part in checking and rechecking your code for grammatical errors…