Jump to content
  • Advertisement
Sign in to follow this  
oenda

Character Rotation While Moving

This topic is 1494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by oenda

Share this post


Link to post
Share on other sites
Advertisement

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);
}

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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)

Edited by ferrous

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!