annotate src/sim.cc @ 7:af24cfbdf9b6
adding collision detection
author |
John Tsiombikas <nuclear@member.fsf.org> |
date |
Sat, 14 Feb 2015 10:08:00 +0200 |
parents |
2f872a179914 |
children |
8cce82794f90 |
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 }
|