3实现模型跟随地形的效果文档格式.docx
《3实现模型跟随地形的效果文档格式.docx》由会员分享,可在线阅读,更多相关《3实现模型跟随地形的效果文档格式.docx(15页珍藏版)》请在冰豆网上搜索。
#region成员变量、属性和构造函数
//需要跟随的地形
SimpleTerrainSceneNodeterrain;
//坦克各部件的Bone。
ModelBoneturretBone;
ModelBonecannonBone;
ModelBonelSteerEngineBone;
ModelBonerSteerEngineBone;
ModelBoneleftBackWheelBone;
ModelBonerightBackWheelBone;
ModelBoneleftFrontWheelBone;
ModelBonerightFrontWheelBone;
//坦克各部件Bone的转换矩阵
MatrixturretTransform;
MatrixcannonTransform;
MatrixlSteerEngineTransform;
MatrixrSteerEngineTransform;
MatrixleftBackWheelTransform;
MatrixrightBackWheelTransform;
MatrixleftFrontWheelTransform;
MatrixrightFrontWheelTransform;
//车轮自转的旋转矩阵
MatrixwheelRollMatrix;
//各部件的旋转量
floatturretRotationValue;
floatcannonRotationValue;
floatsteerRotationValue;
floatwheelRotationValue;
///炮管、炮塔旋转的最大角速度
publicconstfloatMaxCannonSpeed=1f;
///炮塔的旋转范围
privateconstfloatMaxTurretAngle=1.0f;
privateconstfloatMinTurretAngle=-1.0f;
///炮管的旋转范围
privateconstfloatMaxCannonAngle=0.0f;
privateconstfloatMinCannonAngle=-0.9f;
///获取或设置车轮的旋转量
publicfloatWheelRotationValue
get{returnwheelRotationValue;
}
set{wheelRotationValue=value;
///获取或设置前轮轴的旋转量
publicfloatSteerRotationValue
get{returnsteerRotationValue;
set{steerRotationValue=value;
///获取或设置炮塔旋转量
publicfloatTurretRotation
get{returnturretRotationValue;
set
if(value>
MaxTurretAngle)
turretRotationValue=MaxTurretAngle;
elseif(value<
MinTurretAngle)
turretRotationValue=MinTurretAngle;
else
turretRotationValue=value;
///获取或设置炮管旋转量
publicfloatCannonRotation
get{returncannonRotationValue;
MaxCannonAngle)
cannonRotationValue=MaxCannonAngle;
MinCannonAngle)
cannonRotationValue=MinCannonAngle;
cannonRotationValue=value;
///创建一个ModelSceneNode对象。
paramname="
engine"
>
引擎<
/param>
modelAssetName"
模型名称<
publictankModel(StunXnaGEengine,ScenesetScene,stringmodelAssetName,SimpleTerrainSceneNodeterrain)
:
base(engine,setScene,modelAssetName)
this.terrain=terrain;
#endregion
///初始化节点。
publicoverridevoidInitialize()
base.Initialize();
//获取坦克各部件的Bone。
turretBone=model.Bones["
turret_geo"
];
cannonBone=model.Bones["
canon_geo"
lSteerEngineBone=model.Bones["
l_steer_geo"
rSteerEngineBone=model.Bones["
r_steer_geo"
leftBackWheelBone=model.Bones["
l_back_wheel_geo"
rightBackWheelBone=model.Bones["
r_back_wheel_geo"
leftFrontWheelBone=model.Bones["
l_front_wheel_geo"
rightFrontWheelBone=model.Bones["
r_front_wheel_geo"
//获取坦克各部件的转换矩阵
turretTransform=turretBone.Transform;
cannonTransform=cannonBone.Transform;
lSteerEngineTransform=lSteerEngineBone.Transform;
rSteerEngineTransform=rSteerEngineBone.Transform;
leftBackWheelTransform=leftBackWheelBone.Transform;
rightBackWheelTransform=rightBackWheelBone.Transform;
leftFrontWheelTransform=leftFrontWheelBone.Transform;
rightFrontWheelTransform=rightFrontWheelBone.Transform;
publicoverridevoidUpdate(GameTimegameTime)
base.Update(gameTime);
//根据旋转量重新计算变换矩阵
turretBone.Transform=Matrix.CreateRotationY(turretRotationValue)*turretTransform;
cannonBone.Transform=Matrix.CreateRotationX(cannonRotationValue)*cannonTransform;
lSteerEngineBone.Transform=Matrix.CreateRotationY(steerRotationValue)*lSteerEngineTransform;
rSteerEngineBone.Transform=Matrix.CreateRotationY(steerRotationValue)*rSteerEngineTransform;
wheelRollMatrix*=Matrix.CreateRotationX(wheelRotationValue);
leftBackWheelBone.Transform=Matrix.CreateRotationX(wheelRotationValue)*leftBackWheelTransform;
rightBackWheelBone.Transform=Matrix.CreateRotationX(wheelRotationValue)*rightBackWheelTransform;
leftFrontWheelBone.Transform=Matrix.CreateRotationX(wheelRotationValue)*leftFrontWheelTransform;
rightFrontWheelBone.Transform=Matrix.CreateRotationX(wheelRotationValue)*rightFrontWheelTransform;
///绘制坦克。
gameTime"
<
publicoverrideintDraw(GameTimegameTime,booluseReflect)
model.CopyAbsoluteBoneTransformsTo(modelTransforms);
retur