1
General / 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.
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!
// 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);
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!