Ios 如何计算物体的移动距离

Ios 如何计算物体的移动距离,ios,cocos2d-iphone,box2d,Ios,Cocos2d Iphone,Box2d,在我的cocos2d项目中,我在游戏板上有游戏芯片。我用b2MouseJoint在电路板上移动芯片。 要移动芯片,我使用以下代码 -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (_mouseJoint == NULL) return; UITouch *myTouch = [touches anyObject]; CGPoint location =

在我的cocos2d项目中,我在游戏板上有游戏芯片。我用b2MouseJoint在电路板上移动芯片。 要移动芯片,我使用以下代码

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

        if (_mouseJoint == NULL) return;

        UITouch *myTouch = [touches anyObject];
        CGPoint location = [myTouch locationInView:[myTouch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
        b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
        _mouseJoint->SetTarget(locationWorld);


}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

        if (_mouseJoint != NULL) return;
        UITouch* touch = [touches anyObject];
        CGPoint startPoint = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
        b2Vec2 locationWorld = b2Vec2(startPoint.x/PTM_RATIO, startPoint.y/PTM_RATIO);
        _touchedBody = [self getBodyAtLocation:locationWorld];

            if(_touchedBody != NULL)
            {
                b2MouseJointDef md;
                md.bodyA = _groundBody;
                md.bodyB = _touchedBody;
                md.target = locationWorld;
                md.collideConnected = true;
                md.frequencyHz = 4.0f;
                md.maxForce = 900000.0f * _touchedBody->GetMass();
                _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
                _touchedBody->SetAwake(true);

            }         


  }

所以,我需要计算我的身体的移动距离。请帮帮我

调用CCTouchesStart时只需存储对象的位置,然后在ccTouchesMoved完成后使用最终位置来计算差异。

调用CCTouchesStart时只需存储对象的位置,然后在ccTouchesMoved完成后使用最终位置来计算差异。

只要在调用CCtouchesBegind时存储对象的位置,然后在ccTouchesMoved完成后使用最终位置来计算差异。

只要在调用CCtouchesBegind时存储对象的位置,然后在完成ccTouchesMoved后使用最终位置来计算差值。

它不仅会计算两点之间的距离

在标题中:

NSMutableArray *pathPoints;
在init上:

pathPoints = [[[NSMutableArray alloc] init] retain];
开始:

[pathPoints addObject:[NSValue valueWithCGPoint:startPoint]];
ccTouchesMoved:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];
请注意:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];

CGPoint prevPoint = CGPointZero;
float distanceOfTravel = 0;
for(NSValue *v in pathPoints)
{
    if(CGPointEqualToPoint(prevPoint, CGPointZero))
    {
        prevPoint = [v CGPointValue];
        continue;
    }

    CGPoint curPoint = [v CGPointValue];
    distanceOfTravel += ccpDistance(prevPoint, curPoint);
}
NSLog(@"Distance:%f",distanceOfTravel);
[pathPoints removeAllObjects];
解除锁定:

[pathPoints release];

它不仅计算两点之间的距离

在标题中:

NSMutableArray *pathPoints;
在init上:

pathPoints = [[[NSMutableArray alloc] init] retain];
开始:

[pathPoints addObject:[NSValue valueWithCGPoint:startPoint]];
ccTouchesMoved:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];
请注意:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];

CGPoint prevPoint = CGPointZero;
float distanceOfTravel = 0;
for(NSValue *v in pathPoints)
{
    if(CGPointEqualToPoint(prevPoint, CGPointZero))
    {
        prevPoint = [v CGPointValue];
        continue;
    }

    CGPoint curPoint = [v CGPointValue];
    distanceOfTravel += ccpDistance(prevPoint, curPoint);
}
NSLog(@"Distance:%f",distanceOfTravel);
[pathPoints removeAllObjects];
解除锁定:

[pathPoints release];

它不仅计算两点之间的距离

在标题中:

NSMutableArray *pathPoints;
在init上:

pathPoints = [[[NSMutableArray alloc] init] retain];
开始:

[pathPoints addObject:[NSValue valueWithCGPoint:startPoint]];
ccTouchesMoved:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];
请注意:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];

CGPoint prevPoint = CGPointZero;
float distanceOfTravel = 0;
for(NSValue *v in pathPoints)
{
    if(CGPointEqualToPoint(prevPoint, CGPointZero))
    {
        prevPoint = [v CGPointValue];
        continue;
    }

    CGPoint curPoint = [v CGPointValue];
    distanceOfTravel += ccpDistance(prevPoint, curPoint);
}
NSLog(@"Distance:%f",distanceOfTravel);
[pathPoints removeAllObjects];
解除锁定:

[pathPoints release];

它不仅计算两点之间的距离

在标题中:

NSMutableArray *pathPoints;
在init上:

pathPoints = [[[NSMutableArray alloc] init] retain];
开始:

[pathPoints addObject:[NSValue valueWithCGPoint:startPoint]];
ccTouchesMoved:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];
请注意:

[pathPoints addObject:[NSValue valueWithCGPoint:location]];

CGPoint prevPoint = CGPointZero;
float distanceOfTravel = 0;
for(NSValue *v in pathPoints)
{
    if(CGPointEqualToPoint(prevPoint, CGPointZero))
    {
        prevPoint = [v CGPointValue];
        continue;
    }

    CGPoint curPoint = [v CGPointValue];
    distanceOfTravel += ccpDistance(prevPoint, curPoint);
}
NSLog(@"Distance:%f",distanceOfTravel);
[pathPoints removeAllObjects];
解除锁定:

[pathPoints release];

使用
previousLocationInView:
和毕达哥拉斯定理(特别是C标准库中的
hypot()
函数)。H2CO3,很好的解决方案。谢谢。通过谷歌搜索,我可以更快地找到它,我想…@H2CO3如果我查询“物体的移动距离”,我会得到这个作为首要答案,还有4个不相关的答案。如果我向谷歌查询同样的问题,我会得到100多个不相关的问答,但不是这一个。:)使用
previousLocationInView:
和毕达哥拉斯定理(特别是C标准库中的
hypot()
函数)。H2CO3,很好的解决方案。谢谢。通过谷歌搜索,我可以更快地找到它,我想…@H2CO3如果我查询“物体的移动距离”,我会得到这个作为首要答案,还有4个不相关的答案。如果我向谷歌查询同样的问题,我会得到100多个不相关的问答,但不是这一个。:)使用
previousLocationInView:
和毕达哥拉斯定理(特别是C标准库中的
hypot()
函数)。H2CO3,很好的解决方案。谢谢。通过谷歌搜索,我可以更快地找到它,我想…@H2CO3如果我查询“物体的移动距离”,我会得到这个作为首要答案,还有4个不相关的答案。如果我向谷歌查询同样的问题,我会得到100多个不相关的问答,但不是这一个。:)使用
previousLocationInView:
和毕达哥拉斯定理(特别是C标准库中的
hypot()
函数)。H2CO3,很好的解决方案。谢谢。通过谷歌搜索,我可以更快地找到它,我想…@H2CO3如果我查询“物体的移动距离”,我会得到这个作为首要答案,还有4个不相关的答案。如果我向谷歌查询同样的问题,我会得到100多个不相关的问答,但不是这一个。:)维塔利,谢谢你的回复。我发现你的答案对我很有用。但我认为,这个数组在这种情况下是不必要的。足够在标题中声明行程变量的距离并存储上一点。Vitaly,谢谢您的回复。我发现你的答案对我很有用。但我认为,这个数组在这种情况下是不必要的。足够在标题中声明行程变量的距离并存储上一点。Vitaly,谢谢您的回复。我发现你的答案对我很有用。但我认为,这个数组在这种情况下是不必要的。足够在标题中声明行程变量的距离并存储上一点。Vitaly,谢谢您的回复。我发现你的答案对我很有用。但我认为,这个数组在这种情况下是不必要的。足够在标题中声明Travel变量的距离并存储上一个点。