Fixed some transforms, move to 3d and quaternion math in progress...
[ne.git] / src / mods / sandbox / planet.cpp
blobef9ad5627054439abb0c4fafc69ee0f1fefb0159
1 /************************************************************************
2 This file is part of NE.
4 NE is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 NE is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
18 #include "planet.h"
20 using namespace SandBox;
22 Planet::Planet(Texture *texture, float radius, float density) : Sphere(radius,density), m_tex(texture)
27 Planet::~Planet()
31 void Planet::draw()
33 m_tex->apply();
34 Sphere::draw();
37 const Vec3f Planet::getGravity(Object *target)
39 static float v[4];
41 Vec3f pos1 = getPositionV();
43 Vec3f pos2 = Vec3f::FromODE(target->getPosition());
45 Vec3f del = pos1 - pos2;
47 float r2 = del.dot(del);
49 float n = 1.0 / sqrt(r2);
51 Vec3f dir = del * n;
53 dReal m1 = getMass();
54 dReal m2 = target->getMass();
56 dReal g = m1 * m2 / r2;
58 return Vec3f(dir * g);
61 void Planet::applyGravity(Object *target)
63 if (!target->getBody()->isEnabled()) return;
65 Vec3f grav = getGravity(target);
67 target->applyGravity(grav);