]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
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
);// - SCALAR(2.0) * state.velocity;
48 explicit ResistanceForce(Mf::Scalar scale
= 1.0) :
51 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
53 force
= -k
* state
.velocity
;
64 Character::Character(const std::string
& name
) :
71 mState
.inverseMass
= 1.0 / mState
.mass
;
74 mState
.force
= Mf::Vector2(0.0, 0.0);
75 //mState.forces.push_back(SpringForce(Mf::Vector2(5.0, 4.0)));
76 mState
.forces
.push_back(ResistanceForce(2.0));
77 //mState.forces.push_back(Mf::LinearState<2>::GravityForce(-9.8));
80 mState
.position
= Mf::Vector2(5.0, 5.0);
81 mState
.momentum
= Mf::Vector2(0.0, 0.0);
88 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
90 Mf::RigidBody2::update(t
, dt
); // update physics
92 animation
.update(t
, dt
);
94 Mf::Vector3
center(mState
.position
[0], mState
.position
[1], 0.0);
95 Mf::Vector3
a(mState
.position
[0] - 0.5, mState
.position
[1] - 0.5, 0.0);
96 Mf::Vector3
b(mState
.position
[0] + 0.5, mState
.position
[1] + 0.5, 0.0);
99 mSphere
.init(center
, a
);
103 void Character::draw(Mf::Scalar alpha
) const
105 Mf::State2 state
= getState(alpha
);
106 Mf::Vector2 position
= state
.position
;
108 //glColor3f(1.0f, 1.0f, 1.0f);
111 Mf::Texture::TileIndex frame
= animation
.getFrame();
112 Mf::Texture::Orientation orientation
= Mf::Texture::NORMAL
;
114 if (mState
.velocity
[0] < 0.0) orientation
= Mf::Texture::REVERSE
;
116 Mf::Scalar coords
[8];
117 tilemap
.getTileCoords(frame
, coords
, orientation
);
121 glBegin(GL_TRIANGLE_FAN
);
122 glTexCoord(coords
[0], coords
[1]);
123 glVertex(position
[0]-s
, position
[1]-s
);
124 glTexCoord(coords
[2], coords
[3]);
125 glVertex(position
[0]+s
, position
[1]-s
);
126 glTexCoord(coords
[4], coords
[5]);
127 glVertex(position
[0]+s
, position
[1]+s
);
128 glTexCoord(coords
[6], coords
[7]);
129 glVertex(position
[0]-s
, position
[1]+s
);
134 /*int Character::getOctant(const Mf::Aabb<3>& aabb) const
138 Mf::Plane::Halfspace halfspace;
140 Mf::Plane xy = aabb.getPlaneXY();
141 halfspace = xy.intersects(mSphere);
142 if (halfspace == Mf::Plane::INTERSECT)
144 halfspace = xy.intersects(mAabb);
147 if (halfspace == Mf::Plane::POSITIVE)
149 Mf::Plane xz = aabb.getPlaneXZ();
150 halfspace = xz.intersects(mSphere);
151 if (halfspace == Mf::Plane::INTERSECT)
153 halfspace = xz.intersects(mAabb);
156 if (halfspace == Mf::Plane::POSITIVE)
158 Mf::Plane yz = aabb.getPlaneYZ();
159 halfspace = yz.intersects(mSphere);
160 if (halfspace == Mf::Plane::INTERSECT)
162 halfspace = yz.intersects(mAabb);
165 if (halfspace == Mf::Plane::POSITIVE)
169 else if (halfspace == Mf::Plane::NEGATIVE)
174 else if (halfspace == Mf::Plane::NEGATIVE)
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)
193 else if (halfspace == Mf::Plane::NEGATIVE)
195 Mf::Plane xz = aabb.getPlaneXZ();
196 halfspace = xz.intersects(mSphere);
197 if (halfspace == Mf::Plane::INTERSECT)
199 halfspace = xz.intersects(mAabb);
202 if (halfspace == Mf::Plane::POSITIVE)
204 Mf::Plane yz = aabb.getPlaneYZ();
205 halfspace = yz.intersects(mSphere);
206 if (halfspace == Mf::Plane::INTERSECT)
208 halfspace = yz.intersects(mAabb);
211 if (halfspace == Mf::Plane::POSITIVE)
215 else if (halfspace == Mf::Plane::NEGATIVE)
220 else if (halfspace == Mf::Plane::NEGATIVE)
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)
245 void Character::addImpulse(Mf::Vector2 impulse
)
247 mState
.momentum
+= impulse
;
250 void Character::addForce(Mf::Vector2 force
)
252 mState
.force
+= force
;
255 void Character::setPosition(Mf::Vector2 position
)
257 mState
.position
= position
;
This page took 0.043332 seconds and 4 git commands to generate.