jMonkeyEngine译文 FlagRush10(1)——墙壁检测系统(Detection System)

注:本系列教程全部翻译完之后可能会以PDF的形式发布。

如果有什么错误可以到http://blog.csdn.net/kakashi8841留言或EMAILkakashi9bi@gmail.com给我。

 

jME版本 jME_2.0.1_Stable

开发工具:MyEclipse8.5

操作系统:Window7/Vista

 

 

 

 

10、夺取Flag

10.1、介绍

         在这个向导,我将首次为玩家增加夺取flag数目的统计。那么我们将移除flag的计时随机位置,取代的是,我们将增加检查去看看玩家的vehicle是否“夺取”了Flag

10.2Flag夺取计算

首先,增加一个新的字段给Vehicle

 

private int flagsGrabbed;

 

         增加gettersetter方法

 

public int getFlagsGrabbed() {

     return flagsGrabbed;

  }

 

  public void setFlagsGrabbed(int flagsGrabbed) {

     this.flagsGrabbed = flagsGrabbed;

  }

10.3、移除Flag的计时随机位置

         为了移除Flag计时,我们停止更新,注释Flagupdate

 

//     flag.update(interpolation);

10.4、增加检查去看player是否夺取了Flag

         我们首先通过增加一个新字段决定了玩家能在多远获取一面flag

   

//获取距离

    private float grabDistanceSquared = 64f;

 

         我们这里使用的方法将在每次游戏update时检查FlagVehicle的距离。直接在游戏的update中加入:

 

 

             //首先获取Flagplayer的距离

       float distanceSquared =

           player.getLocalTranslation().distanceSquared(

                  flag.getLocalTranslation()

           );

 

         如果vehicleflag足够接近,我们将获取它并重设flag位置。

 

             //检查能否获取flag

       if(distanceSquared <= grabDistanceSquared){

           //重设flag,如果reset不能使用

           //请到Flag.java里面将reset方法改为public

           flag.reset();

           player.setFlagsGrabbed(player.getFlagsGrabbed()+1);

       }

 

10.5Vehicle.java

import com.jme.math.FastMath;

import com.jme.math.Quaternion;

import com.jme.math.Vector3f;

import com.jme.scene.Node;

import com.jme.scene.Spatial;

/**

 * Vehicle将会是一个node,处理了vehicle在游戏中的移动。

 * 它拥有定义它的加速度acceleration、速度velocity

 * 和减速度barking的参数。

 * 转向速度turnSpeed定义了它有怎样的处理。

 * 而重量weight定义了漂移时的摩擦力,它下降多快等等。

 * @author Mark Powell

 */

 

public class Vehicle extends Node {

   

    private Spatial model;

 

    private float velocity;

    private float maxSpeed = 30;

    private float minSpeed = 10;

   

    private float acceleration;

    private float braking;

   

    private float turnSpeed;

    private float weight;

 

    private Vector3f tempVa;

 

    private int lean;

    private float leanAngle;

    private static final float LEAN_BUFFER = 0.05f;

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

    private Quaternion q = new Quaternion();

   

    private int flagsGrabbed;

 

   

    public int getFlagsGrabbed() {

       return flagsGrabbed;

    }

 

    public void setFlagsGrabbed(int flagsGrabbed) {

       this.flagsGrabbed = flagsGrabbed;

    }

 

    private Spatial backwheel,frontwheel;

    public Spatial getFrontwheel() {

       return frontwheel;

    }

 

    public void setFrontwheel(Spatial frontwheel) {

       this.frontwheel = frontwheel;

    }

 

    public Spatial getBackwheel() {

       return backwheel;

    }

 

    public void setBackwheel(Spatial backwheel) {

       this.backwheel = backwheel;

    }

 

    private float angle;

    private Quaternion rotQuat = new Quaternion();

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

   

    /**

     * 基础构造方法,获取模型作为vehicle的外观

     * @param id vehicleid

     * @param model 表达vehicle绘图外观的模型

     */

    public Vehicle(String id, Spatial model){

       super(id);

       setModel(model);

    }

   

    /**

     * 构造方法,获取模型作为vehicle的外观

     * @param id vehicleid

     * @param model 表达vehicle绘图外观的模型

     * @param maxSpeed vehicle能达到的最大速度(Unit/Sec)

     * @param minSpeed vehicle的反向最大速度(Unit/Sec)

     * @param accerlation vehicle多快能达到最大速度

     * @param braking vehicle减速的速度以及按多久反向

     * @param weight vehicle的重量

     * @param turnSpeed vehicle转向转得多块

     */

    public Vehicle(String id,Spatial model,

           float maxSpeed, float minSpeed,

           float accerlation, float braking,

           float weight, float turnSpeed){

       super(id);

       setModel(model);

       this.maxSpeed     = maxSpeed;

       this.minSpeed     = minSpeed;

       this.acceleration    = acceleration;

       this.braking      = braking;

       this.weight       = weight;

       this.turnSpeed       = turnSpeed;

    }

   

