Hello Everyone. I'm box2d beginner.
Now, I'm making arcade game with box2d.
for example, player and enemy collision with each other.
Enemy has 1body and 3 shapes(top/left/right). I made them with vertex helper.
The problem.
When Player is collision with left shape.(Only collision with left shape with DebugDraw's View)
But Contact Listener judged that top/left/right shape is all contacted.
I don't know this situation. I need your help.
And I make a shape with fixturedef. This is pseudo code.
//step1 : body is maded.
//stpe2 : make a multiple shape
b2FixtureDef IIshapeDef;
for(int shape_cnt=0;shape_cnt<shapeCount;shape_cnt++ ) //In this case count is 3(top,left,right)
{
if(shape_info.vertex_node!=nil){
//Have a Vertex Information...
int num = [shape_info.vertex_node count];
b2Vec2 verts[num];
for(int i=0;i<num;i++){
CGPoint p=[[IIvertex objectAtIndex:shape_cnt] CGPointValue];
verts[i].Set(p.x, p.y);
}
IIshape.Set(verts, num);
IIshapeDef.userData = top or left or right; //shape_cnt=0->top
//shape_cnt=1->left
//shap3_cnt=2->right
IIshapeDef.density = IIuserData.IIdensity;
IIshapeDef.friction = IIuserData.IIfriction;
IIshapeDef.restitution = IIuserData.IIrestitution;
IIBody->CreateFixture(&IIshapeDef);
}
And Check Routine is contact listener.
void IIContactListener::BeginContact(b2Contact* contact) {
//check a collision shape
//contact->GetFixtureA()->GetUserData();
//contact->GetFixtureB()->GetUserData();
//TOAN : 04/27/2011. for-debugging.
//STEP1 : Get now fixture and fixture's userdata
b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();
IISprite *spriteA = (IISprite *) bodyA->GetUserData();
IISprite *spriteB = (IISprite *) bodyB->GetUserData();
if((spriteA.spriteTag==ENEMY)&&(spriteB.spriteTag==PLAYER))
{
NSLog(@"Listener:ENEMY/PLAYER is collision");
//The contact shpe is
NSLog(@"The shape A : %d", contact->GetFixtureA()->GetUserData());
NSLog(@"The shape A : %d", contact->GetFixtureB()->GetUserData());
}
if((spriteA.spriteTag==PLAYER)&&(spriteB.spriteTag==ENEMY))
{
NSLog(@"Listener:PLAYER/ENEmY is collision");
//The contact shape is
NSLog(@"The shape A : %d", contact->GetFixtureA()->GetUserData());
NSLog(@"The shape A : %d", contact->GetFixtureB()->GetUserData());
}
//step2 : player collision with top --> enemy died
//step3 : player collision with left or right -->player died
}
I move player with arrow button. and player contact with enemy's left shape.
In my opinion, In this case. contact is happened only left shape.
But Contact Listener checked all(left/top/bottom) is contacted.
Why Listener is called sequentially with left,top,right fixture information. Other fixture is not contact.
current siutation]
void IIContactListener::BeginContact(b2Contact* contact) //first call with left shape
void IIContactListener::BeginContact(b2Contact* contact) //second call with top shape
void IIContactListener::BeginContact(b2Contact* contact) //thrid callwith right shape
I will hope:
void IIContactListener::BeginContact(b2Contact* contact) //first call with left shape
Don't run contactListener with other Fixture's shape.
Because Current contact is only left shape.
Below is vertex information. The vertex is wrong??
Please Help Me...
PS.
top : 3 vertex node
left : 4 vertex node
right : 4 vertex node
//top.
NSArray *tmp_vertex1 = [[NSArray alloc]initWithObjects:
NSStringFromPoint(CGPointMake (14.8f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-14.9f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-1.1f / PTM_RATIO, 9.7f / PTM_RATIO)),
nil];
//right-bottom
NSArray *tmp_vertex2 = [[NSArray alloc]initWithObjects:
NSStringFromPoint(CGPointMake (14.8f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-1.1f / PTM_RATIO, 9.7f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-0.8f / PTM_RATIO, -13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (14.6f / PTM_RATIO, -13.8f / PTM_RATIO)),
nil];
//left-bottom
NSArray *tmp_vertex3 = [[NSArray alloc]initWithObjects:
NSStringFromPoint(CGPointMake (-1.1f / PTM_RATIO, 9.7f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-14.9f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-14.9f / PTM_RATIO, -12.9f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-0.8f / PTM_RATIO, -13.8f / PTM_RATIO)),
nil];
Now, I'm making arcade game with box2d.
for example, player and enemy collision with each other.
Enemy has 1body and 3 shapes(top/left/right). I made them with vertex helper.
The problem.
When Player is collision with left shape.(Only collision with left shape with DebugDraw's View)
But Contact Listener judged that top/left/right shape is all contacted.
I don't know this situation. I need your help.
And I make a shape with fixturedef. This is pseudo code.
//step1 : body is maded.
//stpe2 : make a multiple shape
b2FixtureDef IIshapeDef;
for(int shape_cnt=0;shape_cnt<shapeCount;shape_cnt++ ) //In this case count is 3(top,left,right)
{
if(shape_info.vertex_node!=nil){
//Have a Vertex Information...
int num = [shape_info.vertex_node count];
b2Vec2 verts[num];
for(int i=0;i<num;i++){
CGPoint p=[[IIvertex objectAtIndex:shape_cnt] CGPointValue];
verts[i].Set(p.x, p.y);
}
IIshape.Set(verts, num);
IIshapeDef.userData = top or left or right; //shape_cnt=0->top
//shape_cnt=1->left
//shap3_cnt=2->right
IIshapeDef.density = IIuserData.IIdensity;
IIshapeDef.friction = IIuserData.IIfriction;
IIshapeDef.restitution = IIuserData.IIrestitution;
IIBody->CreateFixture(&IIshapeDef);
}
And Check Routine is contact listener.
void IIContactListener::BeginContact(b2Contact* contact) {
//check a collision shape
//contact->GetFixtureA()->GetUserData();
//contact->GetFixtureB()->GetUserData();
//TOAN : 04/27/2011. for-debugging.
//STEP1 : Get now fixture and fixture's userdata
b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();
IISprite *spriteA = (IISprite *) bodyA->GetUserData();
IISprite *spriteB = (IISprite *) bodyB->GetUserData();
if((spriteA.spriteTag==ENEMY)&&(spriteB.spriteTag==PLAYER))
{
NSLog(@"Listener:ENEMY/PLAYER is collision");
//The contact shpe is
NSLog(@"The shape A : %d", contact->GetFixtureA()->GetUserData());
NSLog(@"The shape A : %d", contact->GetFixtureB()->GetUserData());
}
if((spriteA.spriteTag==PLAYER)&&(spriteB.spriteTag==ENEMY))
{
NSLog(@"Listener:PLAYER/ENEmY is collision");
//The contact shape is
NSLog(@"The shape A : %d", contact->GetFixtureA()->GetUserData());
NSLog(@"The shape A : %d", contact->GetFixtureB()->GetUserData());
}
//step2 : player collision with top --> enemy died
//step3 : player collision with left or right -->player died
}
I move player with arrow button. and player contact with enemy's left shape.
In my opinion, In this case. contact is happened only left shape.
But Contact Listener checked all(left/top/bottom) is contacted.
Why Listener is called sequentially with left,top,right fixture information. Other fixture is not contact.
current siutation]
void IIContactListener::BeginContact(b2Contact* contact) //first call with left shape
void IIContactListener::BeginContact(b2Contact* contact) //second call with top shape
void IIContactListener::BeginContact(b2Contact* contact) //thrid callwith right shape
I will hope:
void IIContactListener::BeginContact(b2Contact* contact) //first call with left shape
Don't run contactListener with other Fixture's shape.
Because Current contact is only left shape.
Below is vertex information. The vertex is wrong??
Please Help Me...
PS.
top : 3 vertex node
left : 4 vertex node
right : 4 vertex node
//top.
NSArray *tmp_vertex1 = [[NSArray alloc]initWithObjects:
NSStringFromPoint(CGPointMake (14.8f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-14.9f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-1.1f / PTM_RATIO, 9.7f / PTM_RATIO)),
nil];
//right-bottom
NSArray *tmp_vertex2 = [[NSArray alloc]initWithObjects:
NSStringFromPoint(CGPointMake (14.8f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-1.1f / PTM_RATIO, 9.7f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-0.8f / PTM_RATIO, -13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (14.6f / PTM_RATIO, -13.8f / PTM_RATIO)),
nil];
//left-bottom
NSArray *tmp_vertex3 = [[NSArray alloc]initWithObjects:
NSStringFromPoint(CGPointMake (-1.1f / PTM_RATIO, 9.7f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-14.9f / PTM_RATIO, 13.8f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-14.9f / PTM_RATIO, -12.9f / PTM_RATIO)),
NSStringFromPoint(CGPointMake (-0.8f / PTM_RATIO, -13.8f / PTM_RATIO)),
nil];