1 /*
2   Copyright (C) 2009 Facundo Domínguez
3 
4   This file is part of Spacejunk.
5 
6   Spacejunk is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   Foobar 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.
15 
16   You should have received a copy of the GNU General Public License
17   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef CMASK_H
21 #define CMASK_H
22 
23 #include "resourcemanager.h"
24 #include "parsercombinators.h"
25 #include <list>
26 #include "outline.h"
27 
28 /**
29  * Implementation of a set of non-overlapped convex polygons which define
30  * the outline of an object.
31  *
32  * Each polygon has a minimum enclosing disk which is used to speed the
33  * collision checks.
34  * */
35 class CMask : public std::list<std::pair<OUTLINE_Polygon*,Circle> > {
36 };
37 
38 /** Reads an object mask from a Lexer. */
39 Lexer & CMASK_parse(Lexer & p,CMask & m);
40 
41 /** Frees the memory used by a cmask. */
42 void CMASK_free(CMask * c);
43 
44 /**
45  * Detects collision of two circles which are moving.
46  *
47  * @param r1 is the radius of a fixed circle at the origin.
48  * @param r2 is the radius of the second circle.
49  * @param pos is the position of the second circle.
50  * @param vel is the translation performed by the second circle.
51  * @param t is the fraction of vel at which collision occurred (between zero and one).
52  * @param normal is the normal in the contact point if ther was a collision.
53  * @return true iff there was a collision.
54  */
55 bool circle_swept_collision(real r1,real r2,const Vector2d & pos,const Vector2d & vel,real * t,Vector2d * normal);
56 
57 /**
58  * Detects a collision between two cmasks m1 and m2.
59  * @param m is the transformation of m2 coordinates to m1 coordinates.
60  * @param vel is the translation vector in m1 coordinates.
61  * @return true iff there was a collision.
62  * */
63 bool CMASK_intersect(const CMask * m1,const Matrix2x3 & m,real angleshift,const CMask * m2,const Vector2d & vel,real * t=NULL);
64 
65 /**
66  * Detects collision of a cmask with a circle which moves from its current location as
67  * indicated by the tarnslation vector vel.
68  */
69 bool CMASK_intersect(const CMask * m,const Circle & c,const Vector2d & vel,real * t);
70 
71 
72 /** Resource loader for cmasks. */
73 class CMaskLoader : public ResourceLoader<CMask> {
74 private:
75     std::map<std::string,std::string> mask_paths;
76 public:
77     CMask * create(const std::string & id);
78     Parser & parse(Parser & p,std::string * id);
free(CMask * p)79     void free(CMask *p) {
80         CMASK_free(p);
81     };
82 };
83 
84 typedef Resource<CMaskLoader> RMCMASKRef;
85 
86 
87 #endif // CMASK_H
88