]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
31 #include "Character.hh"
37 explicit SpringForce(Mf::Vector2 x
) :
40 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
42 Mf::Vector2 x
= state
.position
- location
;
43 Mf::Scalar mag
= x
.length();
47 //current.force += -15.0 * x - 1.5 * current.velocity;
48 force
= -20.0 * (mag
- d
) * (x
/ mag
) - 2.0 * state
.velocity
;
59 struct ResistanceForce
61 explicit ResistanceForce(Mf::Scalar scale
= 1.0) :
64 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
66 force
= -k
* state
.velocity
;
77 Character::Character(const std::string
& name
) :
84 current
.inverseMass
= 1.0 / current
.mass
;
87 current
.force
= Mf::Vector2(0.0, 0.0);
88 current
.forces
.push_back(SpringForce(Mf::Vector2(500.0, 200.0)));
89 current
.forces
.push_back(ResistanceForce(2.0));
90 current
.forces
.push_back(Mf::LinearState
<2>::GravityForce(-1000.0));
93 current
.position
= Mf::Vector2(64.0, 64.0);
94 current
.momentum
= Mf::Vector2(0.0, 0.0);
95 current
.recalculate();
101 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
105 //Mf::Vector2 x = current.position - Mf::Vector2(500.0, 200.0);
106 //Mf::Scalar mag = x.length();
107 //Mf::Scalar d = 50.0;
110 //current.force = Mf::Vector2(0.0, -2000.0);
112 ////current.force += -15.0 * x - 1.5 * current.velocity;
113 //current.force += -20.0 * (mag - d) * (x / mag) - 2.0 * current.velocity;
115 //current.force += userForce;
116 //current.recalculate();
117 //std::cout << "force: " << current.momentum << std::endl;
119 //Mf::euler<State,Derivative>(current, t, dt);
121 current
.integrate(t
, dt
);
122 animation
.update(t
, dt
);
124 Mf::Vector3
center(current
.position
[0], current
.position
[1], z
);
125 Mf::Vector3
a(current
.position
[0] - 16.0, current
.position
[1] - 16.0, z
);
126 Mf::Vector3
b(current
.position
[0] + 16.0, current
.position
[1] + 16.0, z
);
129 sphere_
.init(center
, a
);
133 void Character::draw(Mf::Scalar alpha
) const
135 Mf::Vector2 position
= cml::lerp(previous
.position
, current
.position
, alpha
);
137 //glColor3f(1.0f, 1.0f, 1.0f);
140 Tilemap::Index frame
= animation
.getFrame();
142 Tilemap::Orientation orientation
= Tilemap::NORMAL
;
144 if (current
.velocity
[0] < 0.0) orientation
= Tilemap::REVERSE
;
146 Mf::Scalar coords
[8];
147 tilemap
.getTileCoords(frame
, coords
, orientation
);
151 glBegin(GL_TRIANGLE_FAN
);
152 glTexCoord2f(coords
[0], coords
[1]);
153 glVertex3(position
[0]-s
, position
[1]-s
, z
);
154 glTexCoord2f(coords
[2], coords
[3]);
155 glVertex3(position
[0]+s
, position
[1]-s
, z
);
156 glTexCoord2f(coords
[4], coords
[5]);
157 glVertex3(position
[0]+s
, position
[1]+s
, z
);
158 glTexCoord2f(coords
[6], coords
[7]);
159 glVertex3(position
[0]-s
, position
[1]+s
, z
);
162 //glColor3f(0.0f, 0.0f, 0.0f);
163 Mf::Texture::resetBind();
165 glBegin(GL_TRIANGLES
);
166 glVertex3(480.0, 190.0, 64.0);
167 glVertex3(520.0, 190.0, 64.0);
168 glVertex3(500.0, 210.0, 64.0);
171 //glColor3f(1.0f, 1.0f, 1.0f);
175 bool Character::isInsideAabb(const Mf::Aabb
& aabb
) const
177 // make sure the entity is fully inside the volume
178 if (!(aabb_
.max
[0] < aabb
.max
[0] &&
179 aabb_
.min
[0] > aabb
.min
[0] &&
180 aabb_
.max
[1] < aabb
.max
[1] &&
181 aabb_
.min
[1] > aabb
.min
[1] &&
182 aabb_
.max
[2] < aabb
.max
[2] &&
183 aabb_
.min
[2] > aabb
.min
[2]))
191 int Character::getOctant(const Mf::Aabb
& aabb
) const
195 Mf::Plane::Halfspace halfspace
;
197 Mf::Plane xy
= aabb
.getPlaneXY();
198 halfspace
= xy
.intersects(sphere_
);
199 if (halfspace
== Mf::Plane::INTERSECT
)
201 halfspace
= xy
.intersects(aabb_
);
204 if (halfspace
== Mf::Plane::POSITIVE
)
206 Mf::Plane xz
= aabb
.getPlaneXZ();
207 halfspace
= xz
.intersects(sphere_
);
208 if (halfspace
== Mf::Plane::INTERSECT
)
210 halfspace
= xz
.intersects(aabb_
);
213 if (halfspace
== Mf::Plane::POSITIVE
)
215 Mf::Plane yz
= aabb
.getPlaneYZ();
216 halfspace
= yz
.intersects(sphere_
);
217 if (halfspace
== Mf::Plane::INTERSECT
)
219 halfspace
= yz
.intersects(aabb_
);
222 if (halfspace
== Mf::Plane::POSITIVE
)
226 else if (halfspace
== Mf::Plane::NEGATIVE
)
231 else if (halfspace
== Mf::Plane::NEGATIVE
)
233 Mf::Plane yz
= aabb
.getPlaneYZ();
234 halfspace
= yz
.intersects(sphere_
);
235 if (halfspace
== Mf::Plane::INTERSECT
)
237 halfspace
= yz
.intersects(aabb_
);
240 if (halfspace
== Mf::Plane::POSITIVE
)
244 else if (halfspace
== Mf::Plane::NEGATIVE
)
250 else if (halfspace
== Mf::Plane::NEGATIVE
)
252 Mf::Plane xz
= aabb
.getPlaneXZ();
253 halfspace
= xz
.intersects(sphere_
);
254 if (halfspace
== Mf::Plane::INTERSECT
)
256 halfspace
= xz
.intersects(aabb_
);
259 if (halfspace
== Mf::Plane::POSITIVE
)
261 Mf::Plane yz
= aabb
.getPlaneYZ();
262 halfspace
= yz
.intersects(sphere_
);
263 if (halfspace
== Mf::Plane::INTERSECT
)
265 halfspace
= yz
.intersects(aabb_
);
268 if (halfspace
== Mf::Plane::POSITIVE
)
272 else if (halfspace
== Mf::Plane::NEGATIVE
)
277 else if (halfspace
== Mf::Plane::NEGATIVE
)
279 Mf::Plane yz
= aabb
.getPlaneYZ();
280 halfspace
= yz
.intersects(sphere_
);
281 if (halfspace
== Mf::Plane::INTERSECT
)
283 halfspace
= yz
.intersects(aabb_
);
286 if (halfspace
== Mf::Plane::POSITIVE
)
290 else if (halfspace
== Mf::Plane::NEGATIVE
)
301 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.059758 seconds and 4 git commands to generate.