這次使用PV3D Cube來製作簡單的加速與減速效果。
還是要來聊一下簡單的物理
一個靜止的物件, 不斷加上一個等加速度
這個物件就會以直線的方式運動。
EX:
var vx:int = 2
enterFrame運算 {
球.x += vx;
}
這樣球物件隨著時間的增加, 就一直往x軸的方向每次移動2個值
如果要快一點, 就把vx速率調高
那如果要減速呢?
只要在速率乘上一個衰竭值
EX:
var vx:int = 100
vx *= 0.98
enterFrame運算 {
球.x += vx;
}
隨著時間的增加, 速率就會越來越小, 直到靜止
好,那我們把這樣的公式丟到PV3D裡的Cube裡


完整程式碼
還是要來聊一下簡單的物理
一個靜止的物件, 不斷加上一個等加速度
這個物件就會以直線的方式運動。
EX:
var vx:int = 2
enterFrame運算 {
球.x += vx;
}
這樣球物件隨著時間的增加, 就一直往x軸的方向每次移動2個值
如果要快一點, 就把vx速率調高
那如果要減速呢?
只要在速率乘上一個衰竭值
EX:
var vx:int = 100
vx *= 0.98
enterFrame運算 {
球.x += vx;
}
隨著時間的增加, 速率就會越來越小, 直到靜止
好,那我們把這樣的公式丟到PV3D裡的Cube裡

完整程式碼
/*
Copyright (c) 2009 milkmidi
All rights reserved.
http://milkmidi.com
http://milkmidi.blogspot.com
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import org.papervision3d.cameras.CameraType;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shadematerials.PhongMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.view.BasicView;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.events.InteractiveScene3DEvent;
[SWF(width = "950", height = "500", frameRate = "41", backgroundColor = "#111111", pageTitle = "milkmidi.com")]
public class PV3DCubeRotationDemo extends BasicView
{
private var rootNode :DisplayObject3D;
private var cube :Cube;
private var vx :Number = 0;
private var vy :Number = 0;
//速率
private var decay :Number = .98;
//衰竭值
private var mousePos :Point = new Point();
private var isMouseDown :Boolean = false;
public function PV3DCubeRotationDemo(){
super(0, 0, true, true, CameraType.TARGET);
camera.z = -950;
camera.focus = 12;
init3DObjects();
this.addEventListener(Event.ADDED_TO_STAGE , addToStageHandler);
}
private function addToStageHandler(e:Event):void {
startRendering();
stage.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void {
isMouseDown = true;
mousePos.x = stage.mouseX;
mousePos.y = stage.mouseY;
});
stage.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void {
isMouseDown = false;
});
/*這段程式不用理他
var _copy:CopyrightMC = new CopyrightMC();
_copy.text = "PV3DCubeRotationDemo";
_copy.color = 0xffffff;
this.addChild(_copy);
*/
}
private function init3DObjects():void {
rootNode = new DisplayObject3D();
scene.addChild(rootNode);
var _light :PointLight3D = new PointLight3D();
var _phongMat:PhongMaterial = new PhongMaterial(_light, 0xffffff, 0x999999, 10);
var _ml :MaterialsList = new MaterialsList( { all:_phongMat } );
cube = new Cube(_ml, 400, 400, 400);
rootNode.addChild(cube);
}
override protected function onRenderTick(e:Event = null):void {
if (isMouseDown == true) {
var _differenceX:int = mousePos.x - stage.mouseX;
var _differenceY:int = mousePos.y - stage.mouseY;
//算出上一個滑鼠座標和現在座標的距離差
cube.rotationY += _differenceX;
rootNode.rotationX += _differenceY;
mousePos.x = stage.mouseX;
mousePos.y = stage.mouseY;
vx = _differenceX / 10;
vy = _differenceY / 10;
}else {
/*
* 用上一個滑鼠座標的位置, 減去現在座標的位置
* 即可用來當作加速度,
* 加速度再乘上一個衰竭值,
* 即可讓加速度慢慢的漸近於0,
* 物件也就可以慢慢的靜止,
* 直到又賦予一個新的加速度。
* */
vx *= decay;
vy *= decay;
cube.rotationY += vx;
rootNode.rotationX += vy;
}
super.onRenderTick(e);
}
}
}
留言
在PV3D中我設計了透過滑鼠滾輪讓攝影機zoom的功能,但在操作時一開始,使用者都必須先在畫面用滑鼠點擊一次,才能夠開始滑鼠的滾輪作用,請問這是怎麼樣的問題呢?可以給我點建議嗎?
謝謝你^^
但現在怎麼測都可以偵測的到MouseWheel事件, 我再來再一下原因再來分享心得
(BTW...老師吉他彈不錯內)
我熱愛吉他, 更熱愛Flash
一開始旋轉方向正確
旋轉一個方向到180度
之後遇到旋轉方向卻是相反
是因為沒有Z軸關係嗎
還是要怎麼修正
謝謝您