nuclear@6: #include nuclear@6: #include "sim.h" nuclear@6: nuclear@6: SimWorld::SimWorld() nuclear@6: : gravity(0, -9, 0) nuclear@6: { nuclear@6: damping = 0.005; nuclear@6: } nuclear@6: nuclear@6: void SimWorld::add_object(GObject *obj) nuclear@6: { nuclear@7: CoRigid *co = COCAST(CoRigid, obj->get_component("rigid")); nuclear@6: if(co) { nuclear@6: add_rigid_body(co); nuclear@6: } else { nuclear@6: fprintf(stderr, "SimWorld::add_object: trying to add object without rigid body component\n"); nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: void SimWorld::add_rigid_body(CoRigid *co) nuclear@6: { nuclear@6: if(std::find(rigid.begin(), rigid.end(), co) == rigid.end()) { nuclear@6: rigid.push_back(co); nuclear@6: co->world = this; nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: void SimWorld::remove_object(GObject *obj) nuclear@6: { nuclear@7: CoRigid *co = COCAST(CoRigid, obj->get_component("rigid")); nuclear@6: if(co) { nuclear@6: remove_rigid_body(co); nuclear@6: } else { nuclear@6: fprintf(stderr, "SimWorld::remove_object: failed to remove object without rigid body component\n"); nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: void SimWorld::remove_rigid_body(CoRigid *co) nuclear@6: { nuclear@6: std::list::iterator it = std::find(rigid.begin(), rigid.end(), co); nuclear@6: if(it != rigid.end()) { nuclear@6: rigid.erase(it); nuclear@6: } else { nuclear@6: fprintf(stderr, "SimWorld::remove_rigid_body: failed to remove missing rigid body\n"); nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: void SimWorld::set_damping(float damping) nuclear@6: { nuclear@6: this->damping = damping; nuclear@6: } nuclear@6: nuclear@6: float SimWorld::get_damping() const nuclear@6: { nuclear@6: return damping; nuclear@6: } nuclear@6: nuclear@6: void SimWorld::set_gravity(const Vector3 &v) nuclear@6: { nuclear@6: gravity = v; nuclear@6: } nuclear@6: nuclear@6: const Vector3 &SimWorld::get_gravity() const nuclear@6: { nuclear@6: return gravity; nuclear@6: } nuclear@8: nuclear@8: bool SimWorld::collide(const GObject *obj, HitPoint *hit) const nuclear@8: { nuclear@8: CoCollider *co = COCAST(CoCollider, obj->get_component("collider")); nuclear@8: if(co) { nuclear@8: return collide(co, hit); nuclear@8: } nuclear@8: return false; // no collisions if it doesn't have a collider component nuclear@8: } nuclear@8: nuclear@8: bool SimWorld::collide(const CoCollider *col, HitPoint *hit) const nuclear@8: { nuclear@8: if(!col->shape) { nuclear@8: return false; nuclear@8: } nuclear@8: nuclear@8: // TODO do something better nuclear@8: std::list::const_iterator it = rigid.begin(); nuclear@8: while(it != rigid.end()) { nuclear@8: const CoRigid *r = *it++; nuclear@8: const GObject *obj2 = r->get_object(); nuclear@8: if(!obj2 || obj2 == col->get_object()) { nuclear@8: continue; nuclear@8: } nuclear@8: nuclear@8: const CoCollider *col2 = COCAST(CoCollider, obj2->get_component("collider")); nuclear@8: if(!col2 || !col2->shape) { nuclear@8: continue; nuclear@8: } nuclear@8: nuclear@8: if(col->shape->collide(col2->shape, hit)) { nuclear@8: return true; nuclear@8: } nuclear@8: } nuclear@8: return false; nuclear@8: }