我正在处理中的一个简单的重力程序。我的程序根据重力公式吸收粒子并将它们吸引到彼此。不幸的是,一旦我尝试将力乘以,便会PVector.mult()
在标题中得到错误:
无法在基本类型float上调用mult(float)。
这是我的方法代码。G在其他地方定义。
public float distance(Particle other) {
return location.sub(other.location).mag();
}
public PVector direction(Particle other) {
return location.sub(other.location).normalize();
}
public void gravity(Particle other) {
float grav = (G*((mass * other.mass)/pow(distance(other), 2)));
if(distance(other) != 0) {
acceleration.add(distance(other).mult(grav));
}
为什么我不能在需要浮动的地方通过浮动?
让我们将这条线分开并将其分为多个步骤:
acceleration.add(distance(other).mult(grav));
这是我尝试将其拆分为多行的尝试:
float grav = 42;
float distanceFromOther = distance(other);
float multipliedValue = distanceFromOther.mult(grav);
acceleration.add(multipliedValue);
希望这使事情变得更加明显:您正在尝试调用mult()
一个float
值,该值将不起作用。您需要调用包含函数的mult
一个PVector
或另一个类mult()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句