Character Rotation While Moving

Started by
7 comments, last by oenda 9 years, 11 months ago

Hi.I use SFML with Box2D.I can't rotate the character sprite like GTA 2.My codes:

Update:



    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
        body->ApplyLinearImpulse(b2Vec2(0,-10 * UNRATIO),body->GetWorldCenter(),true);
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
        body->ApplyLinearImpulse(b2Vec2(0,10* UNRATIO),body->GetWorldCenter(),true);
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
        body->ApplyLinearImpulse(b2Vec2(-5* UNRATIO,0),body->GetWorldCenter(),true);

    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
        body->ApplyLinearImpulse(b2Vec2(5* UNRATIO,0),body->GetWorldCenter(),true);



Render:


    b2Transform trans = body->GetTransform();
    animatedSprite.setOrigin(width / 2, height / 2);
    angle = trans.q.GetAngle();
    animatedSprite.setRotation(RADTODEG(angle) );

    animatedSprite.setPosition(body->GetWorldCenter().x * RATIO,body->GetWorldCenter().y * RATIO);

    sf::Time frameTime = frameClock.restart();
    animatedSprite.play(*currentAnimation);
         // update AnimatedSprite
    animatedSprite.update(frameTime);

    window.draw(animatedSprite); 

I drew the problem.Can you help me?Thanks.

Advertisement

Try setting a torque on left/right keys.

thanks for the answer.I tried but it didn't work.


if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
body->ApplyTorque(5* UNRATIO,true);
}

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
body->ApplyTorque(-5* UNRATIO,true);
}

Have you tried applying a larger value to your torque? The mass of the object may be stopping it from moving (so maybe 5 isn't quite enough to get any noticeable movement)

Thanks.I increased the value.But it spins at its orbit.And If I don't do something like this ,it doesn't move left/right


    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){

       body->ApplyLinearImpulse(b2Vec2(-speed * UNRATIO,0),body->GetWorldCenter(),true);
       body->ApplyTorque(100* UNRATIO,true);

    }

If I don't use body->ApplyLinearImpulse ,it doesn't move left or right

Is there any solution?

I think you might need to better explain what it is you're trying to accomplish?

If you want something to always move towards where it's facing, like a car, you can't just apply linear impulses, you need to be giving it either velocity or acceleration.

EDIT: (or you need to change your linear impulses to be in the space of the object, so forward/up would be the objects forward/up vector, instead of the hard coded (0,1)

Thank you ,ferrous.I want something like a car.The character must move where it faces.But i don't know how to do this.

You want to take into account the angle your object is currently facing when doing a linear impulse, so you need to do something like this for the Up/Down keys.


body->ApplyLinearImpulse(Math.sin(body->GetAngle()) * UNRATIO, Math.cos(body->GetAngle()) * UNRATIO)

thanks again.It didn't help.Probably,I will change the game to 3d from 2d.I will use Bullet Physics.

This topic is closed to new replies.

Advertisement