Hi everyone,
I'm having a problem and I don't know if it's a bug or my mistake.
I've some sprites that are attached to the user data of a b2bodies, nothing especial here.
I adapted a code to render a sort of debugdraw using SDL2 to make the bodies visible and it works fine. I can see the sprites within the square borders.
The problem is that some of the sprites are working fine with sensor false or true, but there is one that works only with sensor true. Based on what I read on the documentation isSensor is only used to trigger or not a collision. It shouldn't affect the body behaviour, I mean make it move if sensor is false or true.
This is the code with the sensor set to true:
b2Vec2 b2Position = b2Vec2((m_position.getX()/2*P2M)+(m_width/2*P2M),(m_position.getY()/2*P2M)+(m_height/2*P2M));
// Create sprite body
b2BodyDef turretBodyDef;
turretBodyDef.type = b2_dynamicBody;
turretBodyDef.position.Set(b2Position.x,b2Position.y);
//sprite position based on the body position
m_position.setX(M2P * b2Position.x - (m_width/2));
m_position.setY(M2P * b2Position.y - (m_height/2));
m_body = TheGame::Instance()->getB2World()->CreateBody(&turretBodyDef);
m_body->SetUserData(this);
// Create turret shape
b2PolygonShape turretShape;
turretShape.SetAsBox( P2M*m_width/4,P2M*m_height/4);
// Create shape definition and add to body
b2FixtureDef turretShapeDef;
turretShapeDef.shape = &turretShape;
turretShapeDef.density = 10.0f;
turretShapeDef.friction = 0.4f;
turretShapeDef.restitution = 0.1f;
turretShapeDef.isSensor=true;
m_body->CreateFixture(&turretShapeDef);
and this picture is the sprite with the body showing correctly:
Sorry had to upload it to google drive. The forum board didn't allow me to upload it here, saying my board quota as reached.. first time uploading.. lol
https://drive.google.com/file/d/0B6GYMMl1EY6PYy1TYXVISEZaMFE/edit?usp=sharing
and the same code with isSensor=false gives me this:
https://drive.google.com/file/d/0B6GYMMl1EY6PenFfLWl0WGRWT2s/edit?usp=sharing
I'd love to hear from you and see what do you think it's. I've gone through different versions of Box2D and same happens.
I'm having a hard time with collisions too, I've trying for about 2 weeks trying to get it working but it fails sometimes, not detecting collision and I'm having problems with memory issue. Doesn't matter what I do I end up with memory issue(nullptr) when deleting the bodies or sprites( in event of collision of or the screen). But, this is another topic. I'm trying to bring my code to a minimal to be able to post it here and ask for help.
Thanks for your help!!
I'm having a problem and I don't know if it's a bug or my mistake.
I've some sprites that are attached to the user data of a b2bodies, nothing especial here.
I adapted a code to render a sort of debugdraw using SDL2 to make the bodies visible and it works fine. I can see the sprites within the square borders.
The problem is that some of the sprites are working fine with sensor false or true, but there is one that works only with sensor true. Based on what I read on the documentation isSensor is only used to trigger or not a collision. It shouldn't affect the body behaviour, I mean make it move if sensor is false or true.
This is the code with the sensor set to true:
Code:
b2Vec2 b2Position = b2Vec2((m_position.getX()/2*P2M)+(m_width/2*P2M),(m_position.getY()/2*P2M)+(m_height/2*P2M));
// Create sprite body
b2BodyDef turretBodyDef;
turretBodyDef.type = b2_dynamicBody;
turretBodyDef.position.Set(b2Position.x,b2Position.y);
//sprite position based on the body position
m_position.setX(M2P * b2Position.x - (m_width/2));
m_position.setY(M2P * b2Position.y - (m_height/2));
m_body = TheGame::Instance()->getB2World()->CreateBody(&turretBodyDef);
m_body->SetUserData(this);
// Create turret shape
b2PolygonShape turretShape;
turretShape.SetAsBox( P2M*m_width/4,P2M*m_height/4);
// Create shape definition and add to body
b2FixtureDef turretShapeDef;
turretShapeDef.shape = &turretShape;
turretShapeDef.density = 10.0f;
turretShapeDef.friction = 0.4f;
turretShapeDef.restitution = 0.1f;
turretShapeDef.isSensor=true;
m_body->CreateFixture(&turretShapeDef);
and this picture is the sprite with the body showing correctly:
Sorry had to upload it to google drive. The forum board didn't allow me to upload it here, saying my board quota as reached.. first time uploading.. lol
https://drive.google.com/file/d/0B6GYMMl1EY6PYy1TYXVISEZaMFE/edit?usp=sharing
and the same code with isSensor=false gives me this:
https://drive.google.com/file/d/0B6GYMMl1EY6PenFfLWl0WGRWT2s/edit?usp=sharing
I'd love to hear from you and see what do you think it's. I've gone through different versions of Box2D and same happens.
I'm having a hard time with collisions too, I've trying for about 2 weeks trying to get it working but it fails sometimes, not detecting collision and I'm having problems with memory issue. Doesn't matter what I do I end up with memory issue(nullptr) when deleting the bodies or sprites( in event of collision of or the screen). But, this is another topic. I'm trying to bring my code to a minimal to be able to post it here and ask for help.
Thanks for your help!!