我正在尝试实现碰撞检测和一个精灵从多点触控旋转的另一个精灵的基于物理的反弹。在图像中,看到两个黄点是同时的触摸点,我已经根据ccTouchesBegan
方法和方法中的这些触摸计算了云的正确位置和角度ccTouchesMoved
。当用户移动和旋转这两个触摸时,云会跟随并相应地旋转。
当鸟与云碰撞时,我尝试将云的角度传递给另一种方法,CGRectIntersectsRect
并使用数学滚动我自己的碰撞/反弹,但这并没有产生预期的结果,所以我转向了 Box2D。
在阅读了 Ray Wenderlich 的 4 或 5 篇教程之后,我无法将使用多点触控云创建的运动与 Box2D 的主体、固定装置、鼠标关节和世界进行网格化。
我读过这个:Cocos2d - 旋转精灵的碰撞检测
...并且已经经历过:Ray Wenderlich 碰撞检测
我的最新ccTouchesMoved
方法如下所示:
- (void)ccTouchesBegan:(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);
if (_cloudFixture->TestPoint(locationWorld)) {
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _cloudBody;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 1000.0f * _cloudBody->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
_cloudBody->SetAwake(true);
}
NSSet *allTouches = [event allTouches];
if([allTouches count] == 2){
UITouch * touch1 = [[allTouches allObjects] objectAtIndex:0];
CGPoint location1 = [touch1 locationInView: [touch1 view]];
location1 = [[CCDirector sharedDirector] convertToGL:location1];
UITouch * touch2 = [[allTouches allObjects] objectAtIndex:1];
CGPoint location2 = [touch2 locationInView: [touch2 view]];
location2 = [[CCDirector sharedDirector] convertToGL:location2];
//EASIER TO WORK WITH INTS
int touch1X = location1.x;
int touch2X = location2.x;
int touch1Y = location1.y;
int touch2Y = location2.y;
//FIND THE LEFT-MOST TOUCH
int minX = (touch1X < touch2X) ? touch1X : touch2X;
int maxX = (minX == touch1X) ? touch2X : touch1X;
//FIND THE BOTTOM-MOST TOUCH
int minY = (touch1Y < touch2Y) ? touch1Y : touch2Y;
int maxY = (minY == touch1Y) ? touch2Y : touch1Y;
int touchXDiff = maxX - minX;
int touchYDiff = maxY - minY;
[_cloud setPosition:ccp(touchXDiff/2 + minX, touchYDiff/2 + minY)];
//ROTATE CLOUD AS IT MOVES
int offDiffX = touch1X - touch2X;
int offDiffY = touch1Y - touch2Y;
float angleRadians = atanf((float)offDiffY / (float)offDiffX);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cloudAngle = -1 * angleDegrees;
//[_cloud setRotation:cloudAngle];
_cloudBody->SetTransform( _cloudBody->GetPosition(), cloudAngle );
}
}
-(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];
NSSet *allTouches = [event allTouches];
if([allTouches count] == 2){
UITouch * touch1 = [[allTouches allObjects] objectAtIndex:0];
CGPoint location1 = [touch1 locationInView: [touch1 view]];
location1 = [[CCDirector sharedDirector] convertToGL:location1];
UITouch * touch2 = [[allTouches allObjects] objectAtIndex:1];
CGPoint location2 = [touch2 locationInView: [touch2 view]];
location2 = [[CCDirector sharedDirector] convertToGL:location2];
//EASIER TO WORK WITH INTS
int touch1X = location1.x;
int touch2X = location2.x;
int touch1Y = location1.y;
int touch2Y = location2.y;
//FIND THE LEFT-MOST TOUCH
int minX = (touch1X < touch2X) ? touch1X : touch2X;
int maxX = (minX == touch1X) ? touch2X : touch1X;
//FIND THE BOTTOM-MOST TOUCH
int minY = (touch1Y < touch2Y) ? touch1Y : touch2Y;
int maxY = (minY == touch1Y) ? touch2Y : touch1Y;
int touchXDiff = maxX - minX;
int touchYDiff = maxY - minY;
//ROTATE CLOUD AS IT MOVES
int offDiffX = touch1X - touch2X;
int offDiffY = touch1Y - touch2Y;
float angleRadians = atanf((float)offDiffY / (float)offDiffX);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cloudAngle = -1 * angleDegrees;
//BEFORE BOX2D
//[_cloud setRotation:cloudAngle];
//[_cloud setPosition:ccp(touchXDiff/2 + minX, touchYDiff/2 + minY)];
//WITH BOX2D
_cloudBody->SetTransform( _cloudBody->GetPosition(), cloudAngle );
b2Vec2 locationWorld = b2Vec2((touchXDiff/2 + minX)/PTM_RATIO, (touchYDiff/2 + minY)/PTM_RATIO);
_mouseJoint->SetTarget(locationWorld);
}
}
编辑(为清楚起见):发生的情况是,当实现 Box2D 代码时,云仅响应第一次触摸,不调整触摸之间创建的角度并且不跟随两次触摸。我怎样才能用 Box2D 做到这一点?
有没有人有什么建议?