我試圖實現碰撞檢測和一個精靈的基於物理的反彈關閉的多點觸控旋轉另一個精靈。在圖像中,看到的是,兩個黃色的點是同時的觸摸點,我已經計算出基於在ccTouchesBegan
方法那些觸摸以及所述ccTouchesMoved
方法雲中的適當的位置和角度。隨着用戶移動和旋轉兩次觸摸,雲隨之而來並相應旋轉。多點觸控雪碧旋轉帶衝突檢測
我嘗試雲計算的角度傳遞給另一個方法時鳥用CGRectIntersectsRect
和我自己的滾動排序碰撞/反彈了數學的雲相撞,但是這並沒有產生預期的結果,因此我轉向Box2D。
經過Ray Wenderlich教程的4或5之後,我很難將我用多點觸摸雲創建的運動與Box2D的body,fixture,mousejoints和world進行網格劃分。
我讀過這樣的:Cocos2d - Collision Detection of Rotated Sprite
...並且已經經歷了這一點:Ray Wenderlich Collision Detection
我最近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來實現這一點?
有沒有人有任何建議?
看不到任何的問題在這裏...只是大量的文字和代碼的... – Morion
通常的問題是締結一個「?」 ...... ......像畢竟文本和代碼:「有人有什麼建議嗎?」 也許你應該嘗試做一些有益的事情,而不是嘲諷一個真棒網站。 – d2burke