gpt4 book ai didi

ios - 在 cocos2d 中销毁 box2d 主体

转载 作者:行者123 更新时间:2023-11-29 13:06:38 25 4
gpt4 key购买 nike

我有一张网和一些昆虫,我正在扔网并与昆虫进行碰撞检测并摧毁它们。我使用 bod2d 进行碰撞检测。

世界法

-(void)createWorld
{

// Define the gravity vector.
b2Vec2 b_gravity;
b_gravity.Set(0.0f, -9.8f);

// Do we want to let bodies sleep?
// This will speed up the physics simulation
bool doSleep = true;

// Construct a world object, which will hold and simulate the rigid bodies.
world = new b2World(b_gravity);
world->SetAllowSleeping(doSleep);

world->SetContinuousPhysics(true);

}

创建网站

-(void) createWeb
{
freeBodySprite = [CCSprite spriteWithFile:@"web1.png"];
[self addChild:freeBodySprite z:2 tag:TAG_WEB];

CGPoint startPos = CGPointMake(100, 320/1.25);

bodyDef.type = b2_staticBody;
bodyDef.position = [self toMeters:startPos];
bodyDef.userData = freeBodySprite;

float radiusInMeters = ((freeBodySprite.contentSize.width * freeBodySprite.scale/PTM_RATIO) * 1.0f);
shape.m_radius = radiusInMeters;

fixtureDef.shape = &shape;
fixtureDef.density = 0.01f;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.1f;

circularObstacleBody = world->CreateBody(&bodyDef);
stoneFixture = circularObstacleBody->CreateFixture(&fixtureDef);
freeBody = circularObstacleBody;
}

我用 spritesheet 创建了我的昆虫动画,然后为那个 Sprite 创建了 b2body

-(b2Body *) createMovingBoxObstacle
{


//set this to avoid updating this object in the tick schedule
_ants.userData = (void *)YES;

b2BodyDef bodyDef_Ant;
bodyDef_Ant.type = b2_dynamicBody;
CGPoint startPos = ccp(520,winSize.height/7.6);
bodyDef_Ant.position = [self toMeters:startPos];
bodyDef_Ant.userData = _ants;

b2PolygonShape dynamicBox;
float tileWidth = ((_ants.contentSize.width * _ants.scale/PTM_RATIO) * 0.5f);
float tileHeight = ((_ants.contentSize.height * _ants.scale/PTM_RATIO) * 0.5f);
dynamicBox.SetAsBox(tileWidth, tileHeight);

b2FixtureDef fixtureDef_Ant;
fixtureDef_Ant.shape = &dynamicBox;
fixtureDef_Ant.friction = 0.7;
fixtureDef_Ant.density = 0.1f;
fixtureDef_Ant.restitution = 0.7;


staticBody = world->CreateBody(&bodyDef_Ant);
staticBody->CreateFixture(&fixtureDef_Ant);

VAMovingObstacle* moveableObject = [[VAMovingObstacle alloc] init];
moveableObject.startPoint = ccp(520,winSize.height/7.6);
moveableObject.endPoint = ccp(-50,winSize.height/7.6);
moveableObject.transitionTime = 20.0;
moveableObject.breakTime = 1.0;

moveableObject.obstacleSprite = _ants;
moveableObject.physicalBody = staticBody;
[moveableObject startMovement];

if (!movingObstacles) {
movingObstacles = [[NSMutableArray alloc] init];
}
[movingObstacles addObject:moveableObject];
[moveableObject release];
return staticBody;
}

VAMovingObstacle 是一个帮助 sprite 和 b2body 从左向右移动(runaction)的类

这是我的联系人监听器的 EndContact 方法

void ContactListener::EndContact(b2Contact* contact)
{
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();

if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();


if (spriteA.tag == TAG_WEB && spriteB.tag >= TAG_ANT) {

}

else if (spriteA.tag >= TAG_ANT && spriteB.tag == TAG_WEB) {

[spriteA removeFromParentAndCleanup:YES];
[[HelloWorldLayer sharedLevel] WebCollisionWithInsect:bodyA];
}
}
}

当碰撞发生时,EndContact 方法调用 WebCollisionWithInsect:bodyA

-(void) WebCollisionWithInsect:(b2Body*) bodyT{
world->DestroyBody(bodyT);
}

在 world->DestroyBody(bodyT) 行它给出一个错误

Assertion failed: (IsLocked() == false), function DestroyBody, file /Users/libs/Box2D/Dynamics/b2World.cpp, line 134.

知道我做错了什么。 ?

已编辑我在 EndContact 中添加了这一行

destroyCollisionDetectionBody = objBody;
didInsectCollideWithWeb = YES;

保留我想销毁的 body 的引用。现在我可以在 if check 上用 tick 方法破坏我的 body 在 tick 方法中添加了这些行

if(didInsectCollideWithWeb)
{
[self unschedule:@selector(tick:)];
[freeBodySprite removeFromParentAndCleanup:YES]; // this is my web
world->DestroyBody(destroyCollisionDetectionBody);
world->DestroyBody(freeBody);
[self createWeb];
[self schedule:@selector(tick:)];
didInsectCollideWithWeb = NO;

}

但现在的问题是,当我破坏我的网络时,即自由体。我无法重新创建它。正如我之前通过调用 [self createWeb] 所做的那样;

最佳答案

您正试图销毁一个正在模拟阶段使用的 box2d 对象(因为在该阶段调用了 EndContact)。模拟阶段是调用世界的 step 方法时执行的阶段。

因此,您应该做的是保持对要删除的 body 对象的引用,并在 box2d 模拟阶段之后(在 step 方法返回之后)执行 WebCollisionWithInsect拥有想要的身材

关于ios - 在 cocos2d 中销毁 box2d 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18463018/

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