#include "body.h" #include #include #include #include using namespace wgd; Body::Body(const wgd::InstanceID &inst) : wgd::Instance(inst) { // Set red to be default colour if (property("colour") == Null) { setColour(Colour(wgd::random(), wgd::random(), wgd::random())); } m_selected = property("selected"); vrot = property("vrot"); radius = property("radius"); density = property("density"); velocity.x = property("vx"); velocity.y = property("vy"); if (property("density") == Null) { if (property("mass") == Null) { setDensity(1); } else { // not technically correct, but bodies shouldn't be defining their masses here anyway setDensity((float) property("mass")); } } DOSTE_REGISTER(402, getID()(ix::x)); DOSTE_REGISTER(403, getID()(ix::y)); } void Body::impulse (const Vector3D& impulse, const Vector3D& point) { if (getMass() == (float)Infinity) { return; } setVelocity(getVelocity() + impulse / getMass()); Vector3D offset = point - position(); Vector3D rotChange = offset.cross(impulse) / getMomentOfInertia(); setRotationVel(getRotationVel() + rotChange.z); } void Body::update() { float x = getX(); float y = getY(); float vx = getVX(); float vy = getVY(); float t = Game::frameTime() * (float) WGD["speed"]; float a = WGD["g"]; if (getMass() == (float)Infinity) { a = 0; } x += vx * t; y += vy * t - a * t * t / 2; setX(x); setY(y); setVY(vy - a * t); float rot = getRotation(); float vrot = getRotationVel(); rot += vrot * t; setRotation(rot); } void Body::draw () {}