rev |
line source |
nuclear@6
|
1 #include <algorithm>
|
nuclear@6
|
2 #include "sim.h"
|
nuclear@6
|
3
|
nuclear@6
|
4 SimWorld::SimWorld()
|
nuclear@6
|
5 : gravity(0, -9, 0)
|
nuclear@6
|
6 {
|
nuclear@6
|
7 damping = 0.005;
|
nuclear@6
|
8 }
|
nuclear@6
|
9
|
nuclear@6
|
10 void SimWorld::add_object(GObject *obj)
|
nuclear@6
|
11 {
|
nuclear@7
|
12 CoRigid *co = COCAST(CoRigid, obj->get_component("rigid"));
|
nuclear@6
|
13 if(co) {
|
nuclear@6
|
14 add_rigid_body(co);
|
nuclear@6
|
15 } else {
|
nuclear@6
|
16 fprintf(stderr, "SimWorld::add_object: trying to add object without rigid body component\n");
|
nuclear@6
|
17 }
|
nuclear@6
|
18 }
|
nuclear@6
|
19
|
nuclear@6
|
20 void SimWorld::add_rigid_body(CoRigid *co)
|
nuclear@6
|
21 {
|
nuclear@6
|
22 if(std::find(rigid.begin(), rigid.end(), co) == rigid.end()) {
|
nuclear@6
|
23 rigid.push_back(co);
|
nuclear@6
|
24 co->world = this;
|
nuclear@6
|
25 }
|
nuclear@6
|
26 }
|
nuclear@6
|
27
|
nuclear@6
|
28 void SimWorld::remove_object(GObject *obj)
|
nuclear@6
|
29 {
|
nuclear@7
|
30 CoRigid *co = COCAST(CoRigid, obj->get_component("rigid"));
|
nuclear@6
|
31 if(co) {
|
nuclear@6
|
32 remove_rigid_body(co);
|
nuclear@6
|
33 } else {
|
nuclear@6
|
34 fprintf(stderr, "SimWorld::remove_object: failed to remove object without rigid body component\n");
|
nuclear@6
|
35 }
|
nuclear@6
|
36 }
|
nuclear@6
|
37
|
nuclear@6
|
38 void SimWorld::remove_rigid_body(CoRigid *co)
|
nuclear@6
|
39 {
|
nuclear@6
|
40 std::list<CoRigid*>::iterator it = std::find(rigid.begin(), rigid.end(), co);
|
nuclear@6
|
41 if(it != rigid.end()) {
|
nuclear@6
|
42 rigid.erase(it);
|
nuclear@6
|
43 } else {
|
nuclear@6
|
44 fprintf(stderr, "SimWorld::remove_rigid_body: failed to remove missing rigid body\n");
|
nuclear@6
|
45 }
|
nuclear@6
|
46 }
|
nuclear@6
|
47
|
nuclear@6
|
48 void SimWorld::set_damping(float damping)
|
nuclear@6
|
49 {
|
nuclear@6
|
50 this->damping = damping;
|
nuclear@6
|
51 }
|
nuclear@6
|
52
|
nuclear@6
|
53 float SimWorld::get_damping() const
|
nuclear@6
|
54 {
|
nuclear@6
|
55 return damping;
|
nuclear@6
|
56 }
|
nuclear@6
|
57
|
nuclear@6
|
58 void SimWorld::set_gravity(const Vector3 &v)
|
nuclear@6
|
59 {
|
nuclear@6
|
60 gravity = v;
|
nuclear@6
|
61 }
|
nuclear@6
|
62
|
nuclear@6
|
63 const Vector3 &SimWorld::get_gravity() const
|
nuclear@6
|
64 {
|
nuclear@6
|
65 return gravity;
|
nuclear@6
|
66 }
|
nuclear@8
|
67
|
nuclear@8
|
68 bool SimWorld::collide(const GObject *obj, HitPoint *hit) const
|
nuclear@8
|
69 {
|
nuclear@8
|
70 CoCollider *co = COCAST(CoCollider, obj->get_component("collider"));
|
nuclear@8
|
71 if(co) {
|
nuclear@8
|
72 return collide(co, hit);
|
nuclear@8
|
73 }
|
nuclear@8
|
74 return false; // no collisions if it doesn't have a collider component
|
nuclear@8
|
75 }
|
nuclear@8
|
76
|
nuclear@8
|
77 bool SimWorld::collide(const CoCollider *col, HitPoint *hit) const
|
nuclear@8
|
78 {
|
nuclear@8
|
79 if(!col->shape) {
|
nuclear@8
|
80 return false;
|
nuclear@8
|
81 }
|
nuclear@8
|
82
|
nuclear@8
|
83 // TODO do something better
|
nuclear@8
|
84 std::list<CoRigid*>::const_iterator it = rigid.begin();
|
nuclear@8
|
85 while(it != rigid.end()) {
|
nuclear@8
|
86 const CoRigid *r = *it++;
|
nuclear@8
|
87 const GObject *obj2 = r->get_object();
|
nuclear@8
|
88 if(!obj2 || obj2 == col->get_object()) {
|
nuclear@8
|
89 continue;
|
nuclear@8
|
90 }
|
nuclear@8
|
91
|
nuclear@8
|
92 const CoCollider *col2 = COCAST(CoCollider, obj2->get_component("collider"));
|
nuclear@8
|
93 if(!col2 || !col2->shape) {
|
nuclear@8
|
94 continue;
|
nuclear@8
|
95 }
|
nuclear@8
|
96
|
nuclear@8
|
97 if(col->shape->collide(col2->shape, hit)) {
|
nuclear@8
|
98 return true;
|
nuclear@8
|
99 }
|
nuclear@8
|
100 }
|
nuclear@8
|
101 return false;
|
nuclear@8
|
102 }
|