Ios 在COCOS2D3.1中拖放物理实体?
我是cocos2dv3.1的新手 如何在cocos2dv3.1中拖放物理体 然后,如何检查两个物理体之间的碰撞和检测Ios 在COCOS2D3.1中拖放物理实体?,ios,cocos2d-iphone,drag-and-drop,collision-detection,physics,Ios,Cocos2d Iphone,Drag And Drop,Collision Detection,Physics,我是cocos2dv3.1的新手 如何在cocos2dv3.1中拖放物理体 然后,如何检查两个物理体之间的碰撞和检测 提前感谢请尝试下面的碰撞检测链接。这将对你有帮助 http://www.cocos2d-x.org/wiki/Chapter_5_-_How_to_Detect_the_Collisions 首先,需要一个名为CCTouchJoint的类 CCTouchJoint.h #import "Box2D.h" @interface CCTouchJoint : NSObject
提前感谢请尝试下面的碰撞检测链接。这将对你有帮助
http://www.cocos2d-x.org/wiki/Chapter_5_-_How_to_Detect_the_Collisions
首先,需要一个名为CCTouchJoint的类 CCTouchJoint.h
#import "Box2D.h"
@interface CCTouchJoint : NSObject
{
@public
b2MouseJoint *mouseJoint;
UITouch *touch;
}
@property (assign) b2MouseJoint *mouseJoint;
@property (nonatomic, retain) UITouch *touch;
- (id)initLocal:(UITouch *)touch withMouseJoint:(b2MouseJoint *)mouseJoint;
+ (id)touch:(UITouch *)touch withMouseJoint:(b2MouseJoint *)mouseJoint;
// Public methods
/**
* Destroy the touch joint in the Box2d world.
*/
- (void)destroyTouchJoint;
@end
CCTouchJoint.mm
#import "CCTouchJoint.h"
@implementation CCTouchJoint
@synthesize mouseJoint;
@synthesize touch;
- (void)dealloc
{
[touch release];
[super dealloc];
}
- (id)initLocal:(UITouch *)_touch withMouseJoint:(b2MouseJoint *)_mouseJoint
{
if ((self = [super init]))
{
self.touch = _touch;
mouseJoint = _mouseJoint;
}
return self;
}
+ (id)touch:(UITouch *)_touch withMouseJoint:(b2MouseJoint *)_mouseJoint
{
return [[self alloc] initLocal:_touch withMouseJoint:_mouseJoint];
}
#pragma mark -
#pragma mark CCTouchJoint Public Methods
- (void)destroyTouchJoint
{
if (mouseJoint != NULL)
{
mouseJoint->GetBodyA()->GetWorld()->DestroyJoint(mouseJoint);
}
}
#pragma mark CCTouchJoint Private Methods
@end
第二self.touchEnabled=是您需要
NSMutableArray*touchJointList;
b2Body*地基体和b2Body*b强>
此触摸代码:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
for(UITouch *touch in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 worldLoc = b2Vec2(ptm(location.x), ptm(location.y));
for (b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetType() == b2_dynamicBody)
for (b2Fixture *f = b->GetFixtureList(); f; f = f->GetNext())
{
// Hit!
if (f->TestPoint(worldLoc))
{
/// Mouse joint definition
b2MouseJointDef md;
md.bodyA = groundBody;
md.bodyB = b;
md.target = worldLoc;
md.maxForce = 3000.0 * b->GetMass();
md.dampingRatio = 5;
md.frequencyHz = 60;
// Joint of bodys
b2MouseJoint *m_touchJoint;
m_touchJoint = (b2MouseJoint *)world->CreateJoint(&md);
CCTouchJoint *tj = [CCTouchJoint touch:touch withMouseJoint:m_touchJoint];
[touchJointList addObject:tj];
b->SetAwake(true);
}
break;
}
}
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (CCTouchJoint *tj in touchJointList)
{
if([tj.touch phase] == UITouchPhaseMoved)
{
// Update if it is moved
CGPoint location = [tj.touch locationInView:tj.touch.view];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 worldLocation = b2Vec2(ptm(location.x), ptm(location.y));
tj.mouseJoint->SetTarget(worldLocation);
}
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
NSMutableArray *discardedItems = [NSMutableArray array];
for(UITouch *touch in allTouches)
{
for (CCTouchJoint *tj in touchJointList)
{
if (tj.touch == touch)
{
// Defensive programming - assertion
NSAssert([tj isKindOfClass:[CCTouchJoint class]], @"node is not a touchJoint!");
// If safe - loop through
if ([tj.touch phase] == UITouchPhaseEnded)
{
[discardedItems addObject:tj];
[tj destroyTouchJoint];
[tj release];
}
}
}
}
[touchJointList removeObjectsInArray:discardedItems];
}
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[touchJointList removeAllObjects];
}
记住:更改md.dampingRatio和md.frequencyHz会影响鼠标关节的行为。谢谢您的回复。但我在找iPhone,很抱歉最近的回复。谢谢你,现在我来看看。