Yeah, PolyMass will work, but you should copy the code and simplify it for your purpose. PolyMass is an internal function.
Looking for some pointers.
General DiscussionHi reko,
Thanks for your reply! I really appreciate it. I am also looking at making a 2D platformer.
Here are some new questions:
1) Did you use the default gravity as specified in the testbed examples, with a gravity vector of (0.0,10.0)?
2) What shape did you use to define your character? I used a simple rectangle, but it seems that it sometimes gets "stuck" in other shapes. Did you use a more complex shape or a circle?
And now some follow-up questions:
You are right that the speed of the character increases if the player keeps pressing the left or right arrow. I managed to create a status quo by using the correct mixture of linear damping. I got a decent forward movement with an impluse vector of (0.2,0) and a linear damping of 0.1. Is that close to your results? Did you end up using linear damping at all? I'm guessing you didn't..?
The problem I have with linear damping is that it also applies to vertical movement, so that a character suspended in air is falling extremely slow... so I guess my solution was not so brilliant.
I wonder why did you abandon SetLinearVelocity? It seems that it doesn't multiply the speed, but it keeps it constant...
Thanks for sharing the friction of 0.1, I tried and it works well. BTW do you use a density of 1.0 or other?
mmm. Linear damping seems to screw jumping movement altogether as it's not restricted to x or y axis.
BTW how did you check if the character is in the air? Is there a built in function for this?
Thanks again! I as well appreciate your effort. 😀
Thanks for your reply! I really appreciate it. I am also looking at making a 2D platformer.
Here are some new questions:
1) Did you use the default gravity as specified in the testbed examples, with a gravity vector of (0.0,10.0)?
2) What shape did you use to define your character? I used a simple rectangle, but it seems that it sometimes gets "stuck" in other shapes. Did you use a more complex shape or a circle?
And now some follow-up questions:
reko wrote:
2) There's not really the one and "right" way to do it how I see it. It really depends on what kind of simulation you want to do. I ended up using impulses to move my character, because the acceleration time is much shorter with it. It really depends on what you're after, and you really get the best results by experimenting.
3) Well this was a two part question. I already answered the "acceleration time to the max speed of the player is too slow" in my previous answer (the answer was to use an impulse instead of force). To limit the max speed, I simply check before applying an impulse whether the x component of GetLinearVelocity is too big; If it is I won't apply an impulse. This way other forces can still make the player go faster than the "max speed" of movement I've defined.
3) Well this was a two part question. I already answered the "acceleration time to the max speed of the player is too slow" in my previous answer (the answer was to use an impulse instead of force). To limit the max speed, I simply check before applying an impulse whether the x component of GetLinearVelocity is too big; If it is I won't apply an impulse. This way other forces can still make the player go faster than the "max speed" of movement I've defined.
You are right that the speed of the character increases if the player keeps pressing the left or right arrow. I managed to create a status quo by using the correct mixture of linear damping. I got a decent forward movement with an impluse vector of (0.2,0) and a linear damping of 0.1. Is that close to your results? Did you end up using linear damping at all? I'm guessing you didn't..?
The problem I have with linear damping is that it also applies to vertical movement, so that a character suspended in air is falling extremely slow... so I guess my solution was not so brilliant.
I wonder why did you abandon SetLinearVelocity? It seems that it doesn't multiply the speed, but it keeps it constant...
reko wrote:
4) Using an impulse instead of a force and having a small friction on the player solved this one. It's important to have small friction so your player moves smoothly on a ground and i.e. after jump won't "freeze" for a while before being apple to move again. I use a friction value of 0.1f for my player at the moment.
Thanks for sharing the friction of 0.1, I tried and it works well. BTW do you use a density of 1.0 or other?
reko wrote:
5) I solved this by damping the horizontal velocity by using GetLinearVelocity and SetLinearVelocity, but as stated in this topic, there's a setting to set linear damping for a body, so you probably want to try that first.
mmm. Linear damping seems to screw jumping movement altogether as it's not restricted to x or y axis.
BTW how did you check if the character is in the air? Is there a built in function for this?
Thanks again! I as well appreciate your effort. 😀
Thanks for the replies! This is exactly what I needed, I'm going to try implementing the technique the paper explains this weekend.
No, I don't remember which value I'm using right now (I'm at work and can't check), but I found the best value by experimenting.
I'm using a rectangle. My character also got stuck first, but small friction and big enough impulse for moving really made a difference for me. Also remember to be careful when designing your world to prevent making such shapes where the box can get stuck too easily.
You are right that the speed of the character increases if the player keeps pressing the left or right arrow. I managed to create a status quo by using the correct mixture of linear damping. I got a decent forward movement with an impluse vector of (0.2,0) and a linear damping of 0.1. Is that close to your results? Did you end up using linear damping at all? I'm guessing you didn't..?
The problem I have with linear damping is that it also applies to vertical movement, so that a character suspended in air is falling extremely slow... so I guess my solution was not so brilliant.
I wonder why did you abandon SetLinearVelocity? It seems that it doesn't multiply the speed, but it keeps it constant...
I'm not using linear damping at all right now. If the player falls too slow in the air, you can increase gravity, but I don't think this is really a good solution in a platformer because then bodies with no damping would work way differently. It'd be great if in future version of Box2D it'd be possible to specify linear damping seperately for x and y axis, by using a vector or such.
I didn't abandon SetLinearVelocity; That's what I'm using to damp the speed.
Thanks for sharing the friction of 0.1, I tried and it works well. BTW do you use a density of 1.0 or other?
I don't use density of 1.0 for the player since Box2D is tuned for meters/kilograms (and I didn't want to change the default units, as they work pretty well).
Check the contact list of the player's body to see if it's touching anything. The real thing is a bit more complicated than this though, because you might be sliding against a wall in air/touching another player/whatever. I haven't really spend much effort in this yet, but I doubt it'll be too difficult to define when the player is in air (check if the contact points the player has are in the bottom part of the player i.e. the feet).
Ygryggil wrote:
Hi reko,
1) Did you use the default gravity as specified in the testbed examples, with a gravity vector of (0.0,10.0)?
1) Did you use the default gravity as specified in the testbed examples, with a gravity vector of (0.0,10.0)?
No, I don't remember which value I'm using right now (I'm at work and can't check), but I found the best value by experimenting.
Quote:
2) What shape did you use to define your character? I used a simple rectangle, but it seems that it sometimes gets "stuck" in other shapes. Did you use a more complex shape or a circle?
I'm using a rectangle. My character also got stuck first, but small friction and big enough impulse for moving really made a difference for me. Also remember to be careful when designing your world to prevent making such shapes where the box can get stuck too easily.
Quote:
reko wrote:
2) There's not really the one and "right" way to do it how I see it. It really depends on what kind of simulation you want to do. I ended up using impulses to move my character, because the acceleration time is much shorter with it. It really depends on what you're after, and you really get the best results by experimenting.
3) Well this was a two part question. I already answered the "acceleration time to the max speed of the player is too slow" in my previous answer (the answer was to use an impulse instead of force). To limit the max speed, I simply check before applying an impulse whether the x component of GetLinearVelocity is too big; If it is I won't apply an impulse. This way other forces can still make the player go faster than the "max speed" of movement I've defined.
3) Well this was a two part question. I already answered the "acceleration time to the max speed of the player is too slow" in my previous answer (the answer was to use an impulse instead of force). To limit the max speed, I simply check before applying an impulse whether the x component of GetLinearVelocity is too big; If it is I won't apply an impulse. This way other forces can still make the player go faster than the "max speed" of movement I've defined.
You are right that the speed of the character increases if the player keeps pressing the left or right arrow. I managed to create a status quo by using the correct mixture of linear damping. I got a decent forward movement with an impluse vector of (0.2,0) and a linear damping of 0.1. Is that close to your results? Did you end up using linear damping at all? I'm guessing you didn't..?
The problem I have with linear damping is that it also applies to vertical movement, so that a character suspended in air is falling extremely slow... so I guess my solution was not so brilliant.
I wonder why did you abandon SetLinearVelocity? It seems that it doesn't multiply the speed, but it keeps it constant...
I'm not using linear damping at all right now. If the player falls too slow in the air, you can increase gravity, but I don't think this is really a good solution in a platformer because then bodies with no damping would work way differently. It'd be great if in future version of Box2D it'd be possible to specify linear damping seperately for x and y axis, by using a vector or such.
I didn't abandon SetLinearVelocity; That's what I'm using to damp the speed.
Quote:
reko wrote:
4) Using an impulse instead of a force and having a small friction on the player solved this one. It's important to have small friction so your player moves smoothly on a ground and i.e. after jump won't "freeze" for a while before being apple to move again. I use a friction value of 0.1f for my player at the moment.
Thanks for sharing the friction of 0.1, I tried and it works well. BTW do you use a density of 1.0 or other?
I don't use density of 1.0 for the player since Box2D is tuned for meters/kilograms (and I didn't want to change the default units, as they work pretty well).
Quote:
BTW how did you check if the character is in the air? Is there a built in function for this?
Check the contact list of the player's body to see if it's touching anything. The real thing is a bit more complicated than this though, because you might be sliding against a wall in air/touching another player/whatever. I haven't really spend much effort in this yet, but I doubt it'll be too difficult to define when the player is in air (check if the contact points the player has are in the bottom part of the player i.e. the feet).
reko wrote:
I'm using a rectangle. My character also got stuck first, but small friction and big enough impulse for moving really made a difference for me. Also remember to be careful when designing your world to prevent making such shapes where the box can get stuck too easily.
Did you try to make your character fall off of big heights? This seems to be the problem that once the rectangle hits the ground it doesn't always rest perfectly on the surface, but slightly below it. Making it stuck, regardless of how hard the impulse is.
Has anyone encountered a fix for this?
reko wrote:
I didn't abandon SetLinearVelocity; That's what I'm using to damp the speed.
Ok, I have been trying once more. I don't think you have to use setLinearVelocity to dampen speed at all. It's only a matter of finding the right friction. I increased the friction from 0.1 to 0.5 to get a good slow down. I don't find that friction is the problem with the character being stuck in other shapes.
Quote:
I haven't really spend much effort in this yet, but I doubt it'll be too difficult to define when the player is in air (check if the contact points the player has are in the bottom part of the player i.e. the feet).
Yes, I guess if the shape is a rectangle it's dangerous to use any of the corners. Better to check somewhere closer to the bottom middle.
Ygryggil wrote:
Did you try to make your character fall off of big heights? This seems to be the problem that once the rectangle hits the ground it doesn't always rest perfectly on the surface, but slightly below it. Making it stuck, regardless of how hard the impulse is.
Has anyone encountered a fix for this?
Has anyone encountered a fix for this?
Hello,
I encountered the same problem (the "stuck" problem) when playing around with the Actionscript port of Box2d. I'm trying to make a keyboard-controlled "ball" that rolls around. I've also found that using "impulse" is the best way to move the player based on keyboard input, but in order to make the ball "roll" accurately I have to set the friction on everything up to 1 (so it rolls rather than slides). If I roll the ball into an object with enough velocity (or if it falls fast enough onto a surface) it gets "stuck." If I set the friction back to zero, the ball will "bounce" out of being stuck - no problem. Except in this case the ball just slides instead of rolls (which is what I'd expect).
So far I've been very impressed with box2d. I'd hate to give it up because of this. Any ideas? Thanks,
- Mayo Butter
I can see how objects with large friction get stuck. Instead of using a rectangle for a character, I would use two circles, one on top of another, and possibly a rectangle in the middle so that it looks like a capsule.
Would anyone like to give me a working example of the getting stuck problem? Some test-bed code would be even better.
Would anyone like to give me a working example of the getting stuck problem? Some test-bed code would be even better.
Yes I will make some more tests with your suggestions and send you some test-bed code, but its also the Flash version of Box2D.
Erin Catto wrote:
Would anyone like to give me a working example of the getting stuck problem? Some test-bed code would be even better.
Attached is a zip file of a Flash project that demonstrates this (source code & viewable .swf)
Launch the "dev.swf" file. The arrow keys (left & right) apply impulses to the circle. If you get it going fast enough right, it'll get stuck within the acute angle formed by the two rectangles.
Also notice how when the circle first hits the ground, it kind of "buries" itself for a moment. I imagine if the controllable object was a rectangle instead of a circle, it wouldn't be able to "roll" itself back above ground.
It could just be that this is the proper effect that high friction causes. If that's the case... any suggestions on how to create a similar effect (user controlled rolling ball)?
Thanks!
Hi mayobutter,
Since you're using pixel units, you'll want to change b2_lengthUnitsPerMeter in b2Settings.as
Changing this to 40 fixed the problem (This will make the ball as if it was 1 meter in diameter rather than 40 meters).
Also it doesn't really help that you're running the game at 15Hz.
All the best,
-Matt
Since you're using pixel units, you'll want to change b2_lengthUnitsPerMeter in b2Settings.as
Changing this to 40 fixed the problem (This will make the ball as if it was 1 meter in diameter rather than 40 meters).
Also it doesn't really help that you're running the game at 15Hz.
All the best,
-Matt
Thanks for the example. I was able to reproduce this behavior in the test-bed using this code:
Like Matt said, you need to change some settings. Overall the quality of your simulation was poor with jittering and large overlap.
Regardless, I could still get the circle stuck in my example. The reason is due to the acute angle plus the large friction (most games use friction of 0.2-0.3). The circle gets wedged and the static friction force gets very large.
So you need to use lower friction and/or less acute angles. Another possibility would be to disable warm-starting on character bodies. This will help prevent the build up of large friction forces.
Yet another possibility would be to detect that the character is stuck and temporarily reduce the friction (to zero).
FYI, acute angles have always been trouble for game characters, even with zero friction.
Code:
VaryingRestitution()
{
const float32 k_friction = 0.5f;
{
b2BoxDef sd;
sd.extents.Set(50.0f, 10.0f);
sd.friction = k_friction;
b2BodyDef bd;
bd.position.Set(0.0f, -10.0f);
bd.AddShape(&sd);
m_world->CreateBody(&bd);
sd.extents.Set(1.0f, 10.0f);
bd.position.Set(5.0f, 5.0f);
bd.rotation = 0.35f * b2_pi;
m_world->CreateBody(&bd);
}
{
b2CircleDef sd;
sd.radius = 1.0f;
sd.density = 1.0f;
sd.friction = k_friction;
b2BodyDef bd;
bd.AddShape(&sd);
bd.position.Set(-10.0f, 20.0f);
m_body = m_world->CreateBody(&bd);
}
}
void Keyboard(unsigned char key)
{
switch (key)
{
case ',':
m_body->WakeUp();
m_body->ApplyImpulse(b2Vec2(-10.0f, 0.0f), m_body->GetCenterPosition());
break;
case '.':
m_body->WakeUp();
m_body->ApplyImpulse(b2Vec2(10.0f, 0.0f), m_body->GetCenterPosition());
break;
default:
Test::Keyboard(key);
break;
}
}
{
const float32 k_friction = 0.5f;
{
b2BoxDef sd;
sd.extents.Set(50.0f, 10.0f);
sd.friction = k_friction;
b2BodyDef bd;
bd.position.Set(0.0f, -10.0f);
bd.AddShape(&sd);
m_world->CreateBody(&bd);
sd.extents.Set(1.0f, 10.0f);
bd.position.Set(5.0f, 5.0f);
bd.rotation = 0.35f * b2_pi;
m_world->CreateBody(&bd);
}
{
b2CircleDef sd;
sd.radius = 1.0f;
sd.density = 1.0f;
sd.friction = k_friction;
b2BodyDef bd;
bd.AddShape(&sd);
bd.position.Set(-10.0f, 20.0f);
m_body = m_world->CreateBody(&bd);
}
}
void Keyboard(unsigned char key)
{
switch (key)
{
case ',':
m_body->WakeUp();
m_body->ApplyImpulse(b2Vec2(-10.0f, 0.0f), m_body->GetCenterPosition());
break;
case '.':
m_body->WakeUp();
m_body->ApplyImpulse(b2Vec2(10.0f, 0.0f), m_body->GetCenterPosition());
break;
default:
Test::Keyboard(key);
break;
}
}
Like Matt said, you need to change some settings. Overall the quality of your simulation was poor with jittering and large overlap.
Regardless, I could still get the circle stuck in my example. The reason is due to the acute angle plus the large friction (most games use friction of 0.2-0.3). The circle gets wedged and the static friction force gets very large.
So you need to use lower friction and/or less acute angles. Another possibility would be to disable warm-starting on character bodies. This will help prevent the build up of large friction forces.
Yet another possibility would be to detect that the character is stuck and temporarily reduce the friction (to zero).
FYI, acute angles have always been trouble for game characters, even with zero friction.
Original thread: https://box2d.org/forum/viewtopic.php?t=160