]>
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 <moof/log.hh>
16 #include "Character.hh"
23 explicit SpringForce(moof::vector2 x
) :
26 const moof::vector2
& operator () (const moof::linear_state
<2>& state
)
28 moof::vector2 x
= state
.position
- location
;
29 moof::scalar mag
= x
.length();
33 //state_.force += -15.0 * x - 1.5 * state_.velocity;
34 force
= SCALAR(-10.0) * (mag
- d
) * (x
/ mag
);// - SCALAR(2.0) * state.velocity;
42 moof::vector2 location
;
49 explicit ResistanceForce(moof::scalar scale
= 1.0) :
52 const moof::vector2
& operator () (const moof::linear_state
<2>& state
)
54 force
= -k
* state
.velocity
;
65 Character::Character(const std::string
& name
) :
72 state_
.inverse_mass
= 1.0 / state_
.mass
;
75 state_
.force
= moof::vector2(0.0, 0.0);
76 state_
.forces
.push_back(SpringForce(moof::vector2(5.0, 4.0)));
77 state_
.forces
.push_back(ResistanceForce(2.0));
78 state_
.forces
.push_back(moof::linear_state
<2>::gravity_force(-9.8));
81 state_
.position
= moof::vector2(5.0, 5.0);
82 state_
.momentum
= moof::vector2(1.0, 0.0);
89 void Character::update(moof::scalar t
, moof::scalar dt
)
91 moof::rigid_body2::update(t
, dt
); // update physics
93 animation
.update(t
, dt
);
95 moof::vector3
center(state_
.position
[0], state_
.position
[1], 0.0);
96 moof::vector3
a(state_
.position
[0] - 0.5, state_
.position
[1] - 0.5, 0.0);
97 moof::vector3
b(state_
.position
[0] + 0.5, state_
.position
[1] + 0.5, 0.0);
100 sphere_
.init(center
, a
);
104 void Character::draw(moof::scalar alpha
) const
106 moof::state2 state
= moof::rigid_body2::state(alpha
);
107 moof::vector2 position
= state
.position
;
109 //glColor3f(1.0f, 1.0f, 1.0f);
112 int frame
= animation
.getFrame();
113 moof::texture::orientation orientation
= moof::texture::normal
;
115 if (state_
.velocity
[0] < 0.0) orientation
= moof::texture::reverse
;
117 moof::scalar coords
[8];
118 tilemap
.tile_coordinates(frame
, coords
, orientation
);
120 moof::scalar s
= 0.5;
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 moof::Aabb<3>& aabb) const
139 moof::plane::halfspace halfspace;
141 moof::plane xy = aabb.xy_plane();
142 halfspace = xy.intersects(sphere_);
143 if (halfspace == moof::plane::intersecting)
145 halfspace = xy.intersects(aabb_);
148 if (halfspace == moof::plane::positive)
150 moof::plane xz = aabb.xz_plane();
151 halfspace = xz.intersects(sphere_);
152 if (halfspace == moof::plane::intersecting)
154 halfspace = xz.intersects(aabb_);
157 if (halfspace == moof::plane::positive)
159 moof::plane yz = aabb.yz_plane();
160 halfspace = yz.intersects(sphere_);
161 if (halfspace == moof::plane::intersecting)
163 halfspace = yz.intersects(aabb_);
166 if (halfspace == moof::plane::positive)
170 else if (halfspace == moof::plane::negative)
175 else if (halfspace == moof::plane::negative)
177 moof::plane yz = aabb.yz_plane();
178 halfspace = yz.intersects(sphere_);
179 if (halfspace == moof::plane::intersecting)
181 halfspace = yz.intersects(aabb_);
184 if (halfspace == moof::plane::positive)
188 else if (halfspace == moof::plane::negative)
194 else if (halfspace == moof::plane::negative)
196 moof::plane xz = aabb.xz_plane();
197 halfspace = xz.intersects(sphere_);
198 if (halfspace == moof::plane::intersecting)
200 halfspace = xz.intersects(aabb_);
203 if (halfspace == moof::plane::positive)
205 moof::plane yz = aabb.yz_plane();
206 halfspace = yz.intersects(sphere_);
207 if (halfspace == moof::plane::intersecting)
209 halfspace = yz.intersects(aabb_);
212 if (halfspace == moof::plane::positive)
216 else if (halfspace == moof::plane::negative)
221 else if (halfspace == moof::plane::negative)
223 moof::plane yz = aabb.yz_plane();
224 halfspace = yz.intersects(sphere_);
225 if (halfspace == moof::plane::intersecting)
227 halfspace = yz.intersects(aabb_);
230 if (halfspace == moof::plane::positive)
234 else if (halfspace == moof::plane::negative)
246 void Character::addImpulse(moof::vector2 impulse
)
248 state_
.momentum
+= impulse
;
251 void Character::addForce(moof::vector2 force
)
253 state_
.force
+= force
;
256 void Character::setPosition(moof::vector2 position
)
258 state_
.position
= position
;
This page took 0.048141 seconds and 4 git commands to generate.