Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can't get sprite to move in angle  (Read 1902 times)

0 Members and 1 Guest are viewing this topic.

HarishB

  • Newbie
  • *
  • Posts: 1
    • View Profile
Can't get sprite to move in angle
« on: January 02, 2016, 12:02:55 pm »
Hi, I've been building a car game and currently I've been stuck for a few days trying to get the car to steer using the bicycle model, the code currently has a few errors and is close to working.
    // Update delta time
        MoveableObject::m_deltaTime = m_clock.restart();
       
        // Calculate the new velocity
        const float kfTimeStep = this->getDeltaTime().asSeconds();
        const sf::Vector2f kfAcceleration = this->getAcceleration();
        const sf::Vector2f kfVelocity = this->getVelocity();

        // Calculate the displacement
        const sf::Vector2f kfDisplacement = kfVelocity + kfAcceleration * kfTimeStep;

        // Calculate the new position of the car
        float fVehicleRotationRad = ang::DEGTORAD(this->getRotation());
        const float kfSteeringAngleRad = ang::DEGTORAD(m_fSteeringAngle) + fVehicleRotationRad;
        const sf::Vector2f kfVehiclePosition = this->getPosition();

        // Calculate the positions of the theoretical front and rear wheels
        const float kfHalfWheelBase = m_fWheelBase / 2.f;
        const sf::Vector2f kfVehicleOrientation = sf::Vector2f(cos(fVehicleRotationRad), sin(fVehicleRotationRad));
        sf::Vector2f fFrontWheel = sf::Vector2f(kfVehiclePosition.x + kfHalfWheelBase * kfVehicleOrientation.x, kfVehiclePosition.y + m_fWheelBase / 2 * kfVehicleOrientation.y);
        sf::Vector2f fRearWheel = sf::Vector2f(kfVehiclePosition.x - kfHalfWheelBase * kfVehicleOrientation.x, kfVehiclePosition.y - m_fWheelBase / 2 * kfVehicleOrientation.y);
        // Move the wheels
        const sf::Vector2f kfSteeringOrientation = sf::Vector2f(cos(kfSteeringAngleRad + fVehicleRotationRad), sin(kfSteeringAngleRad + fVehicleRotationRad));
        fFrontWheel += sf::Vector2f(kfVelocity.x * kfTimeStep * kfSteeringOrientation.x, kfVelocity.y * kfTimeStep * kfSteeringOrientation.y);
        fRearWheel += sf::Vector2f(kfVelocity.x * kfTimeStep * kfVehicleOrientation.x, kfVelocity.y * kfTimeStep * kfVehicleOrientation.y);
        // Update the position and angles
        this->setPosition((fFrontWheel + fRearWheel) / 2.f);
        this->setRotation(ang::RADTODEG(atan2(fFrontWheel.y - fRearWheel.y, fFrontWheel.x - fRearWheel.x)));
        m_fSteeringAngle += ang::RADTODEG(fVehicleRotationRad) - this->getRotation();
    float fNewRotation = this->getRotation();
        m_fSteeringAngle += ang::RADTODEG(fVehicleRotationRad) - fNewRotation;
        fVehicleRotationRad = ang::DEGTORAD(fNewRotation);

        if (fVehicleRotationRad != 0)
                this->move(sf::Vector2f(cos(fVehicleRotationRad) * kfDisplacement.x, -sin(fVehicleRotationRad) * kfDisplacement.y));
        else
                this->move(kfVelocity * kfTimeStep);

        // Update the velocity
        this->setVelocity(kfDisplacement);
 

Errors:

1. When turning left and accelerating, the car turns right and backwards.
2. When turning right and accelerating, the car turns left and forwards.
3. The car accelerates forwards and not in the direction of rotation.

Hopefully somebody will be able to identify the simple mistakes that have eluded me.

Any and all help is appreciated, thanks!  ;D
« Last Edit: January 05, 2016, 04:26:02 pm by HarishB »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Can't get sprite to move in angle
« Reply #1 on: January 06, 2016, 12:31:17 am »
Its not very clear what you want. Generally around here we are happy to help with specific problems, but when you just post a snippet of your code and ask us to debug it chances are good you won't get any replies.

Honestly, since this is a logical error in your code it is something you should debug yourself. And if you don't know how to use a debugger then this would be a perfect time for you to learn.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything