]> Dogcows Code - chaz/yoink/blob - src/Character.cc
minor refactoring and state progress
[chaz/yoink] / src / Character.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <iostream>
30
31 #include "Character.hh"
32 #include "Log.hh"
33
34
35 struct SpringForce
36 {
37 explicit SpringForce(Mf::Vector2 x) :
38 location(x) {}
39
40 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
41 {
42 Mf::Vector2 x = state.position - location;
43 Mf::Scalar mag = x.length();
44 Mf::Scalar d = 50.0;
45
46 // spring:
47 //mState.force += -15.0 * x - 1.5 * mState.velocity;
48 force = SCALAR(-10.0) * (mag - d) * (x / mag) - 2.0 * state.velocity;
49
50 return force;
51 }
52
53 private:
54
55 Mf::Vector2 force;
56 Mf::Vector2 location;
57 };
58
59 struct ResistanceForce
60 {
61 explicit ResistanceForce(Mf::Scalar scale = 1.0) :
62 k(scale) {}
63
64 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
65 {
66 force = -k * state.velocity;
67 return force;
68 }
69
70 private:
71
72 Mf::Vector2 force;
73 Mf::Scalar k;
74 };
75
76
77 Character::Character(const std::string& name) :
78 tilemap(name),
79 animation(name)
80 {
81 mState.init();
82
83 mState.mass = 1.0;
84 mState.inverseMass = 1.0 / mState.mass;
85
86 // forces
87 mState.force = Mf::Vector2(0.0, 0.0);
88 //mState.forces.push_back(SpringForce(Mf::Vector2(500.0, 200.0)));
89 mState.forces.push_back(ResistanceForce(2.0));
90 //mState.forces.push_back(Mf::LinearState<2>::GravityForce(-100.0));
91
92 // starting position
93 mState.position = Mf::Vector2(64.0, 64.0);
94 mState.momentum = Mf::Vector2(0.0, 0.0);
95 mState.recalculate();
96
97 mPrevState = mState;
98 }
99
100
101 void Character::update(Mf::Scalar t, Mf::Scalar dt)
102 {
103 Mf::RigidBody2::update(t, dt); // update physics
104
105 animation.update(t, dt);
106
107 Mf::Vector3 center(mState.position[0], mState.position[1], z);
108 Mf::Vector3 a(mState.position[0] - 16.0, mState.position[1] - 16.0, z);
109 Mf::Vector3 b(mState.position[0] + 16.0, mState.position[1] + 16.0, z);
110
111 mAabb.init(a, b);
112 mSphere.init(center, a);
113 }
114
115
116 void Character::draw(Mf::Scalar alpha) const
117 {
118 //Mf::Vector2 position = cml::lerp(mPrevState.position, mState.position, alpha);
119 Mf::State2 state = getState(alpha);
120 Mf::Vector2 position = state.position;
121
122 //glColor3f(1.0f, 1.0f, 1.0f);
123 tilemap.bind();
124
125 Tilemap::Index frame = animation.getFrame();
126
127 Tilemap::Orientation orientation = Tilemap::NORMAL;
128
129 if (mState.velocity[0] < 0.0) orientation = Tilemap::REVERSE;
130
131 Mf::Scalar coords[8];
132 tilemap.getTileCoords(frame, coords, orientation);
133
134 Mf::Scalar s = 16.0;
135
136 glBegin(GL_TRIANGLE_FAN);
137 glTexCoord2f(coords[0], coords[1]);
138 glVertex3(position[0]-s, position[1]-s, z);
139 glTexCoord2f(coords[2], coords[3]);
140 glVertex3(position[0]+s, position[1]-s, z);
141 glTexCoord2f(coords[4], coords[5]);
142 glVertex3(position[0]+s, position[1]+s, z);
143 glTexCoord2f(coords[6], coords[7]);
144 glVertex3(position[0]-s, position[1]+s, z);
145 glEnd();
146
147 //glColor3f(0.0f, 0.0f, 0.0f);
148 Mf::Texture::resetBind();
149
150 glBegin(GL_TRIANGLES);
151 glVertex3(480.0, 190.0, 64.0);
152 glVertex3(520.0, 190.0, 64.0);
153 glVertex3(500.0, 210.0, 64.0);
154 glEnd();
155
156 //glColor3f(1.0f, 1.0f, 1.0f);
157 }
158
159
160 int Character::getOctant(const Mf::Aabb& aabb) const
161 {
162 int octantNum = -1;
163
164 Mf::Plane::Halfspace halfspace;
165
166 Mf::Plane xy = aabb.getPlaneXY();
167 halfspace = xy.intersects(mSphere);
168 if (halfspace == Mf::Plane::INTERSECT)
169 {
170 halfspace = xy.intersects(mAabb);
171 }
172
173 if (halfspace == Mf::Plane::POSITIVE)
174 {
175 Mf::Plane xz = aabb.getPlaneXZ();
176 halfspace = xz.intersects(mSphere);
177 if (halfspace == Mf::Plane::INTERSECT)
178 {
179 halfspace = xz.intersects(mAabb);
180 }
181
182 if (halfspace == Mf::Plane::POSITIVE)
183 {
184 Mf::Plane yz = aabb.getPlaneYZ();
185 halfspace = yz.intersects(mSphere);
186 if (halfspace == Mf::Plane::INTERSECT)
187 {
188 halfspace = yz.intersects(mAabb);
189 }
190
191 if (halfspace == Mf::Plane::POSITIVE)
192 {
193 octantNum = 2;
194 }
195 else if (halfspace == Mf::Plane::NEGATIVE)
196 {
197 octantNum = 3;
198 }
199 }
200 else if (halfspace == Mf::Plane::NEGATIVE)
201 {
202 Mf::Plane yz = aabb.getPlaneYZ();
203 halfspace = yz.intersects(mSphere);
204 if (halfspace == Mf::Plane::INTERSECT)
205 {
206 halfspace = yz.intersects(mAabb);
207 }
208
209 if (halfspace == Mf::Plane::POSITIVE)
210 {
211 octantNum = 1;
212 }
213 else if (halfspace == Mf::Plane::NEGATIVE)
214 {
215 octantNum = 0;
216 }
217 }
218 }
219 else if (halfspace == Mf::Plane::NEGATIVE)
220 {
221 Mf::Plane xz = aabb.getPlaneXZ();
222 halfspace = xz.intersects(mSphere);
223 if (halfspace == Mf::Plane::INTERSECT)
224 {
225 halfspace = xz.intersects(mAabb);
226 }
227
228 if (halfspace == Mf::Plane::POSITIVE)
229 {
230 Mf::Plane yz = aabb.getPlaneYZ();
231 halfspace = yz.intersects(mSphere);
232 if (halfspace == Mf::Plane::INTERSECT)
233 {
234 halfspace = yz.intersects(mAabb);
235 }
236
237 if (halfspace == Mf::Plane::POSITIVE)
238 {
239 octantNum = 6;
240 }
241 else if (halfspace == Mf::Plane::NEGATIVE)
242 {
243 octantNum = 7;
244 }
245 }
246 else if (halfspace == Mf::Plane::NEGATIVE)
247 {
248 Mf::Plane yz = aabb.getPlaneYZ();
249 halfspace = yz.intersects(mSphere);
250 if (halfspace == Mf::Plane::INTERSECT)
251 {
252 halfspace = yz.intersects(mAabb);
253 }
254
255 if (halfspace == Mf::Plane::POSITIVE)
256 {
257 octantNum = 5;
258 }
259 else if (halfspace == Mf::Plane::NEGATIVE)
260 {
261 octantNum = 4;
262 }
263 }
264 }
265
266 return octantNum;
267 }
268
269
270 /** vim: set ts=4 sw=4 tw=80: *************************************************/
271
This page took 0.042938 seconds and 5 git commands to generate.