Objective c Cocos2D设置精灵相对于其他精灵的位置

Objective c Cocos2D设置精灵相对于其他精灵的位置,objective-c,math,cocos2d-iphone,Objective C,Math,Cocos2d Iphone,我相信这对于任何一个懂数学和编程(Cocos2D)的人来说都是一个简单的问题,但说到数学,我就是村里的白痴 我的游戏有一艘飞船在太空中飞行。船停留在屏幕中央,相机随船移动。我试图添加从船上发射并以相同速度移动的抛射物,无论船以什么速度“移动” 为了弄清楚这一点,我一直在尝试在每一步都将(10.0,10.0)添加到投射精灵的位置。这是可行的(尽管它总是在左上角),但是如果我尝试将船或相机的运动添加到精灵位置以及(10.0,10.0),我会完全弄错 以下是我目前正在做的事情: CGPoint tem

我相信这对于任何一个懂数学和编程(Cocos2D)的人来说都是一个简单的问题,但说到数学,我就是村里的白痴

我的游戏有一艘飞船在太空中飞行。船停留在屏幕中央,相机随船移动。我试图添加从船上发射并以相同速度移动的抛射物,无论船以什么速度“移动”

为了弄清楚这一点,我一直在尝试在每一步都将(10.0,10.0)添加到投射精灵的位置。这是可行的(尽管它总是在左上角),但是如果我尝试将船或相机的运动添加到精灵位置以及(10.0,10.0),我会完全弄错

以下是我目前正在做的事情:

CGPoint tempStep = ccpAdd(self.position, ccp(10.0, 10.0));
CGPoint tempSubStep = ccpSub(self.position, player.currentLocation);
NSLog(@"Difference:  (%fx, %fy)", tempSubStep.x, tempSubStep.y);
CGPoint finalStep = ccpAdd(tempStep, tempSubStep);
//  CGPoint tempSubStep = ccpSub(tempStep, player.currentLocation);
//  CGFloat diff = ccpDistance(tempStep, player.currentLocation);
//  CGPoint tempSubStep = ccpAdd(tempStep, ccp(diff, diff));

self.position = finalStep;
被注释掉的东西是我一直在尝试的一些东西(尽管还有很多其他的东西)

  • self:子类CCSprite
  • player.currentLocation是player精灵的保留位置属性。属性是原子的并被赋值。如果这是错误的,请让我知道
我曾尝试将投射物添加到一个新层,并使用玩家精灵移动该层,但当玩家精灵移动(以及该层)时,我的投射物消失了。我试过CCLayerColor和CCParallaxNode

总之

我想从船的位置开一枪。我希望它以恒定的速度离开飞船,而不管玩家飞船在射击后的移动

示例:玩家正朝屏幕右上角移动(可以说),并朝同一方向射击。无论玩家的飞行速度有多快,玩家都不会超过投射物。或者,如果玩家突然转向相反的方向,投射物似乎不会突然加速,但似乎以相同的速度移动

谢谢你能提供的任何帮助

编辑:

好吧,我想我可能已经拿到了。真的很简单

CGPoint tempStep = ccpAdd(self.position, _direction);
CGPoint playerDiff = ccpSub(player.currentLocation, _lastKnownPlayerLocation);
CGPoint finalStep = ccpAdd(tempStep, playerDiff);

self.position = finalStep;

_lastKnownPlayerLocation = player.currentLocation;

现在,我只需要找出如何在一定范围内获得半径。

确认这是未来可能遇到此问题的所有人的完整解决方案:

// Add the direction you want the sprite to move in to it's current position
CGPoint tempStep = ccpAdd(self.position, _direction);
// Subtract the player's current location from it's last known position
CGPoint playerDiff = ccpSub(player.currentLocation, _lastKnownPlayerLocation);
// Add the projectile sprite's desired new position to the change in the player's location
CGPoint finalStep = ccpAdd(tempStep, playerDiff);
// Set the position of the projectile sprite
self.position = finalStep;

// Store the new location of the player in the buffer variable
_lastKnownPlayerLocation = player.currentLocation;

// Move the Box2D body to match the sprite position.
// Not that this will break physics for this body, but I have
// my body set as a sensor so it will still report to the contact
// listener.
b2Vec2 moveToPosition = b2Vec2(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
_body->SetTransform(moveToPosition, 0.0);
  • self是CCSprite的一个子类,因此self.position是精灵在屏幕上的位置
  • _LastKnownPlayerlLocation应该是明显的,但这是上次检查玩家精灵时的位置
  • _方向是从“模拟棒”传入的值。我得到角度并把它传给全班。该类使用ccpForAngle(float angle)获得一个CGPoint,然后使用ccpMult(CGPoint,float)将其乘以我想要的任何方向步长(表示希望快照以什么速度移动)
我的情况是很多事情都使用Box2D,所以最后两行与此相关


我希望这对某人有所帮助:)

确认这是未来可能遇到此问题的所有人的完整解决方案:

// Add the direction you want the sprite to move in to it's current position
CGPoint tempStep = ccpAdd(self.position, _direction);
// Subtract the player's current location from it's last known position
CGPoint playerDiff = ccpSub(player.currentLocation, _lastKnownPlayerLocation);
// Add the projectile sprite's desired new position to the change in the player's location
CGPoint finalStep = ccpAdd(tempStep, playerDiff);
// Set the position of the projectile sprite
self.position = finalStep;

// Store the new location of the player in the buffer variable
_lastKnownPlayerLocation = player.currentLocation;

// Move the Box2D body to match the sprite position.
// Not that this will break physics for this body, but I have
// my body set as a sensor so it will still report to the contact
// listener.
b2Vec2 moveToPosition = b2Vec2(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
_body->SetTransform(moveToPosition, 0.0);
  • self是CCSprite的一个子类,因此self.position是精灵在屏幕上的位置
  • _LastKnownPlayerlLocation应该是明显的,但这是上次检查玩家精灵时的位置
  • _方向是从“模拟棒”传入的值。我得到角度并把它传给全班。该类使用ccpForAngle(float angle)获得一个CGPoint,然后使用ccpMult(CGPoint,float)将其乘以我想要的任何方向步长(表示希望快照以什么速度移动)
我的情况是很多事情都使用Box2D,所以最后两行与此相关


我希望这对某人有所帮助:)

这里不要说这是离题(我认为这很好),但你可能会在gamedev.stackexchange.com上得到很好或更好的帮助……我甚至都不知道有这样的事情。这里不要说这离题(我认为这很好)但是你可能会在gamedev.stackexchange.comHuh上得到更好的帮助……我甚至都没意识到有这样的事情。