本文整理汇总了C++中MovableObject::getMass方法的典型用法代码示例。如果您正苦于以下问题:C++ MovableObject::getMass方法的具体用法?C++ MovableObject::getMass怎么用?C++ MovableObject::getMass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MovableObject
的用法示例。
在下文中一共展示了MovableObject::getMass方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: affect
/**
* Affects a movable object with a planet's gravity, in proportion to the object's
* mass.
*/
void GravityStrategy::affect(MovableObject& mwo, const Planet& swo) {
// Vector from planet to object.
Vector2d mwo_to_swo = swo.getPosition() - mwo.getPosition();
// Distance between centers of object and planet.
float distance = mwo_to_swo.getLength();
// Unit vector pointing at planet from MovableObject.
Vector2d planet_to_obj = mwo_to_swo/distance;
// Note to Jesper: Should it be this way?
// The gravity strategy seems to mess up when mass is 0...
if (mwo.getMass() == 0) {
return;
}
if(distance < 15.0)
return;
// std::cout << "Planet to obj: " << planet_to_obj.getX() << " " << planet_to_obj.getY() << std::endl;
float G = 10.0f;
// Value of force.
float force = (G * mwo.getMass() * swo.getMass()) / (distance*distance);
// Complete force with direction.
Vector2d forcedir = planet_to_obj * force;
// Amount of acceleration.
Vector2d acc = forcedir / mwo.getMass();
// Accelerate movableobject.
Vector2d mov(mwo.getMovement());
mov = mov + acc;
mwo.setMovement(mov);
}