Rebuild autotool system
[construo.git] / particle.cxx
blobce8683731b163a54609e05609063f783f35d0bcb
1 // $Id: particle.cxx,v 1.20 2003/07/24 10:10:02 grumbel Exp $
2 //
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "colors.hxx"
21 #include "math.hxx"
22 #include "lisp_reader.hxx"
23 #include "string_utils.hxx"
24 #include "particle.hxx"
26 using namespace StringUtils;
28 Particle::Particle (int i, const Vector2d& arg_pos, const Vector2d& arg_velocity, float m, bool f)
29 : id (i),
30 pos (arg_pos),
31 velocity (arg_velocity),
32 mass (m),
33 fixed (f),
34 totale_force (0,0),
35 spring_links (0)
39 Particle::Particle (const Particle& p)
40 : id (p.id),
41 pos (p.pos),
42 velocity (p.velocity),
43 mass (p.mass),
44 fixed (p.fixed),
45 totale_force (0,0),
46 spring_links (0)
50 lisp_object_t*
51 Particle::serialize()
53 LispWriter obj ("particle");
54 obj.write_int ("id", id);
55 obj.write_vector ("pos", pos);
56 obj.write_vector ("velocity", velocity);
57 obj.write_boolean ("fixed", fixed);
58 obj.write_float ("mass", mass);
59 return obj.create_lisp ();
62 void
63 Particle::draw_highlight (ZoomGraphicContext* gc)
65 gc->get_parent_gc()->draw_fill_circle (gc->world_to_screen(pos),
66 Math::round(Math::max(6.0f, get_mass() + 3)),
67 Colors::highlight);
70 void
71 Particle::draw_infos (ZoomGraphicContext* gc)
73 Vector2d p = gc->world_to_screen(pos);
74 draw_velocity_vector (gc);
75 gc->get_parent_gc()->draw_string (p + Vector2d(20.0f, 5.0f),
76 "Particle: " + to_string (pos));
77 gc->get_parent_gc()->draw_string (p + Vector2d(20.0f, 25.0f),
78 "Fixed: " + to_string (fixed));
79 gc->get_parent_gc()->draw_string (p + Vector2d(20.0f, 45.0f),
80 "Mass : " + to_string (get_mass()));
81 gc->get_parent_gc()->draw_string (p + Vector2d(20.0f, 70.0f),
82 "Links : " + to_string (spring_links));
85 void
86 Particle::draw (ZoomGraphicContext* gc)
88 if (pos.y < 598.5f)
90 if (fixed)
92 gc->get_parent_gc()->draw_fill_circle (gc->world_to_screen(pos),
94 Color(0.6f, 0.6f, 0.6f));
96 else
98 gc->get_parent_gc()->draw_fill_circle (gc->world_to_screen(pos),
99 Math::round(Math::max(3.0f, get_mass())),
100 Color(1.0f, 0.0f, 0.0f));
105 void
106 Particle::draw_velocity_vector (ZoomGraphicContext* gc)
108 gc->draw_line (int (pos.x), int (pos.y),
109 int (pos.x + velocity.x), int (pos.y + velocity.y),
110 Color (0.0f, 0.0f, 1.0f));
113 void
114 Particle::update (float delta)
116 const float max_velocity = 1000.0f;
118 if (fixed) return;
120 velocity += totale_force * delta * (1.0f/mass);
122 // damping
123 if (0)
124 velocity -= (velocity * (1.0f/mass) * delta) * 0.001f;
126 //velocity *= .999999f ;
128 pos += velocity * delta;
130 float collision_damp = 0.2;
132 #if 0 // FIXME: Replace this with a generic shape collision handling thing
133 // Calc collision with screen x border
134 if (pos.x < 0) {
135 velocity.x = fabs(velocity.x);
136 pos.x = 0;
137 velocity *= collision_damp;
138 } else if (pos.x > 799) {
139 velocity.x = -fabs(velocity.x);
140 pos.x = 799;
141 velocity *= collision_damp;
144 // Calc collision with screen y border
145 if (pos.y < 0) {
146 velocity.y = fabs(velocity.y);
147 pos.y = 0;
148 velocity *= collision_damp;
149 } else
150 #endif
152 if (pos.y > 599) {
153 velocity.y = -fabs(velocity.y);
154 pos.y = 599;
155 velocity *= collision_damp;
159 Vector2d dist = pos - Vector2d (400, 300);
160 if (dist.norm () < 50.0f)
162 velocity = -velocity;
164 clear_force ();
166 // Avoid to fast things
167 if (velocity.norm () > max_velocity)
169 velocity.normalize();
170 velocity *= max_velocity;
175 /* EOF */