gpt4 book ai didi

c++ - 将两种方法结合在一起

转载 作者:行者123 更新时间:2023-11-30 19:44:35 24 4
gpt4 key购买 nike

以下两个函数计算 obj 类型对象的体积力。第一个函数调用第二个函数两次,有没有办法将这些方法组合在一起?

void calculateBodyForcesForEach(obj *pBody, obj *pHead, sys *sys) {
float fTotalForce[4] = {0.0f};

calculateBodyForces(pBody, sys->m_pSun, fTotalForce, sys);


for (obj *pOtherBody = pHead; pOtherBody;) {
if (pBody != pOtherBody) {
calculateBodyForces(pBody, pOtherBody, fTotalForce, sys);
}
pOtherBody = pOtherBody->m_pNext;
}

vecCopy(fTotalForce, pBody->m_fForce);
}

void calculateBodyForces(obj *pBody, obj *pOtherBody,
float *fTotalForce, solarSystem *sys)
{
pBody->m_fForce[0] = 0.0f;
pBody->m_fForce[1] = 0.0f;
pBody->m_fForce[2] = 0.0f;

float fCalculatedForce[3] = {0.0f};

float fCalculatedMass = pBody->m_fMass * pOtherBody->m_fMass;

float fDistanceBetweenPos[3] = {0.0f};
vecSub(pOtherBody->m_fPosition, pBody->m_fPosition, fDistanceBetweenPos);

float fDistanceSquared = (fDistanceBetweenPos[0] * fDistanceBetweenPos[0]) + (fDistanceBetweenPos[1] * fDistanceBetweenPos[1]) + (fDistanceBetweenPos[2] * fDistanceBetweenPos[2]);

float fUnitVector[3] = {0.0f};
vecSub(pOtherBody->m_fPosition, pBody->m_fPosition, fUnitVector);
vecNormalise(fUnitVector, fUnitVector);

float fGravitationalForce = sys->m_fGravitationalConstant * (fCalculatedMass / fDistanceSquared);

vecScalarProduct(fUnitVector, fGravitationalForce, fCalculatedForce);

vecAdd(fTotalForce, fCalculatedForce, fTotalForce);
}

//////到目前为止我的解决方案但仍然不起作用

void calculateVelocityAndPosition(planet *pElement, solarSystem *pSystem) { //completly modified  

float fStartPosition[4] = {0.0f,0.0f,0.0f,0.0f};
float fAccelerationTime[3] = {0.0f,0.0f,0.0f};
float fVelocityInterval[3] = {0.0f,0.0f,0.0f};
float fTotalCalculated[3] = {0.0f,0.0f,0.0f};
vecCopy(pElement->m_fPosition, fStartPosition);
vecScalarProduct(pElement->m_fAcceleration, pSystem->m_fTimeStep * pSystem->m_fTimeStep, fAccelerationTime);
vecScalarProduct(fAccelerationTime, 0.5f, fAccelerationTime);
vecScalarProduct(pElement->m_fVelocity, pSystem->m_fTimeStep, fVelocityInterval);
vecAdd(fVelocityInterval, fAccelerationTime, fTotalCalculated);
vecAdd(fStartPosition, fTotalCalculated, pElement->m_fPosition);
fori(3){
pElement->m_fVelocity[i] = (pElement->m_fPosition[i] - fStartPosition[i]) / pSystem->m_fTimeStep;
}
fori(3)
if (pElement->m_fVelocity[i] > pSystem->m_fMaxVelocity)
pElement->m_fVelocity[i] = pSystem->m_fMaxVelocity;
else if (pElement->m_fVelocity[i] < pSystem->m_fMaxNegativeVelocity)
pElement->m_fVelocity[i] = pSystem->m_fMaxNegativeVelocity;
vecScalarProduct(pElement->m_fVelocity, pSystem->m_fDragCoefficient, pElement->m_fVelocity);
}

最佳答案

fori(3)

这是什么意思?

这个金额:

float fDistanceSquared = (fDistanceBetweenPos[0] * fDistanceBetweenPos[0]) + (fDistanceBetweenPos[1] * fDistanceBetweenPos[1]) + (fDistanceBetweenPos[2] * fDistanceBetweenPos[2]);

最好用它制作一个循环:

float fDistanceSquared = 0.0f
for (int i = 0; i<3; i++)
fDistanceSquared += fDistanceBetweenPos[i] * fDistanceBetweenPos[i];

如何确定标量积?为什么里面有三个变量呢?此外,了解正在解决的问题可能会有所帮助。

关于c++ - 将两种方法结合在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27914191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com