Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can't stop character sliding down slope — Gideros Forum

Can't stop character sliding down slope

Tom2012Tom2012 Guru
edited December 2012 in General questions
Hi all,

I'm using Box2D for my platform game. Everything works well except for when the character is on a slope. I can't seem to stop the body from sliding down the slope when the character is stationary on a slope.

Has anyone had any luck with this? I've been mining google with no luck yet.

Dislikes: MauMau

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • Maybe you can apply a force like, character Mass * -gravity
  • twisttaptwisttap Member
    edited December 2012
    Or better idea, reset the gravityScale to zero when on a slope.
    From guiders ref I -> gravityScale: (number) Scale the gravity applied to this body.
    b2.Body:setGravityScale(scale)
    might work for you :)
  • Thanks Twisttap

    The problem with the gravity scale is that if you then touch say left and the character begins to run down hill, he sort of hovers in the air until enough gravity has built up to push him back down to the slope.

    Unless I've got something wrong.

    The next thing I'll try is applying my own gravity each frame, which can then be turned off by setting the y factor in the setLinearVelocity
  • How about friction of character or slope, can you increase it?
  • twisttaptwisttap Member
    edited December 2012
    Cant you check if the char is moving right or left then apply a if statement :/
    Found something like that, not lua but its very easy to adopt.
    -(void) onSlope
    {
        b2Vec2 current = body->GetLinearVelocity();
     
        if (moving && direction == kDirectionLeft) {
            body->SetGravityScale(1.0f);
            body->SetLinearVelocity(b2Vec2(-HORIZONTAL_SPEED, current.y));
     
        } else if (moving && direction == kDirectionRight) {
            body->SetGravityScale(1.0f);
            body->SetLinearVelocity(b2Vec2(HORIZONTAL_SPEED, current.y));
     
        } else {
            body->SetGravityScale(0.0f);
            body->SetLinearVelocity(b2Vec2(0, current.y));
        }
    }
  • Tom2012Tom2012 Guru
    edited December 2012
    Thanks guys, I've found something that works. Whatever I tried gravity is being applied to the body and is forcing it down the slope.

    The solution is a bit ugly but it seems to work...
    	if(self.scene.hero.onSlope and #self.scene.leftButtonTouches==0 and #self.scene.rightButtonTouches==0) then
     
    		-- make this adjustment to stop hero sliding down slopes
    		self.yVel = -.5
     
    	end
    ... then set the y component in the setLinearVelocity
  • hgvyas123hgvyas123 Guru
    Accepted Answer
    @Tom2012

    can you try setLinearDamping with very high value when you want to freeze the character and reset it when you want to move

    :)

    Likes: Tom2012

    +1 -1 (+1 / -0 )Share on Facebook
  • Ah thanks hgvyas123

    That solved the problem.

    It's so funny when you find the solution and it's so straight forward. I must have spent hours fiddling around with code.

    Much appreciated. :-)
Sign In or Register to comment.