Box2D Forums

It is currently Fri May 24, 2013 1:56 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sat Jul 28, 2012 12:20 pm 
Offline

Joined: Sat Jul 28, 2012 12:12 pm
Posts: 8
I can't think of any sensible reason this will happen.

I am making a platformer, and want to get a more "oldschool" movement feeling - constant velocity, not slipping on angled surfaces, and simple jumps.

I tried a whole lot of ways to achieve that, none of them working decently (closest one being a wheel underneath the player, with the controls directly setting angular velocity).
Then I stumbled upon this Box2DAS3 code that achieves what I want.

I did an exact port to my C++ code, and yet the friction isn't working correctly.

Even though the friction is set correctly to 0, 0.2 and 100, when in air, walking or standing for some time respectively, the
player never acts as if its friction is set to the specified values.
He doesn't stop immediately, and he slips on angle surfaces (no friction = 100).
Only if I comment both friction = 0 and 0.2, does it actually set it to 100.

Again, the function calls are being called correctly, and I can't see any reason for this behavior.

This is the relevant code, with std::couts to check if the calls are called:
Code:
if ((!keyboard[sf::Keyboard::A] && !keyboard[sf::Keyboard::D]) || (keyboard[sf::Keyboard::A] && keyboard[sf::Keyboard::D]))
{
   stillTime += deltaTime;
      
    m_body->SetLinearVelocity(b2Vec2(v.x * 0.9f, v.y));
}
else
{
   stillTime = 0.0f;
}

if(!m_grounded)
{
    std::cout << "0 " << rand() << "\n";
   m_physicsFixture->SetFriction(0.0f);
   m_sensorFixture->SetFriction(0.0f);
}
else
{
   if (((!keyboard[sf::Keyboard::A] && !keyboard[sf::Keyboard::D]) || (keyboard[sf::Keyboard::A] && keyboard[sf::Keyboard::D])) && stillTime > 0.2f)
    {
        std::cout << "100 " << rand() << "\n";
      m_physicsFixture->SetFriction(100.0f);
      m_sensorFixture->SetFriction(100.0f);
   }
   else
    {
        std::cout << "0.2 " << rand() << "\n";
        m_physicsFixture->SetFriction(0.2f);
      m_sensorFixture->SetFriction(0.2f);
   }
}

Thanks for any help.


Top
 Profile  
 
PostPosted: Sat Jul 28, 2012 4:32 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
When two fixtures get close to each other a b2Contact is created to manage any potential interaction between them. The info from the fixtures (eg. friction) is copied into the b2Contact, and the original values in the fixtures themselves are no longer referenced for that contact. To make any changes of the kind you are wanting, you'll have to change the friction values in the b2Contact rather than the fixtures. You can do this in any of the contact listener functions, as they all pass the b2Contact to you. b2Contact has SetFriction and a few other functions which will come in handy for this kind of customization: http://code.google.com/p/box2d/source/b ... 2Contact.h
You might also find this useful: http://www.iforce2d.net/b2dtut/collision-anatomy
I can't explain why the Flash version works as it does... perhaps the port has done things a little differently about this.


Top
 Profile  
 
PostPosted: Sun Jul 29, 2012 9:27 am 
Offline

Joined: Sat Jul 28, 2012 12:12 pm
Posts: 8
Thanks! You just saved me hours of headaches.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group