我有一个地面物体和一个矛状物体(动态)。按下按钮时,将向线施加线速度。它工作正常,但有时,它会卡在地上。这种情况大部分时间(并非总是)发生在矛矛的对面与地面直接碰撞时。我附上了一些图片,所以你们得到了图片:
这是我的代码:
#import "HelloWorldLayer.h"
#define PTM_RATIO 32
@implementation HelloWorldLayer
+(CCScene *) scene{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init {
if( (self=[super init])) {
isSimulating = NO;
CGSize winSize = [CCDirector sharedDirector].winSize;
self.isAccelerometerEnabled = YES;
self.isTouchEnabled = YES;
// Create sprite and add it to the layer
_spear = [CCSprite spriteWithFile:@"image_SPEAR.png"];
_spear.position = ccp(40, 30);
[self addChild:_spear];
label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:20];
CGSize size = [[CCDirector sharedDirector] winSize];
label.position = ccp( size.width /2 , size.height/2 );
[self addChild: label];
CCMenuItem *starMenuItem = [CCMenuItemImage
itemFromNormalImage:@"ball.png" selectedImage:@"ball.png"
target:self selector:@selector(starButtonTapped:)];
starMenuItem.position = ccp(60, 250);
CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, -20.0f);
bool doSleep = true;
_world = new b2World(gravity, doSleep);
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = _world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
boxShapeDef.friction = 0.3f;
[self setup];
}
return self;
}
- (void)setup {
NSLog(@"Setting up...");
//set the sprite's initial position
_spear.position = ccp(40, 30);
_spear.rotation = 0.0f;
//row 1, col 1
int num = 7;
b2Vec2 verts[] = {
b2Vec2(-36.0f / PTM_RATIO, -2.7f / PTM_RATIO),
b2Vec2(20.1f / PTM_RATIO, -2.1f / PTM_RATIO),
b2Vec2(24.4f / PTM_RATIO, -4.6f / PTM_RATIO),
b2Vec2(36.7f / PTM_RATIO, -1.4f / PTM_RATIO),
b2Vec2(23.9f / PTM_RATIO, 2.7f / PTM_RATIO),
b2Vec2(20.7f / PTM_RATIO, 0.0f / PTM_RATIO),
b2Vec2(-36.0f / PTM_RATIO, -0.5f / PTM_RATIO)
};
// Create spear body and shape
b2BodyDef spearBodyDef;
spearBodyDef.type = b2_dynamicBody;
spearBodyDef.position.Set(40.0/PTM_RATIO, 30.0/PTM_RATIO);
//spearBodyDef.angle = 45.0 * (180.0f/b2_pi);
spearBodyDef.userData = _spear;
_spearBody = _world->CreateBody(&spearBodyDef);
b2PolygonShape spearShape;
spearShape.Set(verts, num);
b2FixtureDef spearShapeDef;
spearShapeDef.shape = &spearShape;
spearShapeDef.density = 0.75f;
spearShapeDef.friction = 0.2f;
spearShapeDef.restitution = 0.2f;
_spearBody->CreateFixture(&spearShapeDef);
}
- (void)starButtonTapped:(id)sender {
if (isSimulating) {
NSLog(@"Not simulating now...");
[self unschedule:@selector(tick:)];
_world->DestroyBody(_spearBody);
_spearBody = NULL;
[self setup];
} else {
NSLog(@"Simulating now...");
[self schedule:@selector(tick:)];
float angle = _spearBody->GetAngle();
b2Vec2 force;
force.Set(cos(angle) * 15.0f , sin(angle) * 15.0f);
_spearBody->SetLinearVelocity(force);
}
isSimulating = !isSimulating;
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!isSimulating) {
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
float angleRadians = atanf((float)location.y / (float)location.x);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
_spear.rotation = -1 * angleDegrees;
_spearBody->SetTransform(_spearBody->GetPosition(), angleRadians);
[label setString:[NSString stringWithFormat:@"Angle: %f X: %f Y:%f", angleDegrees, location.x, location.y]];
NSLog(@"%@", @"touched");
}
}
- (void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *ballData = (CCSprite *)b->GetUserData();
ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
- (void) dealloc {
delete _world;
_world = NULL;
[super dealloc];
}
@end
我猜它与摩擦和恢复有关但我已经尝试了很多值,没有什么能让这种行为消失。感谢。
**更新:**我弄明白是什么导致了这一点。每当它被卡住时,它就是当矛离开地面时。每次。但是,为什么它首先会超出groundBody?这是一张显示此图片的图片。在底部,长矛在身体之外:
答案 0 :(得分:3)
尝试关闭睡觉......
bool doSleep = false;
_world = new b2World(gravity, doSleep);
我在我的一个基于box2d的游戏中遇到了类似的问题,而这个“功能”就是造成它的原因。
答案 1 :(得分:2)
请注意,在最新版本的Cocos2D中,您需要使用以下内容来防止这种粘连行为:
_world->SetAllowSleeping(false);
答案 2 :(得分:1)
你的长矛形状不凸。我强烈建议您使用Box2D的调试绘图功能来不时检查事物。 http://www.iforce2d.net/b2dtut/debug-draw
这是多边形的真实结果 - 线条是你指定的,阴影区域是你得到的:
答案 3 :(得分:1)
实际上box2D有一个迭代求解器。因此,模拟的质量直接取决于迭代数量。在制作Step
时尝试增加位置和/或速度迭代。还要检查您是否使用固定时间步骤(通常它会产生更好的结果)。此外,你的矛快速移动尝试启用连续物理并将长矛体设置为子弹(b2BodyDef属性)。