]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
f12ed5f2d88f66935f5b3923fd0d1f7b57632f9e
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 //mState.force += -15.0 * x - 1.5 * mState.velocity;
48 force
= SCALAR(-10.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 mState
.inverseMass
= 1.0 / mState
.mass
;
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));
93 mState
.position
= Mf::Vector2(64.0, 64.0);
94 mState
.momentum
= Mf::Vector2(0.0, 0.0);
101 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
103 Mf::RigidBody2::update(t
, dt
); // update physics
105 animation
.update(t
, dt
);
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
);
112 mSphere
.init(center
, a
);
116 void Character::draw(Mf::Scalar alpha
) const
118 //Mf::Vector2 position = cml::lerp(mPrevState.position, mState.position, alpha);
119 Mf::State2 state
= getState(alpha
);
120 Mf::Vector2 position
= state
.position
;
122 //glColor3f(1.0f, 1.0f, 1.0f);
125 Tilemap::Index frame
= animation
.getFrame();
127 Tilemap::Orientation orientation
= Tilemap::NORMAL
;
129 if (mState
.velocity
[0] < 0.0) orientation
= Tilemap::REVERSE
;
131 Mf::Scalar coords
[8];
132 tilemap
.getTileCoords(frame
, coords
, orientation
);
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
);
147 //glColor3f(0.0f, 0.0f, 0.0f);
148 Mf::Texture::resetBind();
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);
156 //glColor3f(1.0f, 1.0f, 1.0f);
160 int Character::getOctant(const Mf::Aabb
& aabb
) const
164 Mf::Plane::Halfspace halfspace
;
166 Mf::Plane xy
= aabb
.getPlaneXY();
167 halfspace
= xy
.intersects(mSphere
);
168 if (halfspace
== Mf::Plane::INTERSECT
)
170 halfspace
= xy
.intersects(mAabb
);
173 if (halfspace
== Mf::Plane::POSITIVE
)
175 Mf::Plane xz
= aabb
.getPlaneXZ();
176 halfspace
= xz
.intersects(mSphere
);
177 if (halfspace
== Mf::Plane::INTERSECT
)
179 halfspace
= xz
.intersects(mAabb
);
182 if (halfspace
== Mf::Plane::POSITIVE
)
184 Mf::Plane yz
= aabb
.getPlaneYZ();
185 halfspace
= yz
.intersects(mSphere
);
186 if (halfspace
== Mf::Plane::INTERSECT
)
188 halfspace
= yz
.intersects(mAabb
);
191 if (halfspace
== Mf::Plane::POSITIVE
)
195 else if (halfspace
== Mf::Plane::NEGATIVE
)
200 else if (halfspace
== Mf::Plane::NEGATIVE
)
202 Mf::Plane yz
= aabb
.getPlaneYZ();
203 halfspace
= yz
.intersects(mSphere
);
204 if (halfspace
== Mf::Plane::INTERSECT
)
206 halfspace
= yz
.intersects(mAabb
);
209 if (halfspace
== Mf::Plane::POSITIVE
)
213 else if (halfspace
== Mf::Plane::NEGATIVE
)
219 else if (halfspace
== Mf::Plane::NEGATIVE
)
221 Mf::Plane xz
= aabb
.getPlaneXZ();
222 halfspace
= xz
.intersects(mSphere
);
223 if (halfspace
== Mf::Plane::INTERSECT
)
225 halfspace
= xz
.intersects(mAabb
);
228 if (halfspace
== Mf::Plane::POSITIVE
)
230 Mf::Plane yz
= aabb
.getPlaneYZ();
231 halfspace
= yz
.intersects(mSphere
);
232 if (halfspace
== Mf::Plane::INTERSECT
)
234 halfspace
= yz
.intersects(mAabb
);
237 if (halfspace
== Mf::Plane::POSITIVE
)
241 else if (halfspace
== Mf::Plane::NEGATIVE
)
246 else if (halfspace
== Mf::Plane::NEGATIVE
)
248 Mf::Plane yz
= aabb
.getPlaneYZ();
249 halfspace
= yz
.intersects(mSphere
);
250 if (halfspace
== Mf::Plane::INTERSECT
)
252 halfspace
= yz
.intersects(mAabb
);
255 if (halfspace
== Mf::Plane::POSITIVE
)
259 else if (halfspace
== Mf::Plane::NEGATIVE
)
270 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.050268 seconds and 4 git commands to generate.