]> Dogcows Code - chaz/yoink/blob - src/Character.cc
remove some unused stlplus modules
[chaz/yoink] / src / Character.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #include <iostream>
11
12 #include <moof/log.hh>
13
14 #include "Character.hh"
15
16
17 class SpringForce
18 {
19 public:
20
21 explicit SpringForce(moof::vector2 x) :
22 location(x) {}
23
24 const moof::vector2& operator () (const moof::linear_state<2>& state)
25 {
26 moof::vector2 x = state.position - location;
27 moof::scalar mag = x.length();
28 moof::scalar d = 0.0;
29
30 // spring:
31 //state_.force += -15.0 * x - 1.5 * state_.velocity;
32 force = SCALAR(-10.0) * (mag - d) * (x / mag);// - SCALAR(2.0) * state.velocity;
33
34 return force;
35 }
36
37 private:
38
39 moof::vector2 force;
40 moof::vector2 location;
41 };
42
43
44 class ResistanceForce
45 {
46 public:
47
48 explicit ResistanceForce(moof::scalar scale = 1.0) :
49 k(scale) {}
50
51 const moof::vector2& operator () (const moof::linear_state<2>& state)
52 {
53 force = -k * state.velocity;
54 return force;
55 }
56
57 private:
58
59 moof::vector2 force;
60 moof::scalar k;
61 };
62
63
64 Character::Character(const std::string& name) :
65 tilemap(name),
66 animation(name)
67 {
68 state_.init();
69
70 state_.mass = 1.0;
71 state_.inverse_mass = 1.0 / state_.mass;
72
73 // forces
74 state_.force = moof::vector2(0.0, 0.0);
75 //state_.forces.push_back(SpringForce(moof::vector2(5.0, 4.0)));
76 //state_.forces.push_back(ResistanceForce(2.0));
77 //state_.forces.push_back(moof::linear_state<2>::gravity_force(-9.8));
78
79 // starting position
80 state_.position = moof::vector2(5.0, 5.0);
81 state_.momentum = moof::vector2(3.0, 0.0);
82 state_.recalculate();
83
84 prev_state_ = state_;
85 }
86
87 void Character::update(moof::scalar t, moof::scalar dt)
88 {
89 moof::rigid_body2::update(t, dt); // update physics
90
91 animation.update(t, dt);
92
93 moof::vector3 center(state_.position[0], state_.position[1], 0.0);
94 moof::vector3 a(state_.position[0] - 0.5, state_.position[1] - 0.5, 0.0);
95 moof::vector3 b(state_.position[0] + 0.5, state_.position[1] + 0.5, 0.0);
96
97 aabb_.init(a, b);
98 sphere_.init(center, a);
99
100 int frame = animation.getFrame();
101 tilemap.tile(frame);
102 }
103
104 void Character::draw(moof::scalar alpha) const
105 {
106 moof::state2 state = moof::rigid_body2::state(alpha);
107 const moof::vector2& position = state.position;
108
109 const moof::scalar s = 0.5;
110
111 moof::vector3 coords[4];
112
113 coords[0] = moof::vector3(position[0] - s, position[1] - s, SCALAR(0.0));
114 coords[1] = moof::vector3(position[0] + s, position[1] - s, SCALAR(0.0));
115 coords[2] = moof::vector3(position[0] + s, position[1] + s, SCALAR(0.0));
116 coords[3] = moof::vector3(position[0] - s, position[1] + s, SCALAR(0.0));
117
118 tilemap.draw(coords);
119 }
120
121 /*int Character::getOctant(const moof::Aabb<3>& aabb) const
122 {
123 int octantNum = -1;
124
125 moof::plane::halfspace halfspace;
126
127 moof::plane xy = aabb.xy_plane();
128 halfspace = xy.intersects(sphere_);
129 if (halfspace == moof::plane::intersecting)
130 {
131 halfspace = xy.intersects(aabb_);
132 }
133
134 if (halfspace == moof::plane::positive)
135 {
136 moof::plane xz = aabb.xz_plane();
137 halfspace = xz.intersects(sphere_);
138 if (halfspace == moof::plane::intersecting)
139 {
140 halfspace = xz.intersects(aabb_);
141 }
142
143 if (halfspace == moof::plane::positive)
144 {
145 moof::plane yz = aabb.yz_plane();
146 halfspace = yz.intersects(sphere_);
147 if (halfspace == moof::plane::intersecting)
148 {
149 halfspace = yz.intersects(aabb_);
150 }
151
152 if (halfspace == moof::plane::positive)
153 {
154 octantNum = 2;
155 }
156 else if (halfspace == moof::plane::negative)
157 {
158 octantNum = 3;
159 }
160 }
161 else if (halfspace == moof::plane::negative)
162 {
163 moof::plane yz = aabb.yz_plane();
164 halfspace = yz.intersects(sphere_);
165 if (halfspace == moof::plane::intersecting)
166 {
167 halfspace = yz.intersects(aabb_);
168 }
169
170 if (halfspace == moof::plane::positive)
171 {
172 octantNum = 1;
173 }
174 else if (halfspace == moof::plane::negative)
175 {
176 octantNum = 0;
177 }
178 }
179 }
180 else if (halfspace == moof::plane::negative)
181 {
182 moof::plane xz = aabb.xz_plane();
183 halfspace = xz.intersects(sphere_);
184 if (halfspace == moof::plane::intersecting)
185 {
186 halfspace = xz.intersects(aabb_);
187 }
188
189 if (halfspace == moof::plane::positive)
190 {
191 moof::plane yz = aabb.yz_plane();
192 halfspace = yz.intersects(sphere_);
193 if (halfspace == moof::plane::intersecting)
194 {
195 halfspace = yz.intersects(aabb_);
196 }
197
198 if (halfspace == moof::plane::positive)
199 {
200 octantNum = 6;
201 }
202 else if (halfspace == moof::plane::negative)
203 {
204 octantNum = 7;
205 }
206 }
207 else if (halfspace == moof::plane::negative)
208 {
209 moof::plane yz = aabb.yz_plane();
210 halfspace = yz.intersects(sphere_);
211 if (halfspace == moof::plane::intersecting)
212 {
213 halfspace = yz.intersects(aabb_);
214 }
215
216 if (halfspace == moof::plane::positive)
217 {
218 octantNum = 5;
219 }
220 else if (halfspace == moof::plane::negative)
221 {
222 octantNum = 4;
223 }
224 }
225 }
226
227 return octantNum;
228 }
229 */
230
231 void Character::addImpulse(moof::vector2 impulse)
232 {
233 state_.momentum += impulse;
234 }
235
236 void Character::addForce(moof::vector2 force)
237 {
238 state_.force += force;
239 }
240
241 void Character::setPosition(moof::vector2 position)
242 {
243 state_.position = position;
244 }
245
This page took 0.040445 seconds and 4 git commands to generate.