cocos2dx 两点角度怎么得到精灵的旋转角度

cocos2d-x CCRotateTo 对初始角,旋转角的处理
假如有这样的代码(hello 是个sprite):
hello-&setRotation(720);&
hello-&runAction(&
&CCRotateTo::create(2, 720)&
设置 sprite的初始角度720度,然后命令他 &CCRotateTo::create(2, 720)&。 乍一看从720度转到720度,应该是静止不动的;但实际上,这个sprite顺时针转动360度。
太特么搞笑了。。。
参考 CCActionInterval.cpp line:810 代码:
void CCRotateTo::startWithTarget(CCNode *pTarget){&
&&& CCActionInterval::startWithTarget(pTarget);&
&&& m_fStartAngle = pTarget-&getRotation();&
&&& if (m_fStartAngle & 0){&
&&&&&&& m_fStartAngle = fmodf(m_fStartAngle, 360.0f);&
&&& else{&
&&&&&&& m_fStartAngle = fmodf(m_fStartAngle, -360.0f);&
&&& m_fDiffAngle = m_fDstAngle - m_fStartA&
&&& if (m_fDiffAngle & 180){&
&&&&&&& m_fDiffAngle -= 360;&
&&& if (m_fDiffAngle & -180){&
&&&&&&& m_fDiffAngle += 360;&
这个函数的作用就是:假如你的sprite 有个 rotateTo 的action,那么开转之前,他先 getRotation,将得到的rotation与360取模。于是先前那个 720& 变成了0.
接着,函数又判断转动角度:720-0(这个0是先前的720)=720,大于180&,于是 -=360,得到旋转角 360&。
于是就可以解释开始的例子里面hello实际旋转了360&的问题了。
可能是我道行太浅,实在没大明白这个函数对初始角,旋转角这样处理的物理意义。不过既然rotateTo 不好用,那就尽量避免使用它了。rotateBy 应该是没有逻辑问题的09-1709-1709-1309-14
04-2304-2310-0510-04
◇本站云标签
◇热点推荐【转】iphone-Cocos2d:精灵实现缩放,旋转,跳动,移动等动画 - shawn.zp - ITeye博客
博客分类:
// **延迟效果**
[CCDelayTime actionWithDuration:1];
// **缩放效果**
id actionScale1 = [CCScaleBy actionWithDuration:2];
id actionScale2 = [CCScaleBy actionWithDuration:2 scale:2.0f];
id actionScale3 = [CCScaleBy actionWithDuration:2 scaleX:3.0f scaleY:3.0f];
// CCScaleBy 为缩放动画,actionWithDuration为执行次数,scale为缩放的大小,正值为放大,负值为缩小,最后的scaleX,scaleY为在X,Y方向作缩放动作。
// **旋转效果**
id actionRotate1 = [CCRotateBy actionWithDuration:2];
id actionRotate2 = [CCRotateBy actionWithDuration:2 angle:180.0f];
//其他同上,只是增加了angle,旋转的角度。
// **跳动效果**
id actionJump1 = [CCJumpBy actionWithDuration:3];
id actionJump2 = [CCJumpBy actionWithDuration:3 position:ccp(s.width/10, 0) height:20 jumps:3];
// 其他同上,最后一个是在3秒内跳转到右下角,连续跳3次,每次高度为20
// **同时执行旋转和跳动效果**
id actionSpan = [CCSpawn actions:actionRotate2, actionJump2, nil];
//以上的动作基本是有相反的动作的,可以通过reverse来作。
id back = [actionSpan reverse];
// **定制动画顺序**
id actionSequence = [CCSequence actions:actionJump1,actionRotate1,actionScale1, nil];
// **执行动画循环**
id actionRepeat = [CCRepeat actionWithAction:back times:2];
// **执行动画永久循环**
id actionRepeatForever = [CCRepeatForever actionWithAction:actionSequence];
//runAction 运行动画,你想要那个效果直接替换上面的动画即可。
[sprite1 runAction:actionRepeat];
//还有在运行过程的快慢
[sprite2 runAction:[CCEaseIn actionWithAction:actionRepeat rate:2]];
[sprite2 runAction:[CCEaseOut actionWithAction:actionRepeat rate:3]];
[sprite2 runAction:[CCEaseInOut actionWithAction:actionRepeat rate:3]];
[sprite2 runAction:[CCEaseSineIn actionWithAction:actionRepeat]];
[sprite2 runAction:[CCEaseSineOut actionWithAction:actionRepeat]];
[sprite2 runAction:[CCEaseSineInOut actionWithAction:actionRepeat]];
[sprite2 runAction:[CCEaseExponentialIn actionWithAction:actionRepeat]];
[sprite2 runAction:[CCEaseExponentialOut actionWithAction:actionRepeat]];
[sprite2 runAction:[CCEaseExponentialInOut actionWithAction:actionRepeat]];
[sprite2 runAction:[CCSpeed actionWithAction:actionRepeat speed:(CCRANDOM_0_1() * 5)]];
pan_这名还行
浏览: 31631 次
来自: 郑州
怎么都喜欢留一部分让人猜啊...温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
根据正切math.h &中的 & atan2(x, y) * 180 / 3.14 &来求角度以下是代码&
if(angle & -45 && angle &-135)
sGlobal-&hero-&move(kLeft); if(angle & 45 && angle &135)
sGlobal-&hero-&move(kRight); if(angle & 45 && angle & -45)
sGlobal-&hero-&move(kUp); if(angle &135 && angle &-135)
sGlobal-&hero-&move(kDown);但是发现和以前的数学中的线象有写区别左半部分是负的 坐标 0-180 &有半部分是正的0-180和以前数学中的上半部分是正下半部分是负的有区别&暂时我是这样来理解的做成图来表示
阅读(4212)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'cocos2dx摇杆
向量的角度来控制方向',
blogAbstract:'cocos2dx中摇杆参照网上himi的教程编写好我又改进了下 &发现根据方向返回的CCPoint来确定方向比较方便根据正切math.h &中的 & atan2(x, y) * 180 / 3.14 &来求角度以下是代码&
if(angle & -45 && angle &-135)
sGlobal-&hero-&move(kLeft); if(angle & 45 && angle &135)
sGlobal-&hero-&move(kRight); if(angle & 45 && angle & -45)
sGlobal-&hero-&move(kUp);',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:7,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}physicsworld-添加了一个物理世界后(有重力),添加了一个带刚体的精灵,但精灵时不时会自己弹起来,为什么?
添加了一个物理世界后(有重力),添加了一个带刚体的精灵,但精灵时不时会自己弹起来,为什么?
添加了物理世界并设置重力,并用一个空心盒子承载
auto scene = Scene::createWithPhysics();
Vect gravity(0, -1000.0);
scene-&getPhysicsWorld()-&setGravity(gravity);
//开启测试模式
scene-&getPhysicsWorld()-&setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto visibleSize = Director::getInstance()-&getVisibleSize();
//创建一个空心盒子刚体作为世界边界
auto body = PhysicsBody::createEdgeBox(Size(visibleSize.width,visibleSize.height-100),PHYSICSBODY_MATERIAL_DEFAULT,3);
//body-&setDynamic(false);
body-&setContactTestBitmask(1);
//body-&getShape(0)-&setRestitution(0);
//创建一个节点,用来承载刚体,这样刚体就能参与到游戏的物理世界
auto node = Node::create();
node-&setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
node-&setPhysicsBody(body);
scene-&addChild(node);
auto layer = HelloWorld::create();
scene-&addChild(layer,10);
然后给精灵设置了刚体,并把弹性设置为0
auto playerPhysicsBody = PhysicsBody::createBox(this-&getContentSize());
playerPhysicsBody-&getShape(0)-&setFriction(0);
playerPhysicsBody-&getShape(0)-&setRestitution(0);
playerPhysicsBody-&setCategoryBitmask(1);
playerPhysicsBody-&setCollisionBitmask(1);
playerPhysicsBody-&setContactTestBitmask(1);
this-&setPhysicsBody(playerPhysicsBody);
精灵落下后,并不会弹起,可以过一会,我并不做任何操作,他自己会弹一下弹一下,有时高有时低,还会弹不见。
请问下该如何解决,我想让精灵没有弹性。}

我要回帖

更多关于 cocos2dx node 旋转 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信