1 // $Id: particle.cxx,v 1.20 2003/07/24 10:10:02 grumbel Exp $
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
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.
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
)
31 velocity (arg_velocity
),
39 Particle::Particle (const Particle
& p
)
42 velocity (p
.velocity
),
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 ();
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)),
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
));
86 Particle::draw (ZoomGraphicContext
* gc
)
92 gc
->get_parent_gc()->draw_fill_circle (gc
->world_to_screen(pos
),
94 Color(0.6f
, 0.6f
, 0.6f
));
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
));
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
));
114 Particle::update (float delta
)
116 const float max_velocity
= 1000.0f
;
120 velocity
+= totale_force
* delta
* (1.0f
/mass
);
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
135 velocity
.x
= fabs(velocity
.x
);
137 velocity
*= collision_damp
;
138 } else if (pos
.x
> 799) {
139 velocity
.x
= -fabs(velocity
.x
);
141 velocity
*= collision_damp
;
144 // Calc collision with screen y border
146 velocity
.y
= fabs(velocity
.y
);
148 velocity
*= collision_damp
;
153 velocity
.y
= -fabs(velocity
.y
);
155 velocity
*= collision_damp
;
159 Vector2d dist = pos - Vector2d (400, 300);
160 if (dist.norm () < 50.0f)
162 velocity = -velocity;
166 // Avoid to fast things
167 if (velocity
.norm () > max_velocity
)
169 velocity
.normalize();
170 velocity
*= max_velocity
;