Irrlicht学习之向量变化部分

来源:互联网 发布:淘宝网汉服女装 编辑:程序博客网 时间:2024/05/17 01:19

有2D向量和3D向量。

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 +=bvector3d<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 vectortypedef vector3d<f32> vector3df;//! Typedef for an integer 3d vectortypedef vector3d<s32> vector3di;} // end namespace irr} // end namespace core#endif

分析待完善。

0 0
原创粉丝点击