]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
12 #include <moof/log.hh>
14 #include "Character.hh"
21 explicit SpringForce(moof::vector2 x
) :
24 const moof::vector2
& operator () (const moof::linear_state
<2>& state
)
26 moof::vector2 x
= state
.position
- location
;
27 moof::scalar mag
= x
.length();
31 //state_.force += -15.0 * x - 1.5 * state_.velocity;
32 force
= SCALAR(-10.0) * (mag
- d
) * (x
/ mag
);// - SCALAR(2.0) * state.velocity;
40 moof::vector2 location
;
48 explicit ResistanceForce(moof::scalar scale
= 1.0) :
51 const moof::vector2
& operator () (const moof::linear_state
<2>& state
)
53 force
= -k
* state
.velocity
;
64 Character::Character(const std::string
& name
) :
71 state_
.inverse_mass
= 1.0 / state_
.mass
;
74 state_
.force
= moof::vector2(0.0, 0.0);
75 //state_.forces.push_back(SpringForce(moof::vector2(5.0, 4.0)));
76 //state_.forces.push_back(ResistanceForce(2.0));
77 //state_.forces.push_back(moof::linear_state<2>::gravity_force(-9.8));
80 state_
.position
= moof::vector2(5.0, 5.0);
81 state_
.momentum
= moof::vector2(3.0, 0.0);
87 void Character::update(moof::scalar t
, moof::scalar dt
)
89 moof::rigid_body2::update(t
, dt
); // update physics
91 animation
.update(t
, dt
);
93 moof::vector3
center(state_
.position
[0], state_
.position
[1], 0.0);
94 moof::vector3
a(state_
.position
[0] - 0.5, state_
.position
[1] - 0.5, 0.0);
95 moof::vector3
b(state_
.position
[0] + 0.5, state_
.position
[1] + 0.5, 0.0);
98 sphere_
.init(center
, a
);
100 int frame
= animation
.getFrame();
104 void Character::draw(moof::scalar alpha
) const
106 moof::state2 state
= moof::rigid_body2::state(alpha
);
107 const moof::vector2
& position
= state
.position
;
109 const moof::scalar s
= 0.5;
111 moof::vector3 coords
[4];
113 coords
[0] = moof::vector3(position
[0] - s
, position
[1] - s
, SCALAR(0.0));
114 coords
[1] = moof::vector3(position
[0] + s
, position
[1] - s
, SCALAR(0.0));
115 coords
[2] = moof::vector3(position
[0] + s
, position
[1] + s
, SCALAR(0.0));
116 coords
[3] = moof::vector3(position
[0] - s
, position
[1] + s
, SCALAR(0.0));
118 tilemap
.draw(coords
);
121 /*int Character::getOctant(const moof::Aabb<3>& aabb) const
125 moof::plane::halfspace halfspace;
127 moof::plane xy = aabb.xy_plane();
128 halfspace = xy.intersects(sphere_);
129 if (halfspace == moof::plane::intersecting)
131 halfspace = xy.intersects(aabb_);
134 if (halfspace == moof::plane::positive)
136 moof::plane xz = aabb.xz_plane();
137 halfspace = xz.intersects(sphere_);
138 if (halfspace == moof::plane::intersecting)
140 halfspace = xz.intersects(aabb_);
143 if (halfspace == moof::plane::positive)
145 moof::plane yz = aabb.yz_plane();
146 halfspace = yz.intersects(sphere_);
147 if (halfspace == moof::plane::intersecting)
149 halfspace = yz.intersects(aabb_);
152 if (halfspace == moof::plane::positive)
156 else if (halfspace == moof::plane::negative)
161 else if (halfspace == moof::plane::negative)
163 moof::plane yz = aabb.yz_plane();
164 halfspace = yz.intersects(sphere_);
165 if (halfspace == moof::plane::intersecting)
167 halfspace = yz.intersects(aabb_);
170 if (halfspace == moof::plane::positive)
174 else if (halfspace == moof::plane::negative)
180 else if (halfspace == moof::plane::negative)
182 moof::plane xz = aabb.xz_plane();
183 halfspace = xz.intersects(sphere_);
184 if (halfspace == moof::plane::intersecting)
186 halfspace = xz.intersects(aabb_);
189 if (halfspace == moof::plane::positive)
191 moof::plane yz = aabb.yz_plane();
192 halfspace = yz.intersects(sphere_);
193 if (halfspace == moof::plane::intersecting)
195 halfspace = yz.intersects(aabb_);
198 if (halfspace == moof::plane::positive)
202 else if (halfspace == moof::plane::negative)
207 else if (halfspace == moof::plane::negative)
209 moof::plane yz = aabb.yz_plane();
210 halfspace = yz.intersects(sphere_);
211 if (halfspace == moof::plane::intersecting)
213 halfspace = yz.intersects(aabb_);
216 if (halfspace == moof::plane::positive)
220 else if (halfspace == moof::plane::negative)
231 void Character::addImpulse(moof::vector2 impulse
)
233 state_
.momentum
+= impulse
;
236 void Character::addForce(moof::vector2 force
)
238 state_
.force
+= force
;
241 void Character::setPosition(moof::vector2 position
)
243 state_
.position
= position
;
This page took 0.041235 seconds and 4 git commands to generate.