]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
c5d48385c8793bc71db777f4e3416bc4dabe3909
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
);
102 int frame
= animation
.getFrame();
107 void Character::draw(moof::scalar alpha
) const
109 moof::state2 state
= moof::rigid_body2::state(alpha
);
110 const moof::vector2
& position
= state
.position
;
112 const moof::scalar s
= 0.5;
114 moof::vector3 coords
[4];
116 coords
[0] = moof::vector3(position
[0] - s
, position
[1] - s
, SCALAR(0.0));
117 coords
[1] = moof::vector3(position
[0] + s
, position
[1] - s
, SCALAR(0.0));
118 coords
[2] = moof::vector3(position
[0] + s
, position
[1] + s
, SCALAR(0.0));
119 coords
[3] = moof::vector3(position
[0] - s
, position
[1] + s
, SCALAR(0.0));
121 tilemap
.draw(coords
);
125 /*int Character::getOctant(const moof::Aabb<3>& aabb) const
129 moof::plane::halfspace halfspace;
131 moof::plane xy = aabb.xy_plane();
132 halfspace = xy.intersects(sphere_);
133 if (halfspace == moof::plane::intersecting)
135 halfspace = xy.intersects(aabb_);
138 if (halfspace == moof::plane::positive)
140 moof::plane xz = aabb.xz_plane();
141 halfspace = xz.intersects(sphere_);
142 if (halfspace == moof::plane::intersecting)
144 halfspace = xz.intersects(aabb_);
147 if (halfspace == moof::plane::positive)
149 moof::plane yz = aabb.yz_plane();
150 halfspace = yz.intersects(sphere_);
151 if (halfspace == moof::plane::intersecting)
153 halfspace = yz.intersects(aabb_);
156 if (halfspace == moof::plane::positive)
160 else if (halfspace == moof::plane::negative)
165 else if (halfspace == moof::plane::negative)
167 moof::plane yz = aabb.yz_plane();
168 halfspace = yz.intersects(sphere_);
169 if (halfspace == moof::plane::intersecting)
171 halfspace = yz.intersects(aabb_);
174 if (halfspace == moof::plane::positive)
178 else if (halfspace == moof::plane::negative)
184 else if (halfspace == moof::plane::negative)
186 moof::plane xz = aabb.xz_plane();
187 halfspace = xz.intersects(sphere_);
188 if (halfspace == moof::plane::intersecting)
190 halfspace = xz.intersects(aabb_);
193 if (halfspace == moof::plane::positive)
195 moof::plane yz = aabb.yz_plane();
196 halfspace = yz.intersects(sphere_);
197 if (halfspace == moof::plane::intersecting)
199 halfspace = yz.intersects(aabb_);
202 if (halfspace == moof::plane::positive)
206 else if (halfspace == moof::plane::negative)
211 else if (halfspace == moof::plane::negative)
213 moof::plane yz = aabb.yz_plane();
214 halfspace = yz.intersects(sphere_);
215 if (halfspace == moof::plane::intersecting)
217 halfspace = yz.intersects(aabb_);
220 if (halfspace == moof::plane::positive)
224 else if (halfspace == moof::plane::negative)
236 void Character::addImpulse(moof::vector2 impulse
)
238 state_
.momentum
+= impulse
;
241 void Character::addForce(moof::vector2 force
)
243 state_
.force
+= force
;
246 void Character::setPosition(moof::vector2 position
)
248 state_
.position
= position
;
This page took 0.041986 seconds and 4 git commands to generate.