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.

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.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 on other sites

Try setting a torque on left/right keys.

Share on other sites

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 on other sites

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)

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 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 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 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 on other sites

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

• 33
• 12
• 10
• 9
• 9
• Forum Statistics

• Total Topics
631352
• Total Posts
2999484
×