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