]> Dogcows Code - chaz/yoink/blob - src/Character.cc
a29e63d792fd50d17935f6dc1d3bb82067cc2903
[chaz/yoink] / src / Character.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
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.
15
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.
26
27 *******************************************************************************/
28
29 #include <iostream>
30
31 #include "Character.hh"
32 #include "Log.hh"
33
34
35 struct SpringForce
36 {
37 explicit SpringForce(Mf::Vector2 x) :
38 location(x) {}
39
40 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
41 {
42 Mf::Vector2 x = state.position - location;
43 Mf::Scalar mag = x.length();
44 Mf::Scalar d = 50.0;
45
46 // spring:
47 //current.force += -15.0 * x - 1.5 * current.velocity;
48 force = -20.0 * (mag - d) * (x / mag) - 2.0 * state.velocity;
49
50 return force;
51 }
52
53 private:
54
55 Mf::Vector2 force;
56 Mf::Vector2 location;
57 };
58
59 struct ResistanceForce
60 {
61 explicit ResistanceForce(Mf::Scalar scale = 1.0) :
62 k(scale) {}
63
64 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
65 {
66 force = -k * state.velocity;
67 return force;
68 }
69
70 private:
71
72 Mf::Vector2 force;
73 Mf::Scalar k;
74 };
75
76
77 Character::Character(const std::string& name) :
78 tilemap(name),
79 animation(name)
80 {
81 current.init();
82
83 current.mass = 1.0;
84 current.inverseMass = 1.0 / current.mass;
85
86 // gravity
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));
91
92 // starting position
93 current.position = Mf::Vector2(64.0, 64.0);
94 current.momentum = Mf::Vector2(0.0, 0.0);
95 current.recalculate();
96
97 previous = current;
98 }
99
100
101 void Character::update(Mf::Scalar t, Mf::Scalar dt)
102 {
103 previous = current;
104
105 //Mf::Vector2 x = current.position - Mf::Vector2(500.0, 200.0);
106 //Mf::Scalar mag = x.length();
107 //Mf::Scalar d = 50.0;
108
109 //// gravity:
110 //current.force = Mf::Vector2(0.0, -2000.0);
111 //// spring:
112 ////current.force += -15.0 * x - 1.5 * current.velocity;
113 //current.force += -20.0 * (mag - d) * (x / mag) - 2.0 * current.velocity;
114 //// internal:
115 //current.force += userForce;
116 //current.recalculate();
117 //std::cout << "force: " << current.momentum << std::endl;
118
119 //Mf::euler<State,Derivative>(current, t, dt);
120
121 current.integrate(t, dt);
122 animation.update(t, dt);
123 }
124
125
126 void Character::draw(Mf::Scalar alpha) const
127 {
128 Mf::Vector2 position = cml::lerp(previous.position, current.position, alpha);
129
130 //glColor3f(1.0f, 1.0f, 1.0f);
131 tilemap.bind();
132
133 Tilemap::Index frame = animation.getFrame();
134
135 Tilemap::Orientation orientation = Tilemap::NORMAL;
136
137 if (current.velocity[0] < 0.0) orientation = Tilemap::REVERSE;
138
139 Mf::Scalar coords[8];
140 tilemap.getTileCoords(frame, coords, orientation);
141
142 Mf::Scalar s = 16.0;
143
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);
153 glEnd();
154
155 //glColor3f(0.0f, 0.0f, 0.0f);
156 Mf::Texture::resetBind();
157
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);
162 glEnd();
163
164 //glColor3f(1.0f, 1.0f, 1.0f);
165 }
166
167
168 /** vim: set ts=4 sw=4 tw=80: *************************************************/
169
This page took 0.0351 seconds and 4 git commands to generate.