coeng

diff src/sim.cc @ 6:2f872a179914

first component test: - prs, xform, physics components with dependencies - topological sort of components to update them in the correct order - debug visualization component todo: remove draw() from components, doesn't make sense
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Feb 2015 07:27:12 +0200
parents
children af24cfbdf9b6
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sim.cc	Sat Feb 14 07:27:12 2015 +0200
     1.3 @@ -0,0 +1,66 @@
     1.4 +#include <algorithm>
     1.5 +#include "sim.h"
     1.6 +
     1.7 +SimWorld::SimWorld()
     1.8 +	: gravity(0, -9, 0)
     1.9 +{
    1.10 +	damping = 0.005;
    1.11 +}
    1.12 +
    1.13 +void SimWorld::add_object(GObject *obj)
    1.14 +{
    1.15 +	CoRigid *co = COCAST(CoRigid, obj->get_component("phys"));
    1.16 +	if(co) {
    1.17 +		add_rigid_body(co);
    1.18 +	} else {
    1.19 +		fprintf(stderr, "SimWorld::add_object: trying to add object without rigid body component\n");
    1.20 +	}
    1.21 +}
    1.22 +
    1.23 +void SimWorld::add_rigid_body(CoRigid *co)
    1.24 +{
    1.25 +	if(std::find(rigid.begin(), rigid.end(), co) == rigid.end()) {
    1.26 +		rigid.push_back(co);
    1.27 +		co->world = this;
    1.28 +	}
    1.29 +}
    1.30 +
    1.31 +void SimWorld::remove_object(GObject *obj)
    1.32 +{
    1.33 +	CoRigid *co = COCAST(CoRigid, obj->get_component("phys"));
    1.34 +	if(co) {
    1.35 +		remove_rigid_body(co);
    1.36 +	} else {
    1.37 +		fprintf(stderr, "SimWorld::remove_object: failed to remove object without rigid body component\n");
    1.38 +	}
    1.39 +}
    1.40 +
    1.41 +void SimWorld::remove_rigid_body(CoRigid *co)
    1.42 +{
    1.43 +	std::list<CoRigid*>::iterator it = std::find(rigid.begin(), rigid.end(), co);
    1.44 +	if(it != rigid.end()) {
    1.45 +		rigid.erase(it);
    1.46 +	} else {
    1.47 +		fprintf(stderr, "SimWorld::remove_rigid_body: failed to remove missing rigid body\n");
    1.48 +	}
    1.49 +}
    1.50 +
    1.51 +void SimWorld::set_damping(float damping)
    1.52 +{
    1.53 +	this->damping = damping;
    1.54 +}
    1.55 +
    1.56 +float SimWorld::get_damping() const
    1.57 +{
    1.58 +	return damping;
    1.59 +}
    1.60 +
    1.61 +void SimWorld::set_gravity(const Vector3 &v)
    1.62 +{
    1.63 +	gravity = v;
    1.64 +}
    1.65 +
    1.66 +const Vector3 &SimWorld::get_gravity() const
    1.67 +{
    1.68 +	return gravity;
    1.69 +}