2014-10-03 75 views
3

我正在制作关于2D游戏中的自上而下游戏的游戏项目。我想自己管理所有的物理。我正在使用本书:http://www.amazon.fr/Game-Physics-Engine-Development-Commercial-Grade/dp/来实现物理。物理在视频游戏中,在角加速度上应用扭矩

从现在起,我的物理引擎可以处理不同轴上的力。但是我有一些问题需要实现正确的旋转模拟。我试图实现一些扭矩来找到角加速度。所以,我实现了一个惯性张量矩阵:

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

施加扭矩我在我的车的车身点施加一个力,我觉得由一个跨产品的扭矩:

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

(模数%是叉积)

最后,我做我的整合所有的数据:

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

方向根本不起作用。我不擅长物理的东西,所以我认为我误解了惯性张量扭矩的物理实现...

+0

阅读http://gafferongames.com/game-physics/physics-in-3d/和http://gafferongames.com/game-physics/integration-basics/也许这将有所帮助。 – rashmatash 2014-10-03 11:11:51

回答

1

如果你的游戏在2D中自上而下,那么你只能在Z方向旋转。 I.E.进出屏幕。因此,您可以简化问题并避免3D张量。在这种情况下,在您的汽车类中,我将拥有一个名为rotation的私有变量。例如

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