Salut,
je possède une classe de base abstraite (BoundingVolume) dont dérive plusieurs classes. (BoundingBox, BoundingSphere, etc...)
Je possède également une classe Entity et elle possède un pointeur sur un objet de type BoundingVolume.
Le problème c'est que je n'arrive pas à trouver comment faire une méthode intersects générique, je voudrais que la méthode intersects sache quel type de BoundingVolume est ce et qu'elle fasse un cast vers l'object de la sous classe avant d'appeler la méthode intersects correspondante pour la sous classe, j'ai donc fait ceci :
Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 #ifndef ODFAEG_BOUNDING_VOLUME_HPP #define ODFAEG_BOUNDING_VOLUME_HPP class TransformMatrix; /** *\namespace odfaeg * the namespace of the Opensource Development Framework Adapted for Every Games. */ namespace odfaeg { /** * \file boundingVolume.h * \class BoudingVolume * \brief Manage a bounding volume for collision detection * \author Duroisin.L * \version 1.0 * \date 1/02/2014 * * Base class of all bouding volumes of the framework used for collision detection. * */ class BoundingBox; template <typename D> class BoundingVolume { public : /** \fn bool intersects (BoundingAreas &other) * \brief this method can be redefined in the sub class to test if two bounding volumes * are in collision. (if the method isn't redefined it always return false. * We cannot made this methode abstract because, we need to overload the intersects method in subclasses * for each volumes types. * \param the other bounding volume to test with. * \return return true if the two bounding volumes are in collision. */ bool intersects(BoundingVolume *other) { //std::cout<<"intersects"<<std::endl; std::cout<<static_cast<D*>(other)<<std::endl; return intersects(*static_cast<D*>(other)); } virtual bool intersects (BoundingBox &other) { return false; } virtual Vec3f getPosition() { return Vec3f(0, 0, 0); } virtual Vec3f getSize() { return Vec3f(0, 0, 0); } virtual Vec3f getCenter() { return Vec3f(0, 0, 0); } virtual void move (Vec3f t) { } protected : /** \fn BoundingVolume () * \brief constructor : we cannot create bounding volumes directly, we need to use one of its sub classes. */ BoundingVolume() {} }; } #endif // BOUNDING_AREAS
D correspond au type de la sous classe. (BoundingBox, BoundingSphere, etc...)
Je fais donc un cast sur le paramètre de la fonction et je rappelle la méthode intersects en lui envoyant le bon type pour le paramètre.
Le problème est que le cast ne marche pas et me renvoie un pointeur null :
Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 entity->getCollisionVolume()->intersects(entity2->getCollisionVolume())
La classe Entity possède un object de type BoundingVolume<BoundingBox>.
Du coup ça crash dans la méthode intersects de ma classe BoundingBox, donc voilà je cherche une solution à mon problème car là je ne vois pas de trop comment faire ça.
Ensuite je voudrais former une hiérarchie de tel sorte que la classe BoundingVolume contienne des volumes enfants de n'importe quel type et appeler la méthode intersects récursivement mais je vois pas trop bien comment faire cela en c++.
Partager