gpt4 book ai didi

ios - 在 Sprite Cocos2d 上添加 box2d Body

转载 作者:行者123 更新时间:2023-11-29 10:53:16 24 4
gpt4 key购买 nike

我有一个桶,我想在其中添加 box2d body 。不是在整个桶上,而是在左边、右边和底部,这样我就可以把球扔进桶里。这是我的桶。我在桶的左侧和右侧添加了 box2d 主体。像这样

enter image description here

它对我来说工作正常但是当我在底部添加 body 时我的游戏崩溃了。

这是我在 bucket 上添加 2 个 body 以及评论 bottom body 的代码。

-(b2Body *) createBucket
{
CCSprite* bucket = [CCSprite spriteWithFile:@"simple-bucket-md.png"];
[self addChild:bucket z:3];

b2Body* b_bucket;

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

b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
CGPoint startPos = ccp(400,150);
bodyDef.position = [self toMeters:startPos];
bodyDef.userData = bucket;
bodyDef.gravityScale = 0;

b2PolygonShape dynamicBox;
//----------------------------------
// THis IS body for Left Side
//----------------------------------
int num = 5;
b2Vec2 verts[] = {
b2Vec2(-29.1f / PTM_RATIO, 25.0f / PTM_RATIO),
b2Vec2(-25.4f / PTM_RATIO, -14.9f / PTM_RATIO),
b2Vec2(-18.7f / PTM_RATIO, -14.9f / PTM_RATIO),
b2Vec2(-21.8f / PTM_RATIO, 26.7f / PTM_RATIO),
b2Vec2(-28.9f / PTM_RATIO, 25.1f / PTM_RATIO)
};

dynamicBox.Set(verts, num);

b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.friction = 0.7;
fixtureDef.density = 10.0f;
fixtureDef.restitution = 0.7;

b_bucket = world->CreateBody(&bodyDef);
b_bucket->CreateFixture(&fixtureDef);
//----------------------------------
// THis IS body for Right Side
//----------------------------------
int num1 = 5;
b2Vec2 verts1[] = {
b2Vec2(16.8f / PTM_RATIO, 27.0f / PTM_RATIO),
b2Vec2(15.9f / PTM_RATIO, -11.5f / PTM_RATIO),
b2Vec2(22.1f / PTM_RATIO, -10.7f / PTM_RATIO),
b2Vec2(24.6f / PTM_RATIO, 26.9f / PTM_RATIO),
b2Vec2(16.9f / PTM_RATIO, 26.7f / PTM_RATIO)
};


dynamicBox.Set(verts1, num1);
fixtureDef.shape = &dynamicBox;
b_bucket-> CreateFixture(&fixtureDef);

//----------------------------------
// THis IS body for Bottom
//----------------------------------
/*
int num2 = 5;
b2Vec2 verts2[] = {
b2Vec2(-23.0f / PTM_RATIO, -21.6f / PTM_RATIO),
b2Vec2(18.9f / PTM_RATIO, -21.0f / PTM_RATIO),
b2Vec2(18.2f / PTM_RATIO, -26.1f / PTM_RATIO),
b2Vec2(-22.8f / PTM_RATIO, -25.9f / PTM_RATIO),
b2Vec2(-23.0f / PTM_RATIO, -21.7f / PTM_RATIO)
};
dynamicBox.Set(verts2, num2);
fixtureDef.shape = &dynamicBox;
b_bucket-> CreateFixture(&fixtureDef);
*/

return b_bucket;
}

最佳答案

您的顶点是顺时针排列的,而它们应该是逆时针排列的。

You must create polygons with a counter clockwise winding (CCW). We must be careful because the notion of CCW is with respect to a right-handed coordinate system with the z-axis pointing out of the plane. This might turn out to be clockwise on your screen, depending on your coordinate system conventions.

Counter clock-wise vertices

此外,为了创建一个 4 边形的多边形(这似乎是您正在尝试的),您只需要 4 个顶点。尝试这样的事情。-

int num2 = 4;
b2Vec2 verts2[] = {
b2Vec2(-22.8f / PTM_RATIO, -25.9f / PTM_RATIO),
b2Vec2(18.2f / PTM_RATIO, -26.1f / PTM_RATIO),
b2Vec2(18.9f / PTM_RATIO, -21.0f / PTM_RATIO),
b2Vec2(-23.0f / PTM_RATIO, -21.6f / PTM_RATIO)
};
dynamicBox.Set(verts2, num2);
fixtureDef.shape = &dynamicBox;
b_bucket-> CreateFixture(&fixtureDef);

请注意,您还可以删除侧体的第五个顶点。

关于ios - 在 Sprite Cocos2d 上添加 box2d Body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19582260/

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