coeng

view src/geom.h @ 7:af24cfbdf9b6

adding collision detection
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Feb 2015 10:08:00 +0200
parents
children 8cce82794f90
line source
1 #ifndef GEOM_H_
2 #define GEOM_H_
4 #include <vmath/vmath.h>
6 class GeomShape;
8 struct HitPoint {
9 float dist;
10 Vector3 pos;
11 Vector3 normal;
12 const GeomShape *shape;
13 void *data;
14 };
16 class GeomShape {
17 protected:
18 enum Type { GEOM_UNKNOWN, GEOM_SPHERE, GEOM_PLANE } type;
20 public:
21 GeomShape();
22 virtual ~GeomShape();
24 virtual Type get_type() const;
26 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
27 virtual bool collide(const GeomShape *geom, HitPoint *hit = 0) const = 0;
29 virtual float distance(const Vector3 &pt) const = 0;
30 virtual float distance_sq(const Vector3 &pt) const = 0;
31 };
33 class Sphere : public GeomShape {
34 public:
35 Vector3 center;
36 float radius;
38 Sphere();
39 Sphere(const Vector3 &c, float rad);
41 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
42 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
44 float distance(const Vector3 &pt) const;
45 float distance_sq(const Vector3 &pt) const;
46 };
48 class Plane : public GeomShape {
49 public:
50 Vector3 normal;
51 float dist;
53 Plane();
54 Plane(const Vector3 &n, float d);
55 Plane(float a, float b, float c, float d);
56 Plane(const Vector3 &pos, const Vector3 &norm);
57 Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3);
59 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
60 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
62 float distance(const Vector3 &pt) const;
63 float distance_sq(const Vector3 &pt) const;
64 };
66 #endif // GEOM_H_