]> Dogcows Code - chaz/yoink/blob - src/Character.cc
840099c8fcd97cd1cffb17d7a9010a635d0c383f
[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 "Character.hh"
15 #include "Log.hh"
16
17
18 class SpringForce
19 {
20 public:
21
22 explicit SpringForce(Mf::Vector2 x) :
23 location(x) {}
24
25 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
26 {
27 Mf::Vector2 x = state.position - location;
28 Mf::Scalar mag = x.length();
29 Mf::Scalar d = 0.0;
30
31 // spring:
32 //mState.force += -15.0 * x - 1.5 * mState.velocity;
33 force = SCALAR(-10.0) * (mag - d) * (x / mag);// - SCALAR(2.0) * state.velocity;
34
35 return force;
36 }
37
38 private:
39
40 Mf::Vector2 force;
41 Mf::Vector2 location;
42 };
43
44 class ResistanceForce
45 {
46 public:
47
48 explicit ResistanceForce(Mf::Scalar scale = 1.0) :
49 k(scale) {}
50
51 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
52 {
53 force = -k * state.velocity;
54 return force;
55 }
56
57 private:
58
59 Mf::Vector2 force;
60 Mf::Scalar k;
61 };
62
63
64 Character::Character(const std::string& name) :
65 tilemap(name),
66 animation(name)
67 {
68 mState.init();
69
70 mState.mass = 1.0;
71 mState.inverseMass = 1.0 / mState.mass;
72
73 // forces
74 mState.force = Mf::Vector2(0.0, 0.0);
75 //mState.forces.push_back(SpringForce(Mf::Vector2(5.0, 4.0)));
76 //mState.forces.push_back(ResistanceForce(2.0));
77 //mState.forces.push_back(Mf::LinearState<2>::GravityForce(-9.8));
78
79 // starting position
80 mState.position = Mf::Vector2(5.0, 5.0);
81 mState.momentum = Mf::Vector2(0.0, 0.0);
82 mState.recalculate();
83
84 mPrevState = mState;
85 }
86
87
88 void Character::update(Mf::Scalar t, Mf::Scalar dt)
89 {
90 Mf::RigidBody2::update(t, dt); // update physics
91
92 animation.update(t, dt);
93
94 Mf::Vector3 center(mState.position[0], mState.position[1], 0.0);
95 Mf::Vector3 a(mState.position[0] - 0.5, mState.position[1] - 0.5, 0.0);
96 Mf::Vector3 b(mState.position[0] + 0.5, mState.position[1] + 0.5, 0.0);
97
98 mAabb.init(a, b);
99 mSphere.init(center, a);
100 }
101
102
103 void Character::draw(Mf::Scalar alpha) const
104 {
105 Mf::State2 state = getState(alpha);
106 Mf::Vector2 position = state.position;
107
108 //glColor3f(1.0f, 1.0f, 1.0f);
109 tilemap.bind();
110
111 Mf::Texture::TileIndex frame = animation.getFrame();
112 Mf::Texture::Orientation orientation = Mf::Texture::NORMAL;
113
114 if (mState.velocity[0] < 0.0) orientation = Mf::Texture::REVERSE;
115
116 Mf::Scalar coords[8];
117 tilemap.getTileCoords(frame, coords, orientation);
118
119 Mf::Scalar s = 0.5;
120
121 glBegin(GL_TRIANGLE_FAN);
122 glTexCoord(coords[0], coords[1]);
123 glVertex(position[0]-s, position[1]-s);
124 glTexCoord(coords[2], coords[3]);
125 glVertex(position[0]+s, position[1]-s);
126 glTexCoord(coords[4], coords[5]);
127 glVertex(position[0]+s, position[1]+s);
128 glTexCoord(coords[6], coords[7]);
129 glVertex(position[0]-s, position[1]+s);
130 glEnd();
131 }
132
133
134 /*int Character::getOctant(const Mf::Aabb<3>& aabb) const
135 {
136 int octantNum = -1;
137
138 Mf::Plane::Halfspace halfspace;
139
140 Mf::Plane xy = aabb.getPlaneXY();
141 halfspace = xy.intersects(mSphere);
142 if (halfspace == Mf::Plane::INTERSECT)
143 {
144 halfspace = xy.intersects(mAabb);
145 }
146
147 if (halfspace == Mf::Plane::POSITIVE)
148 {
149 Mf::Plane xz = aabb.getPlaneXZ();
150 halfspace = xz.intersects(mSphere);
151 if (halfspace == Mf::Plane::INTERSECT)
152 {
153 halfspace = xz.intersects(mAabb);
154 }
155
156 if (halfspace == Mf::Plane::POSITIVE)
157 {
158 Mf::Plane yz = aabb.getPlaneYZ();
159 halfspace = yz.intersects(mSphere);
160 if (halfspace == Mf::Plane::INTERSECT)
161 {
162 halfspace = yz.intersects(mAabb);
163 }
164
165 if (halfspace == Mf::Plane::POSITIVE)
166 {
167 octantNum = 2;
168 }
169 else if (halfspace == Mf::Plane::NEGATIVE)
170 {
171 octantNum = 3;
172 }
173 }
174 else if (halfspace == Mf::Plane::NEGATIVE)
175 {
176 Mf::Plane yz = aabb.getPlaneYZ();
177 halfspace = yz.intersects(mSphere);
178 if (halfspace == Mf::Plane::INTERSECT)
179 {
180 halfspace = yz.intersects(mAabb);
181 }
182
183 if (halfspace == Mf::Plane::POSITIVE)
184 {
185 octantNum = 1;
186 }
187 else if (halfspace == Mf::Plane::NEGATIVE)
188 {
189 octantNum = 0;
190 }
191 }
192 }
193 else if (halfspace == Mf::Plane::NEGATIVE)
194 {
195 Mf::Plane xz = aabb.getPlaneXZ();
196 halfspace = xz.intersects(mSphere);
197 if (halfspace == Mf::Plane::INTERSECT)
198 {
199 halfspace = xz.intersects(mAabb);
200 }
201
202 if (halfspace == Mf::Plane::POSITIVE)
203 {
204 Mf::Plane yz = aabb.getPlaneYZ();
205 halfspace = yz.intersects(mSphere);
206 if (halfspace == Mf::Plane::INTERSECT)
207 {
208 halfspace = yz.intersects(mAabb);
209 }
210
211 if (halfspace == Mf::Plane::POSITIVE)
212 {
213 octantNum = 6;
214 }
215 else if (halfspace == Mf::Plane::NEGATIVE)
216 {
217 octantNum = 7;
218 }
219 }
220 else if (halfspace == Mf::Plane::NEGATIVE)
221 {
222 Mf::Plane yz = aabb.getPlaneYZ();
223 halfspace = yz.intersects(mSphere);
224 if (halfspace == Mf::Plane::INTERSECT)
225 {
226 halfspace = yz.intersects(mAabb);
227 }
228
229 if (halfspace == Mf::Plane::POSITIVE)
230 {
231 octantNum = 5;
232 }
233 else if (halfspace == Mf::Plane::NEGATIVE)
234 {
235 octantNum = 4;
236 }
237 }
238 }
239
240 return octantNum;
241 }
242 */
243
244
245 void Character::addImpulse(Mf::Vector2 impulse)
246 {
247 mState.momentum += impulse;
248 }
249
250 void Character::addForce(Mf::Vector2 force)
251 {
252 mState.force += force;
253 }
254
255 void Character::setPosition(Mf::Vector2 position)
256 {
257 mState.position = position;
258 }
259
This page took 0.038918 seconds and 3 git commands to generate.