Cocos2D-x教程。目标';s位置
我很难理解cocos2d教程中一段代码背后的数学原理。这段代码是关于获取子弹的目标位置的Cocos2D-x教程。目标';s位置,cocos2d-x,Cocos2d X,我很难理解cocos2d教程中一段代码背后的数学原理。这段代码是关于获取子弹的目标位置的 void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event) { // Choose one of the touches to work with CCTouch* touch = (CCTouch*)( touches->anyObject() ); CCPoint location = touch->
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
// Choose one of the touches to work with
CCTouch* touch = (CCTouch*)( touches->anyObject() );
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
// Set up initial location of projectile
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *projectile = CCSprite::create("Projectile.png", CCRectMake(0, 0, 20, 20));
projectile->setPosition( ccp(20, winSize.height/2) );
// Determinie offset of location to projectile
int offX = location.x - projectile->getPosition().x;
int offY = location.y - projectile->getPosition().y;
// Bail out if we are shooting down or backwards
if (offX <= 0) return;
// Ok to add now - we've double checked position
this->addChild(projectile);
// Determine where we wish to shoot the projectile to
int realX = winSize.width + (projectile->getContentSize().width/2);
float ratio = (float)offY / (float)offX;
int realY = (realX * ratio) + projectile->getPosition().y;
CCPoint realDest = ccp(realX, realY);
// Determine the length of how far we're shooting
// My comment: if in the next two lines we use (offX, offY) instead of (realX, realY)
// bullet direction looks ok
int offRealX = realX - projectile->getPosition().x;
int offRealY = realY - projectile->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY * offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
projectile->runAction( CCSequence::create( CCMoveTo::create(realMoveDuration, realDest),
CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),
NULL) );
// Add to projectiles array
projectile->setTag(2);
_projectiles->addObject(projectile);
}
void HelloWorld::cctouchesend(CCSet*touchs,CCEvent*event)
{
//选择要使用的触摸之一
CCTouch*touch=(CCTouch*)(touch->anyObject());
CCPoint location=touch->getLocationInView();
location=CCDirector::sharedDirector()->convertToGL(位置);
//设置投射物的初始位置
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
CCSprite*sproject=CCSprite::create(“sproject.png”,CCRectMake(0,0,20,20));
射弹->设置位置(ccp(20,winSize.height/2));
//位置对射弹的确定性偏移
int offX=location.x-sparket->getPosition().x;
int offY=位置.y-投射物->获取位置().y;
//如果我们正在向下射击或向后射击,请救出
如果(offX getPosition().x;
int OFREALY=realY-投射物->获取位置().y;
浮动长度=sqrtf((offRealX*offRealX)+(offRealY*offRealY));
浮点速度=480/1;//480像素/1秒
float realMoveDuration=长度/速度;
//将投射物移动到实际端点
投射->运行操作(CCSequence::create(CCMoveTo::create)(realMoveDuration,realDest),
CCCallFuncN::create(这是callfuncN_选择器(HelloWorld::spriteMoveFinished)),
空);
//添加到投射物数组
射弹->设置标签(2);
_射弹->添加对象(射弹);
}
我不明白的是‘realY’的计算。它看起来像是把‘ratio’乘以一个切线
提前非常感谢这似乎与编写代码的游戏设计影响有关 投射物的初始位置:20像素,在屏幕的中间。也许这就是画“枪”的地方 触摸位置:可以在任何地方。我假设触摸位置以像素为单位报告 偏移:在初始位置和接触点之间绘制一条线 如果用户触到了枪的左侧(“向后”),那么我们什么也不做 realX:无论用户触摸到哪里,我们都希望项目符号在屏幕的整个宽度上移动。因此,将要移动到的位置的“x”坐标设置为该值
但是现在,如果我们想沿着用户触摸指示的角度或轨迹发射子弹,我们需要放大目标的Y坐标。这个比例是一个比例因子,告诉我们相对于枪的位置,Y在给定的X下必须如何变化。我们将所需的X目标乘以该斜率,然后将其添加到公牛身上et的起点(屏幕中间)我们有一些“目的地”,很可能在屏幕外,但方向与触摸坐标相同。谢谢你的快速回答。是的,20像素是因为枪应该被画在那里。无论如何,使用realX和realY是非常必要的?我们已经有了从子弹初始位置到目标的矢量(offX,offY)。如果我使用这些值,项目符号的方向看起来是相同的。没有看到我不知道的所有代码。我会从这段代码中假设项目符号在触摸位置停止,没有此外推。我已经用函数的整个主体编辑了代码,您认为如何?如果您只在指定的位置替换realX/realY,您将处于启用状态仅更改持续时间的计算;动画步骤仍然使用具有realX、realY坐标的目标对象,因此它仍将移出屏幕。通过更改持续时间,可以有效地加快速度。是的,这就是发生的情况,我只关心子弹的方向,如果计算r的线条ealX和realY确实与子弹的方向有关,因为如果我使用offX和offY,我会有相同的方向。非常感谢您的帮助:)