    public Spatial getModel() {

       return model;

    }

    public void setModel(Spatial model) {

       this.detachChild(this.model);

       this.model = model;

       this.attachChild(this.model);

      

       //获取前轮和后轮的引用

        backwheel = ((Node)model).getChild("backwheel");

        frontwheel = ((Node)model).getChild("frontwheel");

    }

    public float getVelocity() {

       return velocity;

    }

    public void setVelocity(float velocity) {

       this.velocity = velocity;

    }

    public float getMaxSpeed() {

       return maxSpeed;

    }

    public void setMaxSpeed(float maxSpeed) {

       this.maxSpeed = maxSpeed;

    }

    public float getMinSpeed() {

       return minSpeed;

    }

    public void setMinSpeed(float minSpeed) {

       this.minSpeed = minSpeed;

    }

    public float getAcceleration() {

       return acceleration;

    }

    public void setAcceleration(float acceleration) {

       this.acceleration = acceleration;

    }

    public float getBraking() {

       return braking;

    }

    public void setBraking(float braking) {

       this.braking = braking;

    }

    public float getTurnSpeed() {

       return turnSpeed;

    }

    public void setTurnSpeed(float turnSpeed) {

       this.turnSpeed = turnSpeed;

    }

    public float getWeight() {

       return weight;

    }

    public void setWeight(float weight) {

       this.weight = weight;

    }

   

    public void accerate(float time){

       velocity = (velocity+=acceleration*time)

               > maxSpeed ? maxSpeed : velocity;

    }

   

    public void brake(float time){

       velocity = (velocity-=braking*time)

               < -minSpeed ? -minSpeed : velocity;

    }

 

    public void drift(float time) {

       if(velocity < -FastMath.FLT_EPSILON) {

           velocity += ((weight/5) * time);

            //我们将漂移到停止,所以不能超过0

            velocity = velocity > 0 ? 0 : velocity;

        } else if(velocity > FastMath.FLT_EPSILON){

            velocity -= ((weight/5) * time);

          //我们将漂移到停止,所以不能低于0

            velocity = velocity < 0 ? 0 : velocity;

        }

    }

   

    public void update(float time){

       this.localTranslation.addLocal(

              this.localRotation.getRotationColumn(2, tempVa)

              .mult(velocity*time)

       );

       rotateWheels(time);

       processLean(time);

    }

 

    public void setRotateOn(int modifier) {

       lean = modifier;

    }

   

    /**

     * processlean将基于一个lean因素调整bike模型的角度。

     * 我们调整bike而不是Vehicle

     * 因为Vehicle关心的是biketerrain上的位置

     * @param time the time between frames

     */

    private void processLean(float time) {

        //查查我们是否需要倾斜

        if(lean != 0) {

            if(lean == -1 && leanAngle < 0) {

                leanAngle += -lean * 4 * time;

            } else if(lean == 1 && leanAngle > 0) {

                leanAngle += -lean * 4 * time;

            } else {

                leanAngle += -lean * 2 * time;

            }

            //最大倾斜为-11

            if(leanAngle > 1) {

                leanAngle = 1;

            } else if(leanAngle < -1) {

                leanAngle = -1;

            }

        } else { //我们不需要倾斜,让它正直

            if(leanAngle < LEAN_BUFFER && leanAngle > -LEAN_BUFFER) {

                leanAngle = 0;

            }

            else if(leanAngle < -FastMath.FLT_EPSILON) {

                leanAngle += time * 4;

            } else if(leanAngle > FastMath.FLT_EPSILON) {

                leanAngle -= time * 4;

            } else {

                leanAngle = 0;

            }

        }

      

        q.fromAngleAxis(leanAngle, leanAxis);

        model.setLocalRotation(q);

       

        lean = 0;

    }

   

    private void rotateWheels(float time){

    //vehicle移动的时候旋转轮子

    if(vehicleIsMoving()){

        //前进

        if(velocity > FastMath.FLT_EPSILON){

            angle = angle - ((time) * velocity * 0.5f);

                if (angle < -360) {

                    angle += 360;

                }

        }else{

            angle = angle + ((time) * velocity * .5f);

            if(angle > 360){

               angle -= 360;

            }

        }

        rotQuat.fromAngleAxis(angle, wheelAxis);

        frontwheel.getLocalRotation().multLocal(rotQuat);

        backwheel.setLocalRotation(frontwheel.getLocalRotation());

    }

    }

 

   /**

    * 用于判断vehicle是否移动的便利方法。

    * 当速度velocity接近0时为真。

    * @return true vehicle正在移动,否则为false

    */

    private boolean vehicleIsMoving() {

       return velocity > FastMath.FLT_EPSILON ||

           velocity < -FastMath.FLT_EPSILON;

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值