B2body 在撞击传感器时向上移动而不是在直线路径上行走
Posted
技术标签:
【中文标题】B2body 在撞击传感器时向上移动而不是在直线路径上行走【英文标题】:B2body moving up when hitting sensor rather than walking on straight path 【发布时间】:2013-11-11 06:26:05 【问题描述】:我有一些昆虫在路上行走,我也添加了传感器。 当它与身体碰撞时,它会破坏身体并且工作正常。
但是当球击中昆虫的传感器时,有时我的昆虫身体开始向上移动。
在打昆虫之前
击中昆虫后
这是我的昆虫身体代码实现
-(b2Body *) createMovingAntObstacle :(int) sPosX startPositionY :(int) sPosY transitionTime:(float)speed
NSLog(@"TAG_ANT");
//set this to avoid updating this object in the tick schedule
_currentAnimatedSprite.userData = (void *)YES;
b2BodyDef bodyDef_Ant;
bodyDef_Ant.type = b2_dynamicBody;
CGPoint startPos = ccp(520,winSize.height/7);
bodyDef_Ant.position = [self toMeters:startPos];
bodyDef_Ant.userData = _currentAnimatedSprite;
bodyDef_Ant.gravityScale = 0;
b2PolygonShape dynamicBox;
int num = 5;
b2Vec2 verts[] =
b2Vec2(-19.0f / PTM_RATIO, 1.4f / PTM_RATIO),
b2Vec2(-18.5f / PTM_RATIO, -4.0f / PTM_RATIO),
b2Vec2(8.8f / PTM_RATIO, -3.7f / PTM_RATIO),
b2Vec2(7.9f / PTM_RATIO, 2.1f / PTM_RATIO),
b2Vec2(-18.5f / PTM_RATIO, 1.8f / PTM_RATIO)
;
dynamicBox.Set(verts, num);
b2FixtureDef fixtureDef_Ant;
fixtureDef_Ant.shape = &dynamicBox;
fixtureDef_Ant.friction = 0.7;
fixtureDef_Ant.density = 10.0f;
fixtureDef_Ant.restitution = 0.7;
antBody = world->CreateBody(&bodyDef_Ant);
antBody->CreateFixture(&fixtureDef_Ant);
/*** Begin Tutorial Code ***/
b2Filter defaultFilter = antBody->GetFixtureList()[0].GetFilterData();
defaultFilter.groupIndex = k_indexD; // Negative index (Never collides with same Index group objects)
defaultFilter.categoryBits = k_dCategory;
defaultFilter.maskBits = k_dMask; // HITS EVERYTHING EXCEPT A & C
antBody->GetFixtureList()[0].SetFilterData(defaultFilter);
float padding = 1.0f;
// RIGHT SENSOR
b2PolygonShape shape2;
int num2= 4;
b2Vec2 vertices2[] =
b2Vec2( (_currentAnimatedSprite.contentSize.width / 2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO), //top right corner (borders source image)
b2Vec2( (_currentAnimatedSprite.contentSize.width / 2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 )/ PTM_RATIO), //bottom right corner (borders source image)
b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9 * padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 )/ PTM_RATIO), //bottom right corner (farthest out
b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9 * padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO) //top right
;
shape2.Set(vertices2, num2);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef2;
fixtureDef2.shape = &shape2;
fixtureDef2.density = 0.0f;
fixtureDef2.friction = 0.0f;
fixtureDef2.restitution = 0.0;
fixtureDef2.isSensor = YES;
fixtureDef2.userData = (SensorTypes*) rightSensor;
antBody-> CreateFixture(&fixtureDef2);
//add a new fixture as LEFT sensor
b2PolygonShape shape3;
int num3 = 4;
b2Vec2 vertices3[] =
b2Vec2( (_currentAnimatedSprite.contentSize.width / -2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 ) / PTM_RATIO), //bottom left corner (borders source image)
b2Vec2( (_currentAnimatedSprite.contentSize.width / -2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO), //top left corner (borders source image)
b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9 * -padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO), //top left corner
b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9 * -padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 ) / PTM_RATIO), //bottom left corner
;
shape3.Set(vertices3, num3);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef3;
fixtureDef3.shape = &shape3;
fixtureDef3.density = 0.1f;
fixtureDef3.isSensor = YES;
fixtureDef3.userData = (SensorTypes*) leftSensor;
antBody-> CreateFixture(&fixtureDef3);
VAMovingObstacle* moveableObject = [[VAMovingObstacle alloc] init];
moveableObject.startPoint = ccp(sPosX,sPosY);
moveableObject.endPoint = ccp(-100,sPosY);
moveableObject.transitionTime = speed;
moveableObject.breakTime = 1.0;
moveableObject.obstacleSprite = _currentAnimatedSprite;
moveableObject.physicalBody = antBody;
[moveableObject addAnimation:_currentWalkAction];
[moveableObject addMovement];
if (!movingObstacles_Ant)
movingObstacles_Ant = [[NSMutableArray alloc] init];
[movingObstacles_Ant addObject:moveableObject];
//[moveableObject release];
return antBody;
我是否必须改变密度、摩擦力或恢复力才能使其发挥作用?
【问题讨论】:
【参考方案1】:这个很简单我的朋友....我遇到了同样的问题...
去联系Listener类...
必要时这样做
if ((bodyA->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsCharacter || bodyA->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsBurger) && (bodyB->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsBurger || bodyB->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsCharacter))
contact->SetEnabled(false);
它对我有帮助...希望对你也有帮助
【讨论】:
以上是关于B2body 在撞击传感器时向上移动而不是在直线路径上行走的主要内容,如果未能解决你的问题,请参考以下文章
DOS命令将子目录中的所有文件向上移动一级而不覆盖相同的文件名,唯一的大小