计算和应用摩擦力
我一直在研究如何应用摩擦力,有一个部分我很困惑,那就是如何将摩擦力应用到速度上(如果事实上我正确计算了摩擦力的话)。
当我的表面上有一个球,法线为 (0, 1, 0),当前速度为 (2, 0, 0) 时,我将计算摩擦力为 (0, -0.3, 0)。然而,我不明白的是如何正确地将其应用于我的速度。如果我简单地减去它,那么我的速度将是 (2, -0.3, 0),但是,摩擦力不应该导致 x 分量变得更小吗?
这是我当前的代码,如果有人可以看一下,我将不胜感激。我知道有一些优化需要进行。
mTotalForces = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vSurfaceNormalized;
D3DXVec3Normalize(&vSurfaceNormalized, &vSurfaceNormal);
D3DXVECTOR3 vFrictionForce(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 forceAndVelocity = mTotalForces + m_vVelocity;
float fVelocityMagnitude = sqrt((forceAndVelocity.x * forceAndVelocity.x) + (forceAndVelocity.y * forceAndVelocity.y) + forceAndVelocity.z * (forceAndVelocity.z));
float fFrictionForceMagnitude = 0.0f;
float fFrictionForce = 0.0f;
if(fVelocityMagnitude == 0.0f)
{
fFrictionForce = m_fStaticFrictionCoefficient;
D3DXVECTOR3 vStaticFriction = -m_fStaticFrictionCoefficient * vSurfaceNormalized;
vFrictionForce = vStaticFriction;
}
else if(fVelocityMagnitude > 0.0f)
{
fFrictionForce = m_fKineticFrictionCoefficient;
D3DXVECTOR3 vKineticFriction = -m_fKineticFrictionCoefficient * vSurfaceNormalized;
vFrictionForce = vKineticFriction;
}
{
float fFrictionForceMagnitude = abs(fFrictionForce * D3DXVec3Dot(&vSurfaceNormalized, &forceAndVelocity));
if(fFrictionForceMagnitude > fVelocityMagnitude)
{
fFrictionForceMagnitude = 0.0f;
m_vVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}
else
{
m_vVelocity -= vFrictionForce;
}
}
我在此摩擦函数开始时将总力设为零,因为如果存在摩擦,则先前的力(重力)不应影响以后的速度。 (... 正确的?)
I have been researching how to apply friction and there is a part I am stuck on, which is how to apply that friction to the velocityy (if in fact I am calculating the friction force correctly that is).
When I have a ball on a surface, with a normal of (0, 1, 0), with a present velocity of (2, 0, 0) then I will calculate my friction force as (0, -0.3, 0). However, what I don't understand is how to properly apply that to my velocity. If I simply, subtract it then my velocity will be (2, -0.3, 0), however, shouldn't the friction be cause the x component to be more less?
Here is my current code, if anyone could please take a look at I would greatly appreciate it. There are some optimizations to be made, I am aware of that.
mTotalForces = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vSurfaceNormalized;
D3DXVec3Normalize(&vSurfaceNormalized, &vSurfaceNormal);
D3DXVECTOR3 vFrictionForce(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 forceAndVelocity = mTotalForces + m_vVelocity;
float fVelocityMagnitude = sqrt((forceAndVelocity.x * forceAndVelocity.x) + (forceAndVelocity.y * forceAndVelocity.y) + forceAndVelocity.z * (forceAndVelocity.z));
float fFrictionForceMagnitude = 0.0f;
float fFrictionForce = 0.0f;
if(fVelocityMagnitude == 0.0f)
{
fFrictionForce = m_fStaticFrictionCoefficient;
D3DXVECTOR3 vStaticFriction = -m_fStaticFrictionCoefficient * vSurfaceNormalized;
vFrictionForce = vStaticFriction;
}
else if(fVelocityMagnitude > 0.0f)
{
fFrictionForce = m_fKineticFrictionCoefficient;
D3DXVECTOR3 vKineticFriction = -m_fKineticFrictionCoefficient * vSurfaceNormalized;
vFrictionForce = vKineticFriction;
}
{
float fFrictionForceMagnitude = abs(fFrictionForce * D3DXVec3Dot(&vSurfaceNormalized, &forceAndVelocity));
if(fFrictionForceMagnitude > fVelocityMagnitude)
{
fFrictionForceMagnitude = 0.0f;
m_vVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}
else
{
m_vVelocity -= vFrictionForce;
}
}
I make the total force zero at the start of this friction function because if there is friction, then the previous force (gravity) should not influence the velocity later. (... right?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码有很多问题。因此,除了回答您的实际问题之外,我还将逐步介绍它们。首先,
这是不正确的,因为力和速度在概念上非常不同。这体现在它们的单位不同,所以它们不能相加。从下一行开始,
我相信您希望
forceAndVelocity
严格成为速度。我将重做第一个
if
语句,如下所示,我放弃了向量计算,因为它们与法线平行而不是垂直。
摩擦力的大小是摩擦系数乘以法向力的大小,因此
在第二个
if
语句中,您再次比较力和速度,您想要做的是确定何时可以克服静摩擦力。本质上,您需要确定所有其他力是否超过摩擦力,因此您需要将没有摩擦的总力的大小与摩擦力的大小进行比较。因此,我会重做if
块,因为最后一行是您问题的答案。摩擦力与速度相反,因此它与速度方向相反,其中
m_vVelocity / fVelocityMagnitude
是标准化速度。请注意,这不会直接影响速度,而是速度,其中
mass
是物体的质量;Your code has a number of problems. So, I'm going to step through them, in addition to answering your actual question. First,
is incorrect as force and velocity are conceptually very different. This is embodied in their different units, so they can not be added. From the next line,
I believe you want
forceAndVelocity
to be strictly velocity.I'd redo the first
if
statement as follows,I dropped the vector calculations as they were parallel to the normal not perpendicular.
The magnitude of the friction is the coefficient of friction times the magnitude of the normal force, so
In the second
if
statement, you're again comparing force and velocity, and what you're trying to do is determine when static friction may be overcome. Essentially, you need to determine if all other forces exceed the friction, so you need to compare the magnitude of the total forces without friction to the magnitude of the friction force. So, I'd redo theif
block aswhere the last line is the answer to your question. The friction force opposes velocity, so it is opposite in direction to your velocity, where
m_vVelocity / fVelocityMagnitude
is the normalized velocity. Note, this doesn't directly affect the velocity, instead your velocity iswhere
mass
is the mass of the object;首先,你的摩擦力应该与你的表面相切,沿着速度矢量的方向。所以在你的情况下,你的力向量将是(-0.3,0,0)。那么你的摩擦力就是一种力,所以你不能像这样将它添加到速度上。你的力所做的功会影响速度。这项工作会随着时间的推移而变化,并且实际上可能与您的速度成正比。
First of all, your friction should be tangential to your surface, alongside the direction of the velocity vector. So in your case, your force vector would be (-0.3,0,0). Then your friction is a force so you can't add it to a velocity just like this. Your force performs a work that affects the velocity. That work changes over time and could be actually proportional to your velocity.
动摩擦始终平行于接触表面施加,即垂直于法线,而不是沿着法线施加。
Dynamic friction is always applied parallel to the surface of contact, that is, perpendicular to the normal, not along the normal.