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