]>
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"
39 explicit SpringForce(Mf::Vector2 x
) :
42 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
44 Mf::Vector2 x
= state
.position
- location
;
45 Mf::Scalar mag
= x
.length();
49 //mState.force += -15.0 * x - 1.5 * mState.velocity;
50 force
= SCALAR(-10.0) * (mag
- d
) * (x
/ mag
) - 2.0 * state
.velocity
;
65 explicit ResistanceForce(Mf::Scalar scale
= 1.0) :
68 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
70 force
= -k
* state
.velocity
;
81 Character::Character(const std::string
& name
) :
88 mState
.inverseMass
= 1.0 / mState
.mass
;
91 mState
.force
= Mf::Vector2(0.0, 0.0);
92 //mState.forces.push_back(SpringForce(Mf::Vector2(20.0, 4.0)));
93 mState
.forces
.push_back(ResistanceForce(2.0));
94 //mState.forces.push_back(Mf::LinearState<2>::GravityForce(-9.8));
97 mState
.position
= Mf::Vector2(5.0, 5.0);
98 mState
.momentum
= Mf::Vector2(0.0, 0.0);
105 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
107 Mf::RigidBody2::update(t
, dt
); // update physics
109 animation
.update(t
, dt
);
111 Mf::Vector3
center(mState
.position
[0], mState
.position
[1], 0.0);
112 Mf::Vector3
a(mState
.position
[0] - 0.5, mState
.position
[1] - 0.5, 0.0);
113 Mf::Vector3
b(mState
.position
[0] + 0.5, mState
.position
[1] + 0.5, 0.0);
116 mSphere
.init(center
, a
);
120 void Character::draw(Mf::Scalar alpha
) const
122 //Mf::Vector2 position = cml::lerp(mPrevState.position, mState.position, alpha);
123 Mf::State2 state
= getState(alpha
);
124 Mf::Vector2 position
= state
.position
;
126 //glColor3f(1.0f, 1.0f, 1.0f);
129 Mf::Texture::TileIndex frame
= animation
.getFrame();
130 Mf::Texture::Orientation orientation
= Mf::Texture::NORMAL
;
132 if (mState
.velocity
[0] < 0.0) orientation
= Mf::Texture::REVERSE
;
134 Mf::Scalar coords
[8];
135 tilemap
.getTileCoords(frame
, coords
, orientation
);
139 glBegin(GL_TRIANGLE_FAN
);
140 glTexCoord(coords
[0], coords
[1]);
141 glVertex(position
[0]-s
, position
[1]-s
);
142 glTexCoord(coords
[2], coords
[3]);
143 glVertex(position
[0]+s
, position
[1]-s
);
144 glTexCoord(coords
[4], coords
[5]);
145 glVertex(position
[0]+s
, position
[1]+s
);
146 glTexCoord(coords
[6], coords
[7]);
147 glVertex(position
[0]-s
, position
[1]+s
);
152 /*int Character::getOctant(const Mf::Aabb<3>& aabb) const
156 Mf::Plane::Halfspace halfspace;
158 Mf::Plane xy = aabb.getPlaneXY();
159 halfspace = xy.intersects(mSphere);
160 if (halfspace == Mf::Plane::INTERSECT)
162 halfspace = xy.intersects(mAabb);
165 if (halfspace == Mf::Plane::POSITIVE)
167 Mf::Plane xz = aabb.getPlaneXZ();
168 halfspace = xz.intersects(mSphere);
169 if (halfspace == Mf::Plane::INTERSECT)
171 halfspace = xz.intersects(mAabb);
174 if (halfspace == Mf::Plane::POSITIVE)
176 Mf::Plane yz = aabb.getPlaneYZ();
177 halfspace = yz.intersects(mSphere);
178 if (halfspace == Mf::Plane::INTERSECT)
180 halfspace = yz.intersects(mAabb);
183 if (halfspace == Mf::Plane::POSITIVE)
187 else if (halfspace == Mf::Plane::NEGATIVE)
192 else if (halfspace == Mf::Plane::NEGATIVE)
194 Mf::Plane yz = aabb.getPlaneYZ();
195 halfspace = yz.intersects(mSphere);
196 if (halfspace == Mf::Plane::INTERSECT)
198 halfspace = yz.intersects(mAabb);
201 if (halfspace == Mf::Plane::POSITIVE)
205 else if (halfspace == Mf::Plane::NEGATIVE)
211 else if (halfspace == Mf::Plane::NEGATIVE)
213 Mf::Plane xz = aabb.getPlaneXZ();
214 halfspace = xz.intersects(mSphere);
215 if (halfspace == Mf::Plane::INTERSECT)
217 halfspace = xz.intersects(mAabb);
220 if (halfspace == Mf::Plane::POSITIVE)
222 Mf::Plane yz = aabb.getPlaneYZ();
223 halfspace = yz.intersects(mSphere);
224 if (halfspace == Mf::Plane::INTERSECT)
226 halfspace = yz.intersects(mAabb);
229 if (halfspace == Mf::Plane::POSITIVE)
233 else if (halfspace == Mf::Plane::NEGATIVE)
238 else if (halfspace == Mf::Plane::NEGATIVE)
240 Mf::Plane yz = aabb.getPlaneYZ();
241 halfspace = yz.intersects(mSphere);
242 if (halfspace == Mf::Plane::INTERSECT)
244 halfspace = yz.intersects(mAabb);
247 if (halfspace == Mf::Plane::POSITIVE)
251 else if (halfspace == Mf::Plane::NEGATIVE)
263 void Character::addImpulse(Mf::Vector2 impulse
)
265 mState
.momentum
+= impulse
;
268 void Character::addForce(Mf::Vector2 force
)
270 mState
.force
+= force
;
273 void Character::setPosition(Mf::Vector2 position
)
275 mState
.position
= position
;
279 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.051219 seconds and 4 git commands to generate.