3D战斗游戏之运动模式准备篇1

2015年03月17日 10:43 0 点赞 0 评论 更新于 2017-05-05 06:29

在前两篇文章中,我们探讨了构建游戏数据库的基础。基于该数据库,我们已经能够依据指定的灯光、摄像机位置、3D模型文件以及3D模型位置等要素,构建出一个场景及其包含的精灵。

作为一款游戏,让场景中的精灵动起来是必不可少的。在这个过程中,你会发现一个有趣的现象:在Cocos的示例项目(cpp - test)里,3D精灵展示采用的是平面相机(2D),而非投影相机(3D)。

进一步探究会发现,即便Cocos2d - x发展到目前最新的3.3RC0版本,也并未封装一个3D的移动方法,仍然只有诸如moveTomoveBy这类仅支持XY轴运动的2D移动方法。

那么,如何像在2D中使用moveTo那样,让精灵在XYZ轴上都能实现移动呢?方法有很多,下面先介绍一种更改引擎的方法,即为自己的引擎添加MoveBy3DMoveTo3D方法。

步骤一:修改CCActionInterval.h文件

首先,打开“盘符:\cocos目录\cocos2d\cocos\2d”路径下的CCActionInterval.h文件,添加如下代码:

//dark添加开始***********************************************************************************************************
class CC_DLL MoveBy3D : public ActionInterval
{
public:
/** creates the action */
static MoveBy3D* create(float duration, const Vec3& deltaPosition);

//
// Overrides
//
virtual MoveBy3D* clone() const override;
virtual MoveBy3D* reverse(void) const  override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;

CC_CONSTRUCTOR_ACCESS:
MoveBy3D() {}
virtual ~MoveBy3D() {}

/** initializes the action */
bool initWithDuration(float duration, const Vec3& deltaPosition);

protected:
Vec3 _positionDelta;
Vec3 _startPosition;
Vec3 _previousPosition;

private:
CC_DISALLOW_COPY_AND_ASSIGN(MoveBy3D);
};

/** Moves a Node object to the position x,y. x and y are absolute coordinates by modifying it's position attribute.
Several MoveTo3D actions can be concurrently called, and the resulting
movement will be the sum of individual movements.
@since v2.1beta2-custom
*/
class CC_DLL MoveTo3D : public MoveBy3D
{
public:
/** creates the action */
static MoveTo3D* create(float duration, const Vec3& position);

//
// Overrides
//
virtual MoveTo3D* clone() const override;
virtual void startWithTarget(Node *target) override;

CC_CONSTRUCTOR_ACCESS:
MoveTo3D() {}
virtual ~MoveTo3D() {}

/** initializes the action */
bool initWithDuration(float duration, const Vec3& position);

protected:
Vec3 _endPosition;

private:
CC_DISALLOW_COPY_AND_ASSIGN(MoveTo3D);
};
//dark添加到此***********************************************************************************************************

步骤二:修改CCActionInterval.cpp文件

其次,打开“盘符:\cocos目录\cocos2d\cocos\2d”路径下的CCActionInterval.cpp文件,添加如下代码(这里你原文未给出具体要添加的代码内容,可补充完整后再进行相应的修改):

// 此处应添加具体代码

通过以上两个步骤,我们就为Cocos2d - x引擎添加了3D移动方法,从而可以让精灵在XYZ轴上实现移动,为3D战斗游戏的运动模式做好了前期准备。后续我们可以基于这些方法,实现更加复杂的3D运动效果。

作者信息

boke

boke

共发布了 3994 篇文章