• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    jMonkeyEngine譯文FlagRush6(2)控制交通工具

    來源:懂視網 責編:小采 時間:2020-11-09 07:43:01
    文檔

    jMonkeyEngine譯文FlagRush6(2)控制交通工具

    jMonkeyEngine譯文FlagRush6(2)控制交通工具:天氣有點冷,早上竟然出奇般的七點多就起床了,呵呵~~ 6.4 、 Actions 在第五課,我們創建一個 InputHandler ,它調用了 4 個分開的 InputAction : KeyNodeForwardAction , KeyNodeBackwardAction , KeyTurnLeftActi
    推薦度:
    導讀jMonkeyEngine譯文FlagRush6(2)控制交通工具:天氣有點冷,早上竟然出奇般的七點多就起床了,呵呵~~ 6.4 、 Actions 在第五課,我們創建一個 InputHandler ,它調用了 4 個分開的 InputAction : KeyNodeForwardAction , KeyNodeBackwardAction , KeyTurnLeftActi

    天氣有點冷,早上竟然出奇般的七點多就起床了,呵呵~~ 6.4 、 Actions 在第五課,我們創建一個 InputHandler ,它調用了 4 個分開的 InputAction : KeyNodeForwardAction , KeyNodeBackwardAction , KeyTurnLeftAction , KeyTurnRightAction 。我們現

    天氣有點冷,早上竟然出奇般的七點多就起床了,呵呵~~

    6.4、Actions

    在第五課,我們創建一個InputHandler,它調用了4個分開的InputAction:KeyNodeForwardAction,KeyNodeBackwardAction,KeyTurnLeftAction,KeyTurnRightAction。我們現在將編寫我們自定義的Action去使用新的Vehicle類。

    首先,我們想用一個加速vehicle的action替換KeyNodeForwardAction。我決定創建5個新的InputAction(比之前多了一個)。所以,為了替換keyNodeForwardAction,我們將創建AccelerateAction。

    我們不再需要關心action的速度,因為這是由vehicle決定的。我們只需要在action執行的時候更新它的速度,然后基于這個新的速度移動vehicle。

    publicvoid performAction(InputActionEvent e) {

    node.accerate(e.getTime());

    Vector3f loc = node.getLocalTranslation();

    loc.addLocal(

    node.getLocalRotation().getRotationColumn(2,tempVa)

    .multLocal(node.getVelocity()*e.getTime())

    );

    node.setLocalTranslation(loc);

    }

    正如你看到的,我們僅僅調用Vehicle的accelerate方法(AccelerateAction也在構造期間接收一個Vehicle對象,而不是Node),然后改變它的移動。

    BrakeAction也一樣,除了我們叫它brake之外。

    publicvoid performAction(InputActionEvent e) {

    node.brake(e.getTime());

    Vector3f loc = node.getLocalTranslation();

    loc.addLocal(

    node.getLocalRotation().getRotationColumn(2,tempVa)

    .multLocal(node.getVelocity()*e.getTime())

    );

    node.setLocalTranslation(loc);

    }

    這些action現在將允許我們加速和停止vehicle。現在我們需要允許它轉彎。正如你可能猜到它和KeyNodeTurn*Action類是一樣的,除了我們使用Vehicle的轉彎速度代替action的速度。一點不同的是我加入了判斷我們的速度是正的還是負的。如果它是正的,那么我們正常工作,但如果它是負的,Vehicle正在后退,所以轉彎的效果將相反。

    publicvoid performAction(InputActionEvent evt) {

    //我們想轉不同的方向,這取決于我們往哪個方向行駛

    if(vehicle.getVelocity() < 0) {

    incr.fromAngleNormalAxis(

    -vehicle.getTurnSpeed() * evt.getTime()

    , upAxis

    );

    } else {

    incr.fromAngleNormalAxis(

    vehicle.getTurnSpeed() * evt.getTime(),

    upAxis

    );

    }

    vehicle.getLocalRotation().fromRotationMatrix(

    incr.mult(

    vehicle.getLocalRotation()

    .toRotationMatrix(tempMa),

    tempMb

    )

    );

    vehicle.getLocalRotation().normalize();

    }

    publicvoid performAction(InputActionEvent evt) {

    if (vehicle.getVelocity() < 0) {

    incr.fromAngleNormalAxis(

    vehicle.getTurnSpeed() * evt.getTime(),

    upAxis

    );

    } else {

    incr.fromAngleNormalAxis(

    -vehicle.getTurnSpeed() * evt.getTime(),

    upAxis

    );

    }

    vehicle.getLocalRotation().fromRotationMatrix(

    incr.mult(

    vehicle.getLocalRotation()

    .toRotationMatrix(tempMa),

    tempMb

    )

    );

    vehicle.getLocalRotation().normalize();

    }

    我們最后的InputAction將處理vehicle的漂移。這個action是不同的,因為它不會由key觸發,但在每次update都發生。這將在下一節講到。現在,我們調用Vehicle的drift方法并更新位置。

    publicvoid performAction(InputActionEvent evt) {

    vehicle.drift(evt.getTime());

    Vector3f loc = vehicle.getLocalTranslation();

    loc.addLocal(

    vehicle.getLocalRotation()

    .getRotationColumn(2, tempVa)

    .multLocal(

    vehicle.getVelocity() * evt.getTime()

    )

    );

    vehicle.setLocalTranslation(loc);

    }

    你可能注意到一堆重復的代碼。相當于大多類都做相同的事情,只是調用vehicle不同的方法。下一節,我們將清理并優化。

    既然我們現在已經有了我們5個新的aciton,我們需要在FlagRushHandler中調用它們。我們將賦予相同的鍵(WASD)。

    所以在setActions方法中我們只是創建4個移動action,傳入vehicle對象作為參數。

    Drift也被實例化而并沒有賦給一個觸發器。這是因為我們現在覆蓋了update方法。update調用super去檢查其他常規action,然后調用drift action。這確保了當沒有鍵被按下時drift漂移。然而,這個邏輯有點瑕疵,這就給讀者作為練習去弄清那是什么。一點暗示,當玩家按下W或S的時候發生了什么?我將在下一節課修復和討論這個瑕疵。是的,我現在給出作業了。

    6.5、FlagRushHandler.java

    import lesson6.actions.AccelerateAction;

    import lesson6.actions.DriftAction;

    import lesson6.actions.VehicleRotateLeftAction;

    import com.jme.input.InputHandler;

    import com.jme.input.KeyBindingManager;

    import com.jme.input.KeyInput;

    import com.jme.input.action.KeyNodeBackwardAction;

    import com.jme.input.action.KeyNodeRotateRightAction;

    /**

    * 游戲的InputHnadler。這控制了一個給出的Spatial

    * 允許我們去把它往前移、往后移和左右旋轉。

    * @author John

    *

    */

    publicclass FlagRushInputHandler extends InputHandler {

    private DriftAction drift;

    /**

    * 提供用于控制的node。api將處理input的創建

    * @param node 我們想移動的那個node

    * @param api library將處理input的創建

    */

    public FlagRushInputHandler(Vehicle node, String api){

    setKeyBindings(api);

    setActions(node);

    }

    /**

    * 將action類賦給trigger。這些action處理結點前移、后移和旋轉

    * @param node 用于控制的結點

    */

    privatevoid setActions(Vehicle node) {

    AccelerateAction forward =

    new AccelerateAction(node);

    addAction(forward,"forward",true);

    KeyNodeBackwardAction backward =

    new KeyNodeBackwardAction(node,15f);

    addAction(backward,"backward",true);

    VehicleRotateLeftAction rotateLeft =

    new VehicleRotateLeftAction(node);

    addAction(rotateLeft,"turnLeft",true);

    KeyNodeRotateRightAction rotateRight =

    new KeyNodeRotateRightAction(node,5f);

    rotateRight.setLockAxis(

    node.getLocalRotation().getRotationColumn(1)

    );

    addAction(rotateRight,"turnRight",true);

    //不由key觸發

    drift = new DriftAction(node);

    }

    /**

    * 創建keyboard對象,當鍵被按下時允許我們獲取鍵盤的值。

    * 它接著設置action作為觸發器的基礎,如果確認了鍵被按下(WASD)

    * @param api

    */

    privatevoid setKeyBindings(String api) {

    KeyBindingManager keyboard =

    KeyBindingManager.getKeyBindingManager();

    keyboard.set("forward", KeyInput.KEY_W);

    keyboard.set("backward", KeyInput.KEY_S);

    keyboard.set("turnLeft", KeyInput.KEY_A);

    keyboard.set("turnRight", KeyInput.KEY_D);

    }

    @Override

    publicvoid update(float time) {

    if(!isEnabled()) return;

    super.update(time);

    //我們通常想讓摩擦力控制漂移

    drift.performAction(event);

    }

    }

    6.6、AccelerateAction.java

    import lesson6.Vehicle;

    import com.jme.input.action.InputAction;

    import com.jme.input.action.InputActionEvent;

    import com.jme.math.Vector3f;

    publicclass AccelerateAction extends InputAction {

    private Vehicle node;

    private Vector3f tempVa=new Vector3f();

    public AccelerateAction(Vehicle node){

    this.node = node;

    }

    @Override

    publicvoid performAction(InputActionEvent e) {

    node.accerate(e.getTime());

    Vector3f loc = node.getLocalTranslation();

    loc.addLocal(

    node.getLocalRotation().getRotationColumn(2,tempVa)

    .multLocal(node.getVelocity()*e.getTime())

    );

    node.setLocalTranslation(loc);

    }

    }

    6.7、BrakeAction.java

    import lesson6.Vehicle;

    import com.jme.input.action.InputAction;

    import com.jme.input.action.InputActionEvent;

    import com.jme.math.Vector3f;

    publicclass BrakeAction extends InputAction {

    private Vehicle node;

    private Vector3f tempVa=new Vector3f();

    public BrakeAction(Vehicle node){

    this.node = node;

    }

    @Override

    publicvoid performAction(InputActionEvent e) {

    node.brake(e.getTime());

    Vector3f loc = node.getLocalTranslation();

    loc.addLocal(

    node.getLocalRotation().getRotationColumn(2,tempVa)

    .multLocal(node.getVelocity()*e.getTime())

    );

    node.setLocalTranslation(loc);

    }

    }

    6.8、VehicleRotateLeftAction.java

    import lesson6.Vehicle;

    import com.jme.input.action.InputAction;

    import com.jme.input.action.InputActionEvent;

    import com.jme.math.Matrix3f;

    import com.jme.math.Vector3f;

    publicclass VehicleRotateLeftAction extends InputAction {

    //處理旋轉的臨時變量

    privatestaticfinal Matrix3f incr = new Matrix3f();

    privatestaticfinal Matrix3f tempMa = new Matrix3f();

    privatestaticfinal Matrix3f tempMb = new Matrix3f();

    //我們使用Y軸作為上

    private Vector3f upAxis = new Vector3f(0,1,0);

    //操縱的結點

    private Vehicle vehicle;

    public VehicleRotateLeftAction(Vehicle vehicle){

    this.vehicle = vehicle;

    }

    @Override

    publicvoid performAction(InputActionEvent evt) {

    //我們想轉不同的方向,這取決于我們往哪個方向行駛

    if(vehicle.getVelocity() < 0) {

    incr.fromAngleNormalAxis(

    -vehicle.getTurnSpeed() * evt.getTime()

    , upAxis

    );

    } else {

    incr.fromAngleNormalAxis(

    vehicle.getTurnSpeed() * evt.getTime(),

    upAxis

    );

    }

    vehicle.getLocalRotation().fromRotationMatrix(

    incr.mult(

    vehicle.getLocalRotation()

    .toRotationMatrix(tempMa),

    tempMb

    )

    );

    vehicle.getLocalRotation().normalize();

    }

    }

    6.9、VehicleRotateRightAction.java

    import lesson6.Vehicle;

    import com.jme.input.action.InputAction;

    import com.jme.input.action.InputActionEvent;

    import com.jme.math.Matrix3f;

    import com.jme.math.Vector3f;

    publicclass VehicleRotateRightAction extends InputAction {

    //用于處理旋轉的臨時變量

    privatestaticfinal Matrix3f incr = new Matrix3f();

    privatestaticfinal Matrix3f tempMa = new Matrix3f();

    privatestaticfinal Matrix3f tempMb = new Matrix3f();

    //用于操作的結點

    private Vehicle vehicle;

    private Vector3f upAxis = new Vector3f(0, 1, 0);

    public VehicleRotateRightAction(Vehicle vehicle){

    this.vehicle = vehicle;

    }

    @Override

    publicvoid performAction(InputActionEvent evt) {

    if (vehicle.getVelocity() < 0) {

    incr.fromAngleNormalAxis(

    vehicle.getTurnSpeed() * evt.getTime(),

    upAxis

    );

    } else {

    incr.fromAngleNormalAxis(

    -vehicle.getTurnSpeed() * evt.getTime(),

    upAxis

    );

    }

    vehicle.getLocalRotation().fromRotationMatrix(

    incr.mult(

    vehicle.getLocalRotation()

    .toRotationMatrix(tempMa),

    tempMb

    )

    );

    vehicle.getLocalRotation().normalize();

    }

    }

    6.10、DriftAction.java

    import lesson6.Vehicle;

    import com.jme.input.action.InputAction;

    import com.jme.input.action.InputActionEvent;

    import com.jme.math.Vector3f;

    publicclass DriftAction extends InputAction {

    private Vehicle vehicle;

    private Vector3f tempVa = new Vector3f();

    public DriftAction(Vehicle vehicle) {

    this.vehicle = vehicle;

    }

    @Override

    publicvoid performAction(InputActionEvent evt) {

    vehicle.drift(evt.getTime());

    Vector3f loc = vehicle.getLocalTranslation();

    loc.addLocal(

    vehicle.getLocalRotation()

    .getRotationColumn(2, tempVa)

    .multLocal(

    vehicle.getVelocity() * evt.getTime()

    )

    );

    vehicle.setLocalTranslation(loc);

    }

    }

    6.11、總結

    就那樣,我們現在已經創建自己的action去允許我們的vehicle以一種更真實的方式運行。這將給我們一種控制玩家執行特性的能力,包括后面的敵人。

    下一步,我們將看看改善terrain和圖形外觀。

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    jMonkeyEngine譯文FlagRush6(2)控制交通工具

    jMonkeyEngine譯文FlagRush6(2)控制交通工具:天氣有點冷,早上竟然出奇般的七點多就起床了,呵呵~~ 6.4 、 Actions 在第五課,我們創建一個 InputHandler ,它調用了 4 個分開的 InputAction : KeyNodeForwardAction , KeyNodeBackwardAction , KeyTurnLeftActi
    推薦度:
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国内精品久久久久久野外| 日韩国产精品无码一区二区三区| 亚洲精品无码成人片久久| 亚洲精品第一国产综合精品99| 亚洲精品动漫人成3d在线| 国产a精品视频| 成人免费精品网站在线观看影片| 国产精品你懂的在线播放| 97精品一区二区视频在线观看| 亚洲欧美国产精品专区久久| 亚洲精品乱码久久久久久蜜桃不卡| 日韩精品在线一区二区| 精品久久人人妻人人做精品 | 久久亚洲国产欧洲精品一| 国产精品推荐天天看天天爽| 亚洲精品国产电影| 国产欧美一区二区精品性色99 | 亚洲中文久久精品无码| 成人精品视频99在线观看免费| 亚洲欧美日韩精品久久亚洲区| 精品人妻少妇一区二区三区不卡| 亚洲国产精品婷婷久久| 国产成人精品日本亚洲18图| 精品国产一区二区三区无码| 91亚洲精品自在在线观看| 亚洲精品一级无码中文字幕| 国产精品主播一区二区| 最新亚洲精品国自产在线观看| 国产精品va无码一区二区 | 99国产精品永久免费视频| 久久精品国产亚洲av影院| 最新国产の精品合集| 欧美777精品久久久久网| 国产精品怡红院永久免费| 日韩精品一二三区| 2022国产精品自产拍在线观看| 小辣椒福利视频精品导航| 91大神精品全国在线观看| 久久97精品久久久久久久不卡| 国产精品欧美一区二区三区不卡| 国产精品18久久久久久vr|