Physics in video game, Apply torque on angular acceleration

I'm working on a game project about a top down car game in 2d. I want to manage all the physics by myself. Im working with this book : http://www.amazon.fr/Game-Physics-Engine-Development-Commercial-Grade/dp/0123819768 to implement the physics.

From now my physic engine can handle force on the different axes. But I have some issues to implement a correct simulation of rotation. Im trying to implement some torque to find angular acceleration. So I implemented an inertia tensor matrix :

setMass(400.f);
Matrix3 it;
it.setBlockInertiaTensor(Vector3(2, 1, 1), 400);
setInertiaTensor(it);

void setBlockInertiaTensor(const Vector3 &halfSizes, float mass)
{
    Vector3 squares = halfSizes.componentProduct(halfSizes);
    setInertiaTensorCoeffs(0.3f*mass*(squares.y + squares.z),
        0.3f*mass*(squares.x + squares.z),
        0.3f*mass*(squares.x + squares.y));
}

To apply torque I apply a force at a body point of my car and I find the torque by a cross product :

player->addForceAtBodyPoint(Vector3(-2000, 1000, 0), Vector3(0, 100, 0));

void AObject::addForceAtBodyPoint(const Vector3 &force, const Vector3 &point)
{
    Vector3 pt = getPointInWorldSpace(point);
    addForceAtPoint(force, pt);
}

void AObject::addForceAtPoint(const Vector3 &force,
    const Vector3 &point)
{
    // Convert to coordinates relative to center of mass.
    Vector3 pt = point;
    pt -= _position;

    _forceAccumulate += force;
    _torqueAccumulate += pt % force;
    //std::cout << "torque x " << pt.x << " y " << pt.y  <<  " z "<< pt.z <<  std::endl;
}

Vector3 Vector3::operator%(const Vector3 &vector) const
{
    return Vector3(y*vector.z - z*vector.y,
        z*vector.x - x*vector.z,
        x*vector.y - y*vector.x);
}

(The modulo % is the cross product )

And finally I do my integration of all the data :

void    Player::integrate(float deltaTime)
{

    addForce(_velocity * -150.0f);

    // Calculate linear acceleration from force inputs.
    _lastFrameAcceleration = _acceleration;
    _lastFrameAcceleration.addScaledVector(_forceAccumulate, _inverseMass);
    // Calculate angular acceleration from torque inputs.
    Vector3 angularAcceleration = _inverseInertiaTensorWorld.transform(_torqueAccumulate);
    // Update linear velocity from acceleration .
    _velocity.addScaledVector(_lastFrameAcceleration, deltaTime);
    // Update angular velocity from  acceleration .
    _rotation.addScaledVector(angularAcceleration, deltaTime);
    // Impose drag.
    _velocity *= pow(_linearDamping, deltaTime);
    _rotation *= pow(_angularDamping, deltaTime);
    // Update linear position.
    _position.addScaledVector(_velocity, deltaTime);
    _position.z = 0;
    // Update angular position
    _orientation.addScaledVector(_rotation, deltaTime);
    // Normalise the orientation, and update the matrice
    calculateWorldLocalData();
    // Clear accumulators.
    clearAccumulator();
}

And the orientation is not working at all. Im not that good with physics stuff, so I think that im misunderstanding the physic implementation of torque with inertia tensor...


If your game is top down in 2D then you can only have rotation in the Z direction. IE in and out of the screen. Thus you can simplify your problem and avoid 3D tensors. In which case, in your car class I would have a private variable called rotation. eg

private:
    double angle;
    double tourque;
public:
    void updateTorque(*some way of passing forces*)
    {
        double total_t = 0;
        for each force
        {
            double t = use cosine and length to point to generate a tourque
            total_t = t + total_t
        }
    }
    void update_angle // place your integration routine here and call once per loop
链接地址: http://www.djcxy.com/p/95478.html

上一篇: 一段时间后,子弹物理停止碰撞

下一篇: 视频游戏中的物理,对角加速度应用扭矩