coeng

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/geom.h	Sat Feb 14 10:08:00 2015 +0200
     1.3 @@ -0,0 +1,66 @@
     1.4 +#ifndef GEOM_H_
     1.5 +#define GEOM_H_
     1.6 +
     1.7 +#include <vmath/vmath.h>
     1.8 +
     1.9 +class GeomShape;
    1.10 +
    1.11 +struct HitPoint {
    1.12 +	float dist;
    1.13 +	Vector3 pos;
    1.14 +	Vector3 normal;
    1.15 +	const GeomShape *shape;
    1.16 +	void *data;
    1.17 +};
    1.18 +
    1.19 +class GeomShape {
    1.20 +protected:
    1.21 +	enum Type { GEOM_UNKNOWN, GEOM_SPHERE, GEOM_PLANE } type;
    1.22 +
    1.23 +public:
    1.24 +	GeomShape();
    1.25 +	virtual ~GeomShape();
    1.26 +
    1.27 +	virtual Type get_type() const;
    1.28 +
    1.29 +	virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
    1.30 +	virtual bool collide(const GeomShape *geom, HitPoint *hit = 0) const = 0;
    1.31 +
    1.32 +	virtual float distance(const Vector3 &pt) const = 0;
    1.33 +	virtual float distance_sq(const Vector3 &pt) const = 0;
    1.34 +};
    1.35 +
    1.36 +class Sphere : public GeomShape {
    1.37 +public:
    1.38 +	Vector3 center;
    1.39 +	float radius;
    1.40 +
    1.41 +	Sphere();
    1.42 +	Sphere(const Vector3 &c, float rad);
    1.43 +
    1.44 +	bool intersect(const Ray &ray, HitPoint *hit = 0) const;
    1.45 +	bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
    1.46 +
    1.47 +	float distance(const Vector3 &pt) const;
    1.48 +	float distance_sq(const Vector3 &pt) const;
    1.49 +};
    1.50 +
    1.51 +class Plane : public GeomShape {
    1.52 +public:
    1.53 +	Vector3 normal;
    1.54 +	float dist;
    1.55 +
    1.56 +	Plane();
    1.57 +	Plane(const Vector3 &n, float d);
    1.58 +	Plane(float a, float b, float c, float d);
    1.59 +	Plane(const Vector3 &pos, const Vector3 &norm);
    1.60 +	Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3);
    1.61 +
    1.62 +	bool intersect(const Ray &ray, HitPoint *hit = 0) const;
    1.63 +	bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
    1.64 +
    1.65 +	float distance(const Vector3 &pt) const;
    1.66 +	float distance_sq(const Vector3 &pt) const;
    1.67 +};
    1.68 +
    1.69 +#endif	// GEOM_H_