1

So I have a player (Box2D box) that can be moved horizontally with 'A' and 'D' using:

 SetLinearVelocity(b2Vec2(speed,  object.getB2Object()->GetLinearVelocity().y));

But when I move the box the player doesn't slide but roles on the corners like so:

enter image description here

I figured out that decreasing the friction of the box and the ground prevents this to some extent but still happens fairly regulary and also makes the player slide further when stopping. I have tried the function SetFixedRotation(true) but this seems to make the player stop moving all together, presumably at the points where it was going to rotate. I also tried SetAngularVelocity(0) but this still makes the box rotate, although very slowly. Is there another way to prevent the player from rotating when moving on a surface? Thanks

[Edit]

After doing what pingul suggested by setting the friction to zero the box hardly rotated. It looked like it was tripping on something every so often so, instead of 10 individual tiles for the floor I removed them and created a wide tile and the problem stopped! Therefore I think the box was tripping on the next tile along. My next problem then is why is this happening as I know the tiles all have the same height, width and position as shown here:

//the number of tiles
const unsigned int MAX_TILES = 10;
for(int i = 0; i < MAX_TILES; i++){
                                 //position                       ,size           ,density    ,linear_damping ,friction ,isdynamic,b2World                                    
    tiles.push_back(new GameObject(vec3(6+i-(MAX_TILES/2), -10, 1),  vec3(1, 1, 0),   1.0f    ,1.0f           ,0.5f      ,false   ,world_b2));
    tiles[tiles.size()-1]->Init(); //Some OpenGL stuff for rendering
    tiles[tiles.size()-1]->getMesh()->setTexture(test_sheet.getTexture(2,0)); //gets a texture from sprite sheet class
}

[Edit 2]

Math Library Using: GLM

Overlapping the tiles in the horizontal direction does nothing but I think it have something to do with this but in the vertical axis. When the player goes on to another tile, rotating or not, the y value changes slightly. This can be either up or down and only changing by +-0.003~. When jumping though it will go back to the very first value when the game starts on any of the floor tiles.

Cedric Reichenbach
  • 8,970
  • 6
  • 54
  • 89
MajesticHey
  • 128
  • 8

2 Answers2

1

There is two function in the body class (Box2D) that might help you with the undesirable rotation your getting The first one is :

void SetFixedRotation(bool flag)

Wish allow you to fix the rotation And the other one is:

void SetAngularDamping(float32 angularDamping)

wish will set an amount of angular resistance that depend on the argument you pass to the function I recommend reading the official Box2D documentation, 'Chapter 6 :Bodies' will give you more information about the position and angle (check page 29 and 30 Damping and Fixed Rotation) section or at least a quick look at box2d b2Body class I hope this is helpful for you

Omarito
  • 577
  • 6
  • 22
0

I do not know what kind of vector library you use, but I would assume vec3 takes floats or doubles as arguments. Converting vec3(6+i-(MAX_TILES/2) to a floating point number could lead to a small gap between the objects.

Can the objects overlap a small amount? What happens if you change the size to vec3(1.0001, 1, 0)?

pingul
  • 3,351
  • 3
  • 25
  • 43
  • Thanks for being patient. Added an Edit 2 – MajesticHey Nov 14 '16 at 18:37
  • @MajesticHey Hmm... Your tile code shouldn't be responsible for that discrepancy. What happens if you draw the bounding boxes of the different objects instead of the bitmaps? E.g. make the objects some very apparent colour and draw those instead. Also, could you show the code for moving your character? – pingul Nov 15 '16 at 09:14
  • @MajesticHey Did you find a solution to the problem? – pingul Nov 22 '16 at 11:25
  • In a way yes. Instead of using a box collider I created a function to add a capsule collider (2 circles with a square in the middle) and this fixed the problem. Sorry I took so long to reply, I haven't logged in in a while. I saw this and thoughtIi needed to reply so people in the future could have a solution if they had the same problem. – MajesticHey May 03 '17 at 19:44