]>
Dogcows Code - chaz/yoink/blob - Character.cc
6a7c69883ea6ddeeda7f8ec77f9c806f0b76c080
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
14 #include "Character.hh"
22 explicit SpringForce(Mf::Vector2 x
) :
25 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
27 Mf::Vector2 x
= state
.position
- location
;
28 Mf::Scalar mag
= x
.length();
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
;
49 explicit ResistanceForce(Mf::Scalar scale
= 1.0) :
52 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
54 force
= -k
* state
.velocity
;
65 Character::Character(const std::string
& name
) :
72 mState
.inverseMass
= 1.0 / mState
.mass
;
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));
81 mState
.position
= Mf::Vector2(5.0, 5.0);
82 mState
.momentum
= Mf::Vector2(0.0, 0.0);
89 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
91 Mf::RigidBody2::update(t
, dt
); // update physics
93 animation
.update(t
, dt
);
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);
100 mSphere
.init(center
, a
);
104 void Character::draw(Mf::Scalar alpha
) const
106 Mf::State2 state
= getState(alpha
);
107 Mf::Vector2 position
= state
.position
;
109 //glColor3f(1.0f, 1.0f, 1.0f);
112 Mf::Texture::TileIndex frame
= animation
.getFrame();
113 Mf::Texture::Orientation orientation
= Mf::Texture::NORMAL
;
115 if (mState
.velocity
[0] < 0.0) orientation
= Mf::Texture::REVERSE
;
117 Mf::Scalar coords
[8];
118 tilemap
.getTileCoords(frame
, coords
, orientation
);
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
);
135 /*int Character::getOctant(const Mf::Aabb<3>& aabb) const
139 Mf::Plane::Halfspace halfspace;
141 Mf::Plane xy = aabb.getPlaneXY();
142 halfspace = xy.intersects(mSphere);
143 if (halfspace == Mf::Plane::INTERSECT)
145 halfspace = xy.intersects(mAabb);
148 if (halfspace == Mf::Plane::POSITIVE)
150 Mf::Plane xz = aabb.getPlaneXZ();
151 halfspace = xz.intersects(mSphere);
152 if (halfspace == Mf::Plane::INTERSECT)
154 halfspace = xz.intersects(mAabb);
157 if (halfspace == Mf::Plane::POSITIVE)
159 Mf::Plane yz = aabb.getPlaneYZ();
160 halfspace = yz.intersects(mSphere);
161 if (halfspace == Mf::Plane::INTERSECT)
163 halfspace = yz.intersects(mAabb);
166 if (halfspace == Mf::Plane::POSITIVE)
170 else if (halfspace == Mf::Plane::NEGATIVE)
175 else if (halfspace == Mf::Plane::NEGATIVE)
177 Mf::Plane yz = aabb.getPlaneYZ();
178 halfspace = yz.intersects(mSphere);
179 if (halfspace == Mf::Plane::INTERSECT)
181 halfspace = yz.intersects(mAabb);
184 if (halfspace == Mf::Plane::POSITIVE)
188 else if (halfspace == Mf::Plane::NEGATIVE)
194 else if (halfspace == Mf::Plane::NEGATIVE)
196 Mf::Plane xz = aabb.getPlaneXZ();
197 halfspace = xz.intersects(mSphere);
198 if (halfspace == Mf::Plane::INTERSECT)
200 halfspace = xz.intersects(mAabb);
203 if (halfspace == Mf::Plane::POSITIVE)
205 Mf::Plane yz = aabb.getPlaneYZ();
206 halfspace = yz.intersects(mSphere);
207 if (halfspace == Mf::Plane::INTERSECT)
209 halfspace = yz.intersects(mAabb);
212 if (halfspace == Mf::Plane::POSITIVE)
216 else if (halfspace == Mf::Plane::NEGATIVE)
221 else if (halfspace == Mf::Plane::NEGATIVE)
223 Mf::Plane yz = aabb.getPlaneYZ();
224 halfspace = yz.intersects(mSphere);
225 if (halfspace == Mf::Plane::INTERSECT)
227 halfspace = yz.intersects(mAabb);
230 if (halfspace == Mf::Plane::POSITIVE)
234 else if (halfspace == Mf::Plane::NEGATIVE)
246 void Character::addImpulse(Mf::Vector2 impulse
)
248 mState
.momentum
+= impulse
;
251 void Character::addForce(Mf::Vector2 force
)
253 mState
.force
+= force
;
256 void Character::setPosition(Mf::Vector2 position
)
258 mState
.position
= position
;
This page took 0.042281 seconds and 3 git commands to generate.