]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
ffecfd5b8c5db2c2c82c3e4cc478c00d718f3fed
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
31 #include "Character.hh"
37 explicit SpringForce(Mf::Vector2 x
) :
40 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
42 Mf::Vector2 x
= state
.position
- location
;
43 Mf::Scalar mag
= x
.length();
47 //current.force += -15.0 * x - 1.5 * current.velocity;
48 force
= -20.0 * (mag
- d
) * (x
/ mag
) - 2.0 * state
.velocity
;
59 struct WindResistenceForce
61 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
63 force
= -2.0 * state
.velocity
;
73 Character::Character(const std::string
& name
) :
80 current
.inverseMass
= 1.0 / current
.mass
;
83 current
.force
= Mf::Vector2(0.0, 000.0);
84 current
.forces
.push_back(SpringForce(Mf::Vector2(500.0, 200.0)));
85 current
.forces
.push_back(WindResistenceForce());
86 current
.forces
.push_back(Mf::LinearState
<2>::GravityForce(-2000.0));
89 current
.position
= Mf::Vector2(64.0, 64.0);
90 current
.momentum
= Mf::Vector2(0.0, 0.0);
91 current
.recalculate();
97 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
101 //Mf::Vector2 x = current.position - Mf::Vector2(500.0, 200.0);
102 //Mf::Scalar mag = x.length();
103 //Mf::Scalar d = 50.0;
106 //current.force = Mf::Vector2(0.0, -2000.0);
108 ////current.force += -15.0 * x - 1.5 * current.velocity;
109 //current.force += -20.0 * (mag - d) * (x / mag) - 2.0 * current.velocity;
111 //current.force += userForce;
112 //current.recalculate();
113 //std::cout << "force: " << current.momentum << std::endl;
115 //Mf::euler<State,Derivative>(current, t, dt);
117 //current.force = Mf::Vector2(0.0, -2000.0);
118 current
.force
= userForce
;
119 current
.integrate(t
, dt
);
121 animation_
.update(t
, dt
);
125 void Character::draw(Mf::Scalar alpha
) const
127 Mf::Vector2 position
= cml::lerp(previous
.position
, current
.position
, alpha
);
129 //glColor3f(1.0f, 1.0f, 1.0f);
132 Mf::Tilemap::Index frame
= animation_
.getFrame();
134 Mf::Tilemap::Orientation orientation
= Mf::Tilemap::NORMAL
;
136 if (current
.velocity
[0] < 0.0) orientation
= Mf::Tilemap::REVERSE
;
138 Mf::Scalar coords
[8];
139 tilemap_
.getTileCoords(frame
, coords
, orientation
);
143 glBegin(GL_TRIANGLE_FAN
);
144 glTexCoord2f(coords
[0], coords
[1]);
145 glVertex3(position
[0]-s
, position
[1]-s
, z
);
146 glTexCoord2f(coords
[2], coords
[3]);
147 glVertex3(position
[0]+s
, position
[1]-s
, z
);
148 glTexCoord2f(coords
[4], coords
[5]);
149 glVertex3(position
[0]+s
, position
[1]+s
, z
);
150 glTexCoord2f(coords
[6], coords
[7]);
151 glVertex3(position
[0]-s
, position
[1]+s
, z
);
154 //glColor3f(0.0f, 0.0f, 0.0f);
155 Mf::Texture::resetBind();
157 glBegin(GL_TRIANGLES
);
158 glVertex3(480.0, 190.0, 64.0);
159 glVertex3(520.0, 190.0, 64.0);
160 glVertex3(500.0, 210.0, 64.0);
163 //glColor3f(1.0f, 1.0f, 1.0f);
167 Mf::Tilemap
& Character::getTilemap()
172 Mf::Animation
& Character::getAnimation()
178 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.046326 seconds and 4 git commands to generate.