如何在COCOS2D中绘制3d椭圆柱?

来源:互联网 发布:手机淘宝登陆连接失败 编辑:程序博客网 时间:2024/05/22 14:16

将以下代码添加到CCActionGrid3D.h


class CC_DLL CCEllipse3D : public CCGrid3DAction

{
public:


    /** initializes an action with duration, grid size, waves and amplitude */
    bool initWithDuration(float duration, const CCSize& gridSize, float alength, float blength, float startangle, float endagnle,float xoffset,float yoffset);
    /**
     *  @js NA
     *  @lua NA
     */
    virtual CCObject* copyWithZone(CCZone* pZone);
    virtual void update(float time);


public:
    /** creates an action with duration, grid size, waves and amplitude */
    static CCEllipse3D* create(float duration, const CCSize& gridSize, float alength, float blength, float startangle, float endagnle,float xoffset,float yoffset);
protected:
float m_fStartAngle;//开始角
float m_fEndAngle;//结束角
float m_fALength;//长轴
float m_fBLength;//短轴
float m_fXOffset;//X偏移
float m_fYOffset;//Y偏移

};


将以下代码添加到CCActionGrid3D.cpp


CCEllipse3D* CCEllipse3D::create(float duration, const CCSize& gridSize, float alength, float blength, float startangle, float endagnle,float xoffset,float yoffset)
{
CCEllipse3D *pAction = new CCEllipse3D();


if (pAction)
{
if (pAction->initWithDuration(duration, gridSize,  alength, blength, startangle, endagnle, xoffset, yoffset))
{
pAction->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pAction);
}
}


return pAction;    
}


bool CCEllipse3D::initWithDuration(float duration, const CCSize& gridSize, float alength, float blength, float startangle, float endagnle,float xoffset,float yoffset)
{
if (CCGrid3DAction::initWithDuration(duration, gridSize))
{
m_fStartAngle = startangle;//开始角
m_fEndAngle = endagnle;//结束角
m_fALength = alength;//长轴
m_fBLength = blength;//短轴
m_fXOffset = xoffset;//X偏移
m_fYOffset = yoffset;//Y偏移


return true;
}


return false;
}


CCObject* CCEllipse3D::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEllipse3D* pCopy = NULL;
if(pZone && pZone->m_pCopyObject) 
{
//in case of being called at sub class
pCopy = (CCEllipse3D*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEllipse3D();
pZone = pNewZone = new CCZone(pCopy);
}


CCGrid3DAction::copyWithZone(pZone);




pCopy->initWithDuration(m_fDuration, m_sGridSize, m_fALength, m_fBLength,m_fStartAngle,m_fEndAngle,m_fXOffset,m_fYOffset);


CC_SAFE_DELETE(pNewZone);
return pCopy;
}


void CCEllipse3D::update(float time)
{
 
int i, j;
    for (i = 0; i < m_sGridSize.width+1; i++)
{
float angle = (m_fStartAngle +(m_fEndAngle-m_fStartAngle)*i/m_sGridSize.width)*time;
for (j = 0; j < m_sGridSize.height + 1; j++)
{
ccVertex3F v = originalVertex(ccp(i ,j));
v.x = m_fALength*cos(angle)-m_fALength+m_fXOffset;  
v.y = m_fBLength*sin(angle)+ v.y + m_fYOffset; 
setVertex(ccp(i, j), v);
}
}
  
}


调用相关方法:

runAction(CCEllipse3D::create(1.0f, CCSize(60,1) ,490.0f,-90.0f,0.0f,(float)M_PI,1135.0f,-130.0f));

第一个参数代表时间

第二个参数代表网格

第三个参数椭圆长轴

第四个参数椭圆短柱

第五个参数开始绘制角

第六个参数结束绘制角

第七个参数x轴偏移

第八个参数Y轴偏移


0 0
原创粉丝点击