]> Dogcows Code - chaz/yoink/blob - src/Character.cc
testing new non-autotools build system
[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
103
104 void Character::draw(moof::scalar alpha) const
105 {
106 moof::state2 state = moof::rigid_body2::state(alpha);
107 moof::vector2 position = state.position;
108
109 //glColor3f(1.0f, 1.0f, 1.0f);
110 tilemap.bind();
111
112 int frame = animation.getFrame();
113 moof::texture::orientation orientation = moof::texture::normal;
114
115 if (state_.velocity[0] < 0.0) orientation = moof::texture::reverse;
116
117 moof::scalar coords[8];
118 tilemap.tile_coordinates(frame, coords, orientation);
119
120 moof::scalar s = 0.5;
121
122 glBegin(GL_TRIANGLE_FAN);
123 glTexCoord(coords[0], coords[1]);
124 glVertex(position[0]-s, position[1]-s);
125 glTexCoord(coords[2], coords[3]);
126 glVertex(position[0]+s, position[1]-s);
127 glTexCoord(coords[4], coords[5]);
128 glVertex(position[0]+s, position[1]+s);
129 glTexCoord(coords[6], coords[7]);
130 glVertex(position[0]-s, position[1]+s);
131 glEnd();
132 }
133
134
135 /*int Character::getOctant(const moof::Aabb<3>& aabb) const
136 {
137 int octantNum = -1;
138
139 moof::plane::halfspace halfspace;
140
141 moof::plane xy = aabb.xy_plane();
142 halfspace = xy.intersects(sphere_);
143 if (halfspace == moof::plane::intersecting)
144 {
145 halfspace = xy.intersects(aabb_);
146 }
147
148 if (halfspace == moof::plane::positive)
149 {
150 moof::plane xz = aabb.xz_plane();
151 halfspace = xz.intersects(sphere_);
152 if (halfspace == moof::plane::intersecting)
153 {
154 halfspace = xz.intersects(aabb_);
155 }
156
157 if (halfspace == moof::plane::positive)
158 {
159 moof::plane yz = aabb.yz_plane();
160 halfspace = yz.intersects(sphere_);
161 if (halfspace == moof::plane::intersecting)
162 {
163 halfspace = yz.intersects(aabb_);
164 }
165
166 if (halfspace == moof::plane::positive)
167 {
168 octantNum = 2;
169 }
170 else if (halfspace == moof::plane::negative)
171 {
172 octantNum = 3;
173 }
174 }
175 else if (halfspace == moof::plane::negative)
176 {
177 moof::plane yz = aabb.yz_plane();
178 halfspace = yz.intersects(sphere_);
179 if (halfspace == moof::plane::intersecting)
180 {
181 halfspace = yz.intersects(aabb_);
182 }
183
184 if (halfspace == moof::plane::positive)
185 {
186 octantNum = 1;
187 }
188 else if (halfspace == moof::plane::negative)
189 {
190 octantNum = 0;
191 }
192 }
193 }
194 else if (halfspace == moof::plane::negative)
195 {
196 moof::plane xz = aabb.xz_plane();
197 halfspace = xz.intersects(sphere_);
198 if (halfspace == moof::plane::intersecting)
199 {
200 halfspace = xz.intersects(aabb_);
201 }
202
203 if (halfspace == moof::plane::positive)
204 {
205 moof::plane yz = aabb.yz_plane();
206 halfspace = yz.intersects(sphere_);
207 if (halfspace == moof::plane::intersecting)
208 {
209 halfspace = yz.intersects(aabb_);
210 }
211
212 if (halfspace == moof::plane::positive)
213 {
214 octantNum = 6;
215 }
216 else if (halfspace == moof::plane::negative)
217 {
218 octantNum = 7;
219 }
220 }
221 else if (halfspace == moof::plane::negative)
222 {
223 moof::plane yz = aabb.yz_plane();
224 halfspace = yz.intersects(sphere_);
225 if (halfspace == moof::plane::intersecting)
226 {
227 halfspace = yz.intersects(aabb_);
228 }
229
230 if (halfspace == moof::plane::positive)
231 {
232 octantNum = 5;
233 }
234 else if (halfspace == moof::plane::negative)
235 {
236 octantNum = 4;
237 }
238 }
239 }
240
241 return octantNum;
242 }
243 */
244
245
246 void Character::addImpulse(moof::vector2 impulse)
247 {
248 state_.momentum += impulse;
249 }
250
251 void Character::addForce(moof::vector2 force)
252 {
253 state_.force += force;
254 }
255
256 void Character::setPosition(moof::vector2 position)
257 {
258 state_.position = position;
259 }
260
This page took 0.044827 seconds and 5 git commands to generate.