]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
a29e63d792fd50d17935f6dc1d3bb82067cc2903
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 ResistanceForce
61 explicit ResistanceForce(Mf::Scalar scale
= 1.0) :
64 const Mf::Vector2
& operator () (const Mf::LinearState
<2>& state
)
66 force
= -k
* state
.velocity
;
77 Character::Character(const std::string
& name
) :
84 current
.inverseMass
= 1.0 / current
.mass
;
87 current
.force
= Mf::Vector2(0.0, 000.0);
88 current
.forces
.push_back(SpringForce(Mf::Vector2(500.0, 200.0)));
89 current
.forces
.push_back(ResistanceForce(4.0));
90 current
.forces
.push_back(Mf::LinearState
<2>::GravityForce(-2000.0));
93 current
.position
= Mf::Vector2(64.0, 64.0);
94 current
.momentum
= Mf::Vector2(0.0, 0.0);
95 current
.recalculate();
101 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
105 //Mf::Vector2 x = current.position - Mf::Vector2(500.0, 200.0);
106 //Mf::Scalar mag = x.length();
107 //Mf::Scalar d = 50.0;
110 //current.force = Mf::Vector2(0.0, -2000.0);
112 ////current.force += -15.0 * x - 1.5 * current.velocity;
113 //current.force += -20.0 * (mag - d) * (x / mag) - 2.0 * current.velocity;
115 //current.force += userForce;
116 //current.recalculate();
117 //std::cout << "force: " << current.momentum << std::endl;
119 //Mf::euler<State,Derivative>(current, t, dt);
121 current
.integrate(t
, dt
);
122 animation
.update(t
, dt
);
126 void Character::draw(Mf::Scalar alpha
) const
128 Mf::Vector2 position
= cml::lerp(previous
.position
, current
.position
, alpha
);
130 //glColor3f(1.0f, 1.0f, 1.0f);
133 Tilemap::Index frame
= animation
.getFrame();
135 Tilemap::Orientation orientation
= Tilemap::NORMAL
;
137 if (current
.velocity
[0] < 0.0) orientation
= Tilemap::REVERSE
;
139 Mf::Scalar coords
[8];
140 tilemap
.getTileCoords(frame
, coords
, orientation
);
144 glBegin(GL_TRIANGLE_FAN
);
145 glTexCoord2f(coords
[0], coords
[1]);
146 glVertex3(position
[0]-s
, position
[1]-s
, z
);
147 glTexCoord2f(coords
[2], coords
[3]);
148 glVertex3(position
[0]+s
, position
[1]-s
, z
);
149 glTexCoord2f(coords
[4], coords
[5]);
150 glVertex3(position
[0]+s
, position
[1]+s
, z
);
151 glTexCoord2f(coords
[6], coords
[7]);
152 glVertex3(position
[0]-s
, position
[1]+s
, z
);
155 //glColor3f(0.0f, 0.0f, 0.0f);
156 Mf::Texture::resetBind();
158 glBegin(GL_TRIANGLES
);
159 glVertex3(480.0, 190.0, 64.0);
160 glVertex3(520.0, 190.0, 64.0);
161 glVertex3(500.0, 210.0, 64.0);
164 //glColor3f(1.0f, 1.0f, 1.0f);
168 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.038399 seconds and 4 git commands to generate.