gpt4 book ai didi

ios - 如何计算b2body对象的行进距离

转载 作者:行者123 更新时间:2023-11-28 20:11:22 24 4
gpt4 key购买 nike

在我的 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);

}


}

因此,我需要计算 _touchedBody 的移动距离。请帮我。

最佳答案

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

关于ios - 如何计算b2body对象的行进距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20058537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com