1 // $Id: rect_collider.cxx,v 1.10 2003/07/26 11:18:47 grumbel Exp $
3 // Construo - A wire-frame construction gamee
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 "particle_factory.hxx"
23 #include "controller.hxx"
24 #include "rect_collider.hxx"
25 #include "construo_error.hxx"
28 RectCollider::duplicate() const
30 return new RectCollider(x1
, y1
, x2
, y2
);
33 RectCollider::RectCollider (lisp_object_t
* cursor
)
37 LispReader
reader(cursor
);
38 if (reader
.read_vector("pos1", &pos1
) == false
39 || reader
.read_vector("pos2", &pos2
) == false)
41 throw ConstruoError("RectCollider entry incomplete");
50 RectCollider::RectCollider (float x1_
, float y1_
, float x2_
, float y2_
)
51 : x1 (Math::min(x1_
, x2_
)),
52 y1 (Math::min(y1_
, y2_
)),
53 x2 (Math::max(x1_
, x2_
)),
54 y2 (Math::max(y1_
, y2_
))
59 RectCollider::is_at (const Vector2d
& pos
)
61 return (x1
<= pos
.x
&& x2
> pos
.x
62 && y1
<= pos
.y
&& y2
> pos
.y
);
66 RectCollider::get_pos()
68 return Vector2d ((x1
+ x2
)/2.0f
,
73 RectCollider::set_pos(const Vector2d
& pos
)
75 Vector2d center
= get_pos();
76 x1
= x1
- center
.x
+ pos
.x
;
77 x2
= x2
- center
.x
+ pos
.x
;
78 y1
= y1
- center
.y
+ pos
.y
;
79 y2
= y2
- center
.y
+ pos
.y
;
83 RectCollider::bounce ()
85 ParticleFactory
* particle_mgr
= Controller::instance()->get_world()->get_particle_mgr();
88 for (ParticleFactory::ParticleIter i
= particle_mgr
->begin(); i
!= particle_mgr
->end (); ++i
)
90 Vector2d
& pos
= (*i
)->pos
;
91 Vector2d
& velocity
= (*i
)->velocity
;
93 if (pos
.x
> x1
&& pos
.x
< x2
94 && pos
.y
> y1
&& pos
.y
< y2
)
96 float left_dist
= pos
.x
- x1
;
97 float right_dist
= x2
- pos
.x
;
99 float top_dist
= pos
.y
- y1
;
100 float bottom_dist
= y2
- pos
.y
;
102 if (left_dist
< right_dist
103 && left_dist
< top_dist
104 && left_dist
< bottom_dist
)
106 velocity
.x
= -fabs(velocity
.x
);
109 else if (right_dist
< left_dist
110 && right_dist
< top_dist
111 && right_dist
< bottom_dist
)
113 velocity
.x
= fabs(velocity
.x
);
116 else if (top_dist
< left_dist
117 && top_dist
< right_dist
118 && top_dist
< bottom_dist
)
120 velocity
.y
= -fabs(velocity
.y
);
125 velocity
.y
= fabs(velocity
.y
);
134 RectCollider::draw (GraphicContext
* gc
)
136 //std::cout << "Drawing collider" << std::endl;
137 gc
->draw_fill_rect (x1
, y1
, x2
, y2
, Colors::rect_collider_bg
);
138 gc
->draw_rect (x1
, y1
, x2
, y2
, Colors::rect_collider_fg
);
142 RectCollider::draw_highlight (GraphicContext
* gc
)
144 //gc->draw_fill_rect (x1, y1, x2, y2, Colors::rect_collider_bg);
145 gc
->draw_rect (x1
, y1
, x2
, y2
, Colors::selection_rect
);
149 RectCollider::get_bounding_box() const
151 return BoundingBox(x1
, y1
, x2
, y2
);
155 RectCollider::serialize()
157 LispWriter
obj ("rect");
158 obj
.write_vector ("pos1", Vector2d(x1
, y1
));
159 obj
.write_vector ("pos2", Vector2d(x2
, y2
));
160 return obj
.create_lisp ();