Irrlicht学习之向量变化部分
2015-06-11 21:21
169 查看
有2D向量和3D向量。
2D向量涉及到旋转,点乘,叉乘,归一化操作。
如下:
3D向量涉及到点乘,差乘,求距离,归一化,翻转,旋转(分别以X,Z轴和Y轴为对称)。如下:
分析待完善。
2D向量涉及到旋转,点乘,叉乘,归一化操作。
如下:
#ifndef __IRR_POINT_2D_H_INCLUDED__ #define __IRR_POINT_2D_H_INCLUDED__ #include "irrTypes.h" namespace irr { namespace core { template <typename T> inline void rotateBy(f64 degrees, T& x, T& y, T centerx, T centery) { degrees *=GRAD_PI2; T cs = (T)cos(degrees); T sn = (T)sin(degrees); x -= centerx; y -= centery; } } // end namespace core } // end namespace irr #endif
3D向量涉及到点乘,差乘,求距离,归一化,翻转,旋转(分别以X,Z轴和Y轴为对称)。如下:
/**************************** * 2015年5月24 星期日 零点 *(周六一天写了仿真的代码,晚上闲余来敲一敲IRRLICHT引擎的代码,不知不觉已经到了周日凌晨了。) *(要学习Irrlicht的编码方式和各种编程方法。以提高自己用C++编码以及思考的能力。) ***************************/ #ifndef _IRR_POINT_3D_H_INCLUDE_ #define _IRR_POINT_3D_H_INCLUDE_ #include <math.h> #include "irrTypes.h" namespace irr { namespace core { template<typename T> class vector3d { public: vector3d():X(0), Y(0), Z(0){}; vector3d( T nx, T ny, T nz) : X(nx), Y(ny), Z(nz){}; vector3d(const vector3d<T>& other ) :X(other.X), Y(other.Y), Z(other.Z){}; // 操作符 a + b + c a +=b vector3d<T>& operator=(const vector3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; } vector3d<T> operator+(const vector3d<T>& other) const { return vector3d<T>(X + other.X, Y + other.Y, Z + other.Z); } vector3d<T>& operator+=(const vector3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; } vector3d<T> operator-(const vector3d<T>& other) const { return vector3d<T>(X - other.X, Y - other.Y, Z - other.Z); } vector3d<T>& operator-=(const vector3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; } vector3d<T> operator*(const vector3d<T>& other) const { return vector3d<T>(X*other.X, Y*other.Y, Z*other.Z); } vector3d<T>& operator*=(const vector3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; } vector3d<T> operator*(const T v) const { return vector3d<T>(X*v, Y*v, Z*v); } vector3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; } vector3d<T> operator/(const vector3d<T>& other) const { return vector3d<T>(X/other.X, Y/other.Y, Z/other.Z); } vector3d<T>& operator/=(const vector3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; } vector3d<T> operator/(const T v) const { T i=(T)1.0/v; return vector3d<T>(X*i, Y*i, Z*i); } vector3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; } bool operator<=(const vector3d<T>& other) const { return X<=other.X && Y<=other.Y && Z<=other.Z; }; bool operator>=(const vector3d<T>& other) const { return X>=other.X && Y>=other.Y && z>=other.Z; }; bool operator==(const vector3d<T>& other) const { return other.X==X && other.Y==Y && other.Z==Z; } bool operator!=(const vector3d<T>& other) const { return other.X!=X || other.Y!=Y && other.Z!=Z; } // 方法 void set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; } void set(const vector3d<T>& p) { X=p.X; Y=p.Y; Z=p.Z; } // 返回向量的长度值 f64 getLength() const { return sqrt(X*X + Y*Y + Z*Z); } // 返回两个向量的点乘值 T dotProduct( const vector3d<T>& other) const { return X*other.X + Y*other.Y + Z*other.Z; } // 返回点和点之间的距离。此处向量被用作为三位坐标空间中的一个点。 f64 getDistanceFrom(const vector3d<T>& other) const { f64 vx = X - other.X; f64 vy = Y - other.Y; f64 vz = Z - other.Z; return sqrt(vx*vx + vy*vy + vz*vz); } vector3d<T> corssProduct(const vector3d<T>& p) const { return vector3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X ); } // 未完 // 成员变量 T X, Y, Z; }; //! Typedef for a f32 3d vector typedef vector3d<f32> vector3df; //! Typedef for an integer 3d vector typedef vector3d<s32> vector3di; } // end namespace irr } // end namespace core #endif
分析待完善。
相关文章推荐
- .net Url.Encode 跳转到PHP解析问题
- AD用户操作
- [安卓]手机管家(三)homeActivity
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
- 管道
- 42.Trapping Rain Water
- Binder 驱动学习笔记
- 关于qt学习的一点小记录(1)
- springmvc 开涛 拦截器
- Android之NDK开发Androd.mk
- js 瀑布流加载图片
- iOS 文件操作相关
- nyoj 168 房间安排(区间覆盖)
- 四大组件之一Service——应用实例二(IntentService类的使用)
- 二叉树两结点的最低公共父结点
- QT中获得打开文件的名字
- 类学习之一Pass arguments to base class
- NGUI研究院之为什么打开界面太慢(十三)
- 【VB.NET机房重构】数据传递----实体、DataTable、泛型
- 自定义ListView使用Volley获取数据