void updateCenterOfMass(Body body) {
		if (_mass == 0) {
			_mass = body.getMass();
			_centerOfMass = body.getPos();
		} else {
			double new_mass = _mass + body.getMass();
			double new_cen_x = ((_mass*_centerOfMass.getX()) + (body.getMass() * body.getXPos()))/new_mass;
			double new_cen_y = ((_mass*_centerOfMass.getY()) + (body.getMass() * body.getYPos()))/new_mass;
			_centerOfMass = Vec2D(new_cen_x, new_cen_y);
			_mass = new_mass;
		}
	}
예제 #2
0
sf::Vector2f Universe::force(Body p1, Body p2){
  	sf::Vector2f planet_force;
  	double totalForce, dx, dy, radius, radius_squared;
  	double grav = (-6.67e-11);
  
  	dx = (((p1.getPosition()).x) - ((p2.getPosition()).x));
  	dy = (((p1.getPosition()).y) - ((p2.getPosition()).y));

  	radius_squared = pow(dx, 2) + pow(dy, 2);
  	radius = sqrt(radius_squared);

  	totalForce = ((grav) * (p1.getMass()) * (p2.getMass())) / (radius_squared);

  	planet_force.x = totalForce * (dx/radius);
  	planet_force.y = totalForce * (dy/radius);

  	return planet_force;
}
	void print() {
		cout << "Entering Print" << endl;
		if (_state == INTERNAL) {
			cout << "Internal" << endl;
					if (_body == NULL) {
			cout << "NO Body here" << endl;
			cout << "Mass of internal node " << _mass << endl;
			cout << "Center of Mass " << _centerOfMass.getX() << " " << _centerOfMass.getY() << endl;
		} else {
			cout << _body->getMass() << endl;
		}
		}
		else {
			cout << "External" << endl;
					if (_body == NULL) {
			cout << "NO Body here" << endl;
		} else {
			cout << _body->getMass() << endl;
		}
		}

		if (_NW != NULL) {
			cout << "Printing NW" << endl;
			_NW->print();
		}
		if (_NE != NULL) {
			cout << "Printing NE" << endl;
			_NE->print();
		}
		if (_SW != NULL) {
			cout << "Printing SW" << endl;
			_SW->print();
		}
		if (_SE != NULL) {
			cout << "Printing SE" << endl;
			_SE->print();
		}
		cout << "Leaving Print" << endl;
	}
	Force calculateForceFromBody(Body otherBody) {
		Vec2D distance = calculateDistanceFromBody(otherBody);
		Vec2D unit_distance = distance.getUnitVector();
		if (distance.getMagnitude() != 0) {
			double forceMagnitude= (GRAVITYCONST * otherBody.getMass() * getMass()) / (pow(distance.getMagnitude(), 2));

			double forceMagnitude_x = forceMagnitude * unit_distance.getX();
			double forceMagnitude_y = forceMagnitude * unit_distance.getY();
			return Force(forceMagnitude_x, forceMagnitude_y);
		} else {
			return Force(0,0);
		}
	}
	void updateCenterOfMass(double &mass, Vec2D &centerOfMass) {
		if (_state == INTERNAL) {
			double child_mass = 0;
			Vec2D child_com;
			if (_NW != NULL)
				_NW->updateCenterOfMass(child_mass, child_com);
			if (_SW != NULL)
				_SW->updateCenterOfMass(child_mass, child_com);
			if (_NE != NULL)
				_NE->updateCenterOfMass(child_mass, child_com);
			if (_SE != NULL)
				_SE->updateCenterOfMass(child_mass, child_com);
			_mass = child_mass;
			_centerOfMass = child_com/_mass;
			centerOfMass = centerOfMass + child_com*child_mass;
			mass += child_mass;
		} else {
			if (_body != NULL) {
				mass += _body->getMass();
				centerOfMass = centerOfMass+  _body->getPos()*_body->getMass();
			}
		}
	}
예제 #6
0
	int w_Body_getMass(lua_State * L)
	{
		Body * t = luax_checkbody(L, 1);
		lua_pushnumber(L, t->getMass());
		return 1;
	}
	Body(const Body &otherBody) {
		_mass = otherBody.getMass();
		_vel = otherBody.getVel();
		_pos = otherBody.getPos();
		_forces = otherBody._forces;
	}
	bool insertBody(Body body) {
	// cout << "In insert" << endl;

		if (outOfRange(body)) {
			return false;
		}

		if ((_mass == 0 && isExternal())) {
//			cout << "Keeping body here" << endl;
			_mass += body.getMass();
			_centerOfMass = _centerOfMass + body.getPos();
			//_body = new Body(body);
			return true;
		}
	//			updateCenterOfMass(body);

		Body temp(_mass, 0, 0, _centerOfMass.getX(), _centerOfMass.getY());

		if (isExternal()) {
	//		cout << "is external" << endl;
			_mass = 0;
			_centerOfMass = Vec2D(0,0);

		int quad = getQuadrantOfBody(temp);


		if (quad == 1) {
			if (_NW == NULL)
				_NW = new TreeNode(_box.getQuadrant(1));
			_NW->insertBody(temp);
		}
		
		if (quad == 2) {
			if (_NE == NULL)
				_NE = new TreeNode(_box.getQuadrant(2));
			_NE->insertBody(temp);
		}

		if (quad == 3) {
			if (_SW == NULL)
				_SW = new TreeNode(_box.getQuadrant(3));
			_SW->insertBody(temp);
		}

		if (quad == 4) {
		if (_SE == NULL)
				_SE = new TreeNode(_box.getQuadrant(4));
			_SE->insertBody(temp);
		}


	//		cout << "is external 2 " << endl;
		}

		int quad = getQuadrantOfBody(body);

		if (quad == 1) {
			if (_NW == NULL)
				_NW = new TreeNode(_box.getQuadrant(1));
			_NW->insertBody(body);
		}
		
		if (quad == 2) {
			if (_NE == NULL)
				_NE = new TreeNode(_box.getQuadrant(2));
			_NE->insertBody(body);
		}

		if (quad == 3) {
			if (_SW == NULL)
				_SW = new TreeNode(_box.getQuadrant(3));
			_SW->insertBody(body);
		}

		if (quad == 4) {
		if (_SE == NULL)
				_SE = new TreeNode(_box.getQuadrant(4));
			_SE->insertBody(body);
		}



		if (_NW != NULL) {
			_mass += _NW->_mass;
			_centerOfMass = _centerOfMass + _NW->_mass * _NW->_centerOfMass;
		}

		if (_NE != NULL) {
			_mass += _NE->_mass;
			_centerOfMass = _centerOfMass + _NE->_mass * _NE->_centerOfMass;
		}

		if (_SW != NULL) {
			_mass += _SW->_mass;
			_centerOfMass = _centerOfMass + _SW->_mass * _SW->_centerOfMass;
		}

		if (_SE != NULL) {
			_mass += _SE->_mass;
			_centerOfMass = _centerOfMass + _SE->_mass * _SE->_centerOfMass;
		}
		if (_mass != 0) {
		_centerOfMass =  _centerOfMass / _mass;
	}
//		updateCenterOfMass(_mass, _centerOfMass);
	//	cout << "Leaving insert" << endl;
		return false;

	}