[SOLVED] Body moves away from sprite if sensor is false

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:

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!!
Quote:
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.

To hazard a guess, it could actually be the same topic... there is no reason setting a fixture to be a sensor should have any effect on a sprite being rendered. It sounds like you might be inadvertently overwriting memory somewhere, or casting a pointer incorrectly maybe.
irresistible force wrote:
Quote:
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.

To hazard a guess, it could actually be the same topic... there is no reason setting a fixture to be a sensor should have any effect on a sprite being rendered. It sounds like you might be inadvertently overwriting memory somewhere, or casting a pointer incorrectly maybe.


I only create the fixture once in a single place and I was playing with sensors before, however, I forgot to disable this one and when I went to set to it false or remove the line I got this issue.

The memory issue is happening because I couldn't find yet a better way to remove elements from 3 different vectors, which are used by some other classes, based on collision. So, whenever I delete the pointer after the world.step(), I get nullptr on collision listener on the next step, even after I've cleared the listener list up, my list of collisions, somehow the zombies ptrs are colliding and coming back to the collision listener. Well, I need to post the full code here for this, I'm trying to write small version of it..
I managed to fix this problem. It actually was my mistake, I was creating 2 bodies for the same sprite.
I also fixed most of my memory issue finding the spot where the problem was such as mistakenly incrementing an iterator before calling update(draw, etc) on the bodies/sprites. It should be the opposite, of course, but copy and paste has it's problems some times.. Also, huge "if statement conditions" that sometimes won't meet all the conditions, however if they are split in separate if statements the conditions will work..
I guess posting the message here helped me a lot to get my head away from my code for a bit and think about it.. and thank irresistible force for trying to help me.

Original thread: https://box2d.org/forum/viewtopic.php?t=9683