gpt4 book ai didi

iphone - 检测两个物体的碰撞,每个物体都有不同的形状

转载 作者:行者123 更新时间:2023-11-29 04:40:36 24 4
gpt4 key购买 nike

我想检测两个物体之间的碰撞,一个物体具有圆形形状,另一个物体具有凸形物体(通过使用顶点和顶点数量),并且我使用接触监听器类来检测物体之间的碰撞。我尝试检测两个圆形之间的碰撞并且成功了,但是当我尝试检测两个物体(一个具有凸形状,另一个具有圆形形状)之间的碰撞时,它失败的!即使我使用标签来识别这些尸体。

如何检测具有两种不同形状(圆形和凸形)的两个物体?

ContactListener.h代码:

#import "Box2D.h"

class ContactListener : public b2ContactListener
{
private:
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};

ContactListener.mm代码:

#import "ContactListener.h"
#import "SimpleAudioEngine.h"
#import "cocos2d.h"

void ContactListener::BeginContact(b2Contact* contact) {

b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();

CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();

if (spriteA != NULL && spriteB != NULL) {
if (spriteA.tag == 1 && spriteB.tag == 50) { // this is work (two shapes are circles

[[SimpleAudioEngine sharedEngine] playEffect:@"Pin.wav"];
NSLog(@"Contact With Pin is Beginning");
}

if (spriteA.tag == 1 && spriteB.tag == 51) { // this is not work (one is circle shape and another one is convex shape)

[[SimpleAudioEngine sharedEngine] playEffect:@"Jump.wav"];
NSLog(@"Contact With Barrier is Beginning");
}
}
}

void ContactListener::EndContact(b2Contact* contact) {

b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();

CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();

if (spriteA != NULL && spriteB != NULL) {

}
}

上面代码的标签:

  • 球(游戏主要 Sprite )形状的标签为 1
  • 圆形标签为50
  • 凸形标签为51

编辑:

这是对象主体创建的代码:

#define pinSpriteTag 50
#define barrierSpriteTag 51
#define ballSpriteTag 1

// Creating ball of game (has b2CircleShape)
-(void) createBallOfGameWithPositionX:(int)x yPosition:(int)y radius:(float)radius {

// Put ball on screen
ballOfGameSprite = [CCSprite spriteWithFile:@"GameBall-1-ipad.png"];
ballOfGameSprite.position = ccp(x, y);
[self addChild:ballOfGameSprite z:1 tag: ballSpriteTag];

// Create body of ball
b2BodyDef ballOfGameBodyDef;
ballOfGameBodyDef.type = b2_staticBody;
ballOfGameBodyDef.position.Set(ballOfGameSprite.position.x/PTM_RATIO, ballOfGameSprite.position.y/PTM_RATIO);
ballOfGameBodyDef.userData = ballOfGameSprite;
ballOfGameBody = world->CreateBody(&ballOfGameBodyDef);

// Create Physics properties of ball
b2CircleShape ballOfGameShape;
ballOfGameShape.m_radius = radius/PTM_RATIO;

// Create fixture of ball
b2FixtureDef ballOfGameFixtureDef;
ballOfGameFixtureDef.shape = &ballOfGameShape;
ballOfGameFixtureDef.density = 0.9f;
ballOfGameFixtureDef.friction = 1.0f;
ballOfGameFixtureDef.restitution = 0.9f;
ballOfGameBody->CreateFixture(&ballOfGameFixtureDef);

}

// Creating pin of game (has b2CircleShape)
-(void) createNormalPinOnScreenWithPositionX:(int)x yPosition:(int)y radius:(float)radius {

// Put ball on screen
CCSprite *pin = [CCSprite spriteWithFile:@"GamePinNormal-ipad.png"];
pin.position = ccp(x, y);
[self addChild:pin z:2 tag:pinSpriteTag];

// Create body of ball
b2BodyDef pinBodyDef;
pinBodyDef.type = b2_staticBody;
pinBodyDef.position.Set(pin.position.x/PTM_RATIO, pin.position.y/PTM_RATIO);
pinBodyDef.userData = pin;
b2Body *pinBody = world->CreateBody(&pinBodyDef);

// Create Physics properties of ball
b2CircleShape pinShape;
pinShape.m_radius = radius/PTM_RATIO;

// Create fixture of ball
b2FixtureDef pinFixtureDef;
pinFixtureDef.shape = &pinShape;
pinFixtureDef.density = 0.9f;
pinFixtureDef.friction = 1.0f;
pinFixtureDef.restitution = 0.85f;
pinBody->CreateFixture(&pinFixtureDef);
}

// Creating Barrier of game (has b2PolygonShape)
-(void) createBarrierOnScreenPositionX:(int)x yPosition:(int)y imageName:(NSString *)imageName verts:(b2Vec2*)verts verNum:(int)verNum {

CCSprite *BarrierOfGameSprite = [CCSprite spriteWithFile:imageName];
BarrierOfGameSprite.position = ccp(x, y);
[self addChild:BarrierOfGameSprite z:1 tag:barrierSpriteTag];

b2BodyDef BarrierOfGameBodyDef;
BarrierOfGameBodyDef.type = b2_staticBody;
BarrierOfGameBodyDef.position.Set(BarrierOfGameSprite.position.x/PTM_RATIO, BarrierOfGameSprite.position.y/PTM_RATIO);
BarrierOfGameBodyDef.userData = BarrierOfGameSprite;

b2Body *BarrierOfGameBody = world->CreateBody(&BarrierOfGameBodyDef);

b2PolygonShape BarrierOfGameShape;
BarrierOfGameShape.Set(verts, verNum);

b2FixtureDef BarrierOfGameFixtureDef;
BarrierOfGameFixtureDef.shape = &BarrierOfGameShape;
BarrierOfGameFixtureDef.density = 0.9f;
BarrierOfGameFixtureDef.friction = 1.0f;
BarrierOfGameFixtureDef.restitution = 0.0f;
BarrierOfGameBody->CreateFixture(&BarrierOfGameFixtureDef);

}

最佳答案

不检测两个形状之间的碰撞。最好的方法是将对象设置为body的userData。

@implemention RigidBody
-(id)init{
...
self.body->userData = self;
}

然后比较类和地址

void beginContact(b2contact contact){
RigidBody *A = (RigidBody*)contact->GetFixtureA()->GetBody()->GetUserData();
if([A isKindOfClass:[RigidBody class]]){

}
// Or
if(self == A){

}

希望对您有帮助。

关于iphone - 检测两个物体的碰撞,每个物体都有不同的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10456816/

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