数学类Vec2SizeRect,是cocos2dx中比较常用的类。

比如设置图片位置,设置图片大小,两图片的碰撞检测等等。

比起2.x版本,在3.x中本质上其实没有太大的变化,主要的变化就是将全局宏定义相关的操作封装到各自的类中而已。比如:Vec2的向量运算宏定义ccp***(),现在都已经封装到Vec2类里面去了。

在V2.x中,底层数学库使用的是:Kazmath数学库

而在 V3.1 中,由于 Sprite3D 需要我们提供更多的API给开发者,这是Kazmath库所不能提供的,而cocos2d-x内部拥有多个数学库是没有意义的。

所以V3.1中,底层选择了新的数学库:GamePlay3D数学库


【Vec2】

Vec2原名Point,它既可以表示一个二维坐标点,又可以表示一个二维向量。

同时Vec2对运算符进行了重载,可以很方便的完成Vec2的赋值、加减乘除等操作。另外还有与坐标向量相关的:距离、角度、点积、叉积、投影、标准化等操作。

此外在3.x中,还将2.x里的函数定义ccp***(如ccp,ccpAdd,ccpSub)相关的操作都封装到了这个Vec2的类中,这样就可以更加系统化地管理向量的运算操作了。

此外,除了Vec2。还有两个坐标类:Vec3、Vec4,分别代表了三维、四维坐标向量。

Vec2可以是一个二维坐标点,也可以是一个二维向量。

1、创建方式

  1. //
  2. /**
  3. * Vec2只有两个成员变量x , y
  4. */
  5. float x; //X坐标
  6. float y; //Y坐标
  7. /**
  8. * 构造函数
  9. */
  10. Vec2(); //(0 , 0)
  11. Vec2(float xx, float yy); //(xx , yy)
  12. Vec2(const float* array); //(array[0] , array[1])
  13. Vec2(const Vec2& copy); //copy
  14. Vec2(const Vec2& p1, const Vec2& p2); //p2 - p1
  15. //

2、设置向量坐标

使用set可以给向量重新设置新坐标值。

  1. //
  2. void set(float xx, float yy); //(xx , yy)
  3. void set(const float* array); //(array[0] , array[1])
  4. void set(const Vec2& v); //v
  5. void set(const Vec2& p1, const Vec2& p2); //p2 - p1
  6. //

3、向量运算

其中包含了一些2.x中的ccp***()宏定义的函数,都全部封装到了Vec2类中。

  1. //
  2. /**
  3. * 向量运算
  4. * void : 自身运算 , 值会改变
  5. * 有返回值 : 返回运算结果, 值不会改变
  6. */
  7. void add(const Vec2& v); //相加( x+v.x , y+v.y )
  8. void subtract(const Vec2& v); //相减( x-v.x , y-v.y )
  9. void clamp(const Vec2& min, const Vec2& max); //将向量值限制在[min,max]区间内
  10. void negate(); //向量取负( -x , -y )
  11. void normalize(); //标准化向量. 若为零向量,忽略
  12. void scale(float scalar); //x,y坐标同时放缩
  13. void scale(const Vec2& scale); //x,y坐标分别放缩
  14. void rotate(const Vec2& point, float angle); //绕point点, 旋转angle弧度
  15. float dot(const Vec2& v) const; //点积: x*v.x + y*v.y
  16. float cross(const Vec2& v) const; //叉积: x*v.y - y*v.x
  17. Vec2 project(const Vec2& v) const; //投影: 向量在v上的投影向量
  18. float distance(const Vec2& v) const; //与v的距离.
  19. float distanceSquared(const Vec2& v) const; //与v的距离平方.
  20. float length() const; //向量长度. 即与原点的距离
  21. float lengthSquared() const; //向量长度平方. 即与原点的距离平方
  22. Vec2 getNormalized() const; //获取向量的标准化形式. 若为零向量,返回(0,0)
  23. inline Vec2 getPerp() const; //逆时针旋转90度. Vec2(-y, x);
  24. inline Vec2 getRPerp() const //顺时针旋转90度. Vec2(y, -x);
  25. inline float getAngle() const; //与X轴的夹角(弧度)
  26. float getAngle(const Vec2& v) const; //与v向量的夹角(弧度)
  27. inline Vec2 getMidpoint(const Vec2& v) const; //计算两点间的中点
  28. //将向量值限制在[min,max]区间内,返回该点
  29. inline Vec2 getClampPoint(const Vec2& min, const Vec2& max) const
  30. {
  31. return Vec2(clampf(x, min.x, max.x), clampf(y, min.y, max.y));
  32. }
  33. bool isZero() const; //是否为(0,0)
  34. bool isOne() const; //是否为(1,1)
  35. //判断target是否在坐标点模糊偏差为var的范围内.
  36. //if( (x - var <= target.x && target.x <= x + var) &&
  37. // (y - var <= target.y && target.y <= y + var) )
  38. // return true;
  39. bool fuzzyEquals(const Vec2& target, float variance) const;
  40. //以pivot为轴, 逆时针旋转angle度(弧度)
  41. Vec2 rotateByAngle(const Vec2& pivot, float angle) const;
  42. //绕other向量旋转
  43. //返回向量: 角度 this.getAngle() +other.getAngle();
  44. // 长度 this.getLength()*other.getLength();
  45. inline Vec2 rotate(const Vec2& other) const {
  46. return Vec2(x*other.x - y*other.y, x*other.y + y*other.x);
  47. };
  48. //绕other向量旋转前的向量值
  49. //返回向量: 角度 this.getAngle() -other.getAngle();
  50. // 长度 this.getLength()*other.getLength();
  51. //(这里是不是有点问题,难道不应该是this.getLength()/other.getLength()么?)
  52. inline Vec2 unrotate(const Vec2& other) const {
  53. return Vec2(x*other.x + y*other.y, y*other.x - x*other.y);
  54. };
  55. //两个点a和b之间的线性插值
  56. //alpha ==0 ? a alpha ==1 ? b 否则为a和b之间的一个值
  57. inline Vec2 lerp(const Vec2& other, float alpha) const {
  58. return *this * (1.f - alpha) + other * alpha;
  59. };
  60. //平滑更新向量的当前位置,指向目标向量target.
  61. //responseTime定义了平滑时间量,该值越大结果越平滑,相应的延迟时间越长。
  62. //如果希望向量紧跟target向量,提供一个相对elapsedTime小很多的responseTime值即可。
  63. //参数
  64. //target 目标值
  65. //elapsedTime 消逝时间
  66. //responseTime 响应时间
  67. void smooth(const Vec2& target, float elapsedTime, float responseTime);
  68. /**
  69. * 自定义运算
  70. * compOp
  71. */
  72. //对该点向量形式的各分量进行function参数来指定的运算,
  73. //如absf,floorf,ceilf,roundf等,
  74. //任何函数拥有如下形式:float func(float)均可。
  75. //例如:我们对x,y进行floor运算,则调用方法为p.compOp(floorf);
  76. //3.0
  77. inline Vec2 compOp(std::function<float(float)> function) const
  78. {
  79. return Vec2(function(x), function(y));
  80. }
  81. /**
  82. * 兼容代码
  83. * 估计是要被抛弃了~(>_<)~
  84. */
  85. void setPoint(float xx, float yy); //同set(float xx, float yy)
  86. bool equals(const Vec2& target) const; //同==
  87. float getLength() const; //同length()
  88. float getLengthSq() const; //同lengthSquared()
  89. float getDistance(const Vec2& other) const; //同distance(const Vec2& v)
  90. float getDistanceSq(const Vec2& other) const; //同distanceSquared(const Vec2& v)
  91. //

4、运算符重载

  1. //
  2. inline const Vec2 operator+(const Vec2& v) const; //( x+v.x , y+v.y )
  3. inline const Vec2 operator-(const Vec2& v) const; //( x-v.x , y-v.y )
  4. inline const Vec2 operator*(float s) const; //( x*s , y*s )
  5. inline const Vec2 operator/(float s) const; //( x/s , y/s )
  6. inline const Vec2 operator-() const; //( -x , -y )
  7. inline Vec2& operator+=(const Vec2& v); //(x,y) = ( x+v.x , y+v.y )
  8. inline Vec2& operator-=(const Vec2& v); //(x,y) = ( x-v.x , y-v.y )
  9. inline Vec2& operator*=(float s); //(x,y) = ( x*s , y*s )
  10. inline bool operator<(const Vec2& v) const;
  11. inline bool operator==(const Vec2& v) const;
  12. inline bool operator!=(const Vec2& v) const;
  13. //

5、静态函数与常量

  1. //
  2. /**
  3. * 静态方法
  4. */
  5. static void add(const Vec2& v1, const Vec2& v2, Vec2* dst); //dst = v1 + v2
  6. static void subtract(const Vec2& v1, const Vec2& v2, Vec2* dst); //dst = v1 - v2
  7. static void clamp(const Vec2& v, const Vec2& min, const Vec2& max, Vec2* dst); //将向量v限制在[min,max]区间内,结果存入dst
  8. static float angle(const Vec2& v1, const Vec2& v2); //两向量夹角(弧度)
  9. static float dot(const Vec2& v1, const Vec2& v2); //两向量点积
  10. static inline Vec2 forAngle(const float a); //返回向量坐标 x=cos(a) , y=sin(a)
  11. /**
  12. * 静态常量
  13. */
  14. static const Vec2 ZERO; //Vec2(0, 0)
  15. static const Vec2 ONE; //Vec2(1, 1)
  16. static const Vec2 UNIT_X; //Vec2(1, 0)
  17. static const Vec2 UNIT_Y; //Vec2(0, 1)
  18. static const Vec2 ANCHOR_MIDDLE; //Vec2(0.5, 0.5)
  19. static const Vec2 ANCHOR_BOTTOM_LEFT; //Vec2(0, 0)
  20. static const Vec2 ANCHOR_TOP_LEFT; //Vec2(0, 1)
  21. static const Vec2 ANCHOR_BOTTOM_RIGHT; //Vec2(1, 0)
  22. static const Vec2 ANCHOR_TOP_RIGHT; //Vec2(1, 1)
  23. static const Vec2 ANCHOR_MIDDLE_RIGHT; //Vec2(1, 0.5)
  24. static const Vec2 ANCHOR_MIDDLE_LEFT; //Vec2(0, 0.5)
  25. static const Vec2 ANCHOR_MIDDLE_TOP; //Vec2(0.5, 1)
  26. static const Vec2 ANCHOR_MIDDLE_BOTTOM; //Vec2(0.5, 0)
  27. //

6、线段相交检测

这些用于检测线段相交的函数,也都是静态的成员函数

  1. //
  2. /**
  3. 线段相交检测 v3.0
  4. 参数:
  5. A 为线段L1起点. L1 = (A - B)
  6. B 为L1终点 . L1 = (A - B)
  7. C 为线段L2起点. L2 = (C - D)
  8. D 为L2终点 . L2 = (C - D)
  9. S 为L1上计算各点的插值参数,计算方法为:p = A + S*(B - A)
  10. T 为L2上计算各点的插值参数,计算方法为:p = C + T*(D - C)
  11. */
  12. //直线AB与线段CD是否平行
  13. static bool isLineParallel(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D);
  14. //直线AB与线段CD是否重叠
  15. static bool isLineOverlap(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D);
  16. //直线AB与直线CD是否相交
  17. static bool isLineIntersect(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D,
  18. float *S = nullptr, float *T = nullptr);
  19. //线段AB与线段CD是否重叠
  20. static bool isSegmentOverlap(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D,
  21. Vec2* S = nullptr, Vec2* E = nullptr);
  22. //线段AB与线段CD是否相交
  23. static bool isSegmentIntersect(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D);
  24. //返回直线AB与直线CD的交点
  25. static Vec2 getIntersectPoint(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D);
  26. //

【Size】

Size比较简单,只是一个用来表示尺寸大小的类。宽为width,高为height

和Vec2一样,也对一些运算符进行了重载。

与2.x相比,没有太大的变化。

PS: 因为和Vec2一样,都只有两个成员变量,所以Size和Vec2之间可以相互转换。

1、主要函数如下

  1. //
  2. class CC_DLL Size
  3. {
  4. /**
  5. * Size只有两个成员变量width , height
  6. */
  7. float width; //宽
  8. float height; //高
  9. /**
  10. * 构造函数
  11. */
  12. Size(); //(0, 0)
  13. Size(float width, float height); //(width, height)
  14. Size(const Size& other); //other
  15. explicit Size(const Vec2& point); //(显式)构造函数. 构造时Size size = Size(Vec2&), 而不能Size size = vec2;
  16. /**
  17. * 相关操作
  18. * - setSize
  19. * - equals
  20. * - Vec2()
  21. */
  22. void setSize(float width, float height); //设置尺寸
  23. bool equals(const Size& target) const; //判断是否等于target
  24. //Size::Vec2()
  25. //返回类型为Vec2
  26. operator Vec2() const { return Vec2(width, height); }
  27. /**
  28. * 静态常量
  29. */
  30. static const Size ZERO; //(0, 0)
  31. /**
  32. * 运算符重载
  33. */
  34. Size& operator= (const Size& other);
  35. Size& operator= (const Vec2& point); //可以用Vec2赋值
  36. Size operator+(const Size& right) const;
  37. Size operator-(const Size& right) const;
  38. Size operator*(float a) const;
  39. Size operator/(float a) const;
  40. };
  41. //

【Rect】

Rect是一个矩形类。包含两个成员属性:起始坐标(左下角)Vec2、矩阵尺寸大小Size

Rect只对“=”运算符进行了重载。

与2.x相比,多了一个函数unionWithRect,用于合并两个矩形。

值得注意的是Rect类中:

        intersectsRect函数,可以用作两个Rect矩形是否相交,即碰撞检测。

        containsPoint 函数,可以用作判断点Vec2是否在Rect矩形中。

        unionWithRect 函数,可以用做将两矩形进行合并操作。

1、主要函数如下

  1. //
  2. class CC_DLL Rect
  3. {
  4. public:
  5. Vec2 origin; //起始坐标: 矩形左下角坐标
  6. Size size; //尺寸大小
  7. /**
  8. * 构造函数
  9. */
  10. Rect();
  11. Rect(float x, float y, float width, float height);
  12. Rect(const Rect& other);
  13. /**
  14. * 运算符重载
  15. * 只重载了 “=” 运算符
  16. */
  17. Rect& operator= (const Rect& other);
  18. /**
  19. * 相关操作
  20. * - setRect
  21. * - getMinX , getMidX , getMaxX
  22. * - getMinY , getMidY , getMaxY
  23. * - equals , containsPoint , intersectsRect
  24. * - unionWithRect
  25. */
  26. //设置矩形
  27. void setRect(float x, float y, float width, float height);
  28. //获取矩形信息
  29. float getMinX() const; //origin.x
  30. float getMidX() const; //origin.x + size.width/2
  31. float getMaxX() const; //origin.x + size.width
  32. float getMinY() const; //origin.y
  33. float getMidY() const; //origin.y + size.height/2
  34. float getMaxY() const; //origin.y + size.height
  35. //判断是否与rect相同. 原点相同,尺寸相同.
  36. bool equals(const Rect& rect) const;
  37. //判断point是否包含在矩形内或四条边上
  38. bool containsPoint(const Vec2& point) const;
  39. //判断矩形是否相交. 常常用作碰撞检测.
  40. bool intersectsRect(const Rect& rect) const;
  41. //与rect矩形合并. 并返回结果. v3.0
  42. //不会改变原矩形的值
  43. Rect unionWithRect(const Rect & rect) const;
  44. /**
  45. * 静态常量
  46. * Rect::ZERO
  47. */
  48. static const Rect ZERO;
  49. };
  50. //

2、精灵创建中的一种方式

还记得Sprite的几种创建方式吗?里面有一种创建方式如下:

Sprite::create(const std::string& filename, const Rect& rect)

若用Rect来作为创建Sprite精灵的参数,需要注意,从大图中截取某一区域的图片的Rect rect的构造应该是这样的:

Rect("小图左上角坐标x", "小图左上角坐标y", 小图宽, 小图高);

使用的是UIKit坐标系,而不是cocos2dx的OpenGL坐标系是不一样的。

如下图所示:

3、矩形合并函数unionWithRect

看几张图,你应该就会明白了。

两个黑色矩形区域,使用unionWithRect合并后,变成红色矩形区域。

    

cocos2dx[3.2](8) 数学类Vec2/Size/Rect的更多相关文章

  1. 【转】Cocos2d-x 3.x基础学习: 总结数学类Vec2/Size/Rect

    转载:http://www.taikr.com/article/1847 在Cocos2d-x 3.x中,数学类Vec2.Size.Rect,是比较常用的类.比如设置图片位置,图片大小,两图片的碰撞检 ...

  2. Lua 数学类

    数学类主要有Vec2(坐标向量).Size(尺寸).Rect(矩形). 创建 在Lua中创建的 Vec2.Size.Rect 都是一个table类型. 其中只有相应的成员变量,没有相关的函数运算. c ...

  3. Java:日历类、日期类、数学类、运行时类、随机类、系统类

    一:Calendar类 java.util 抽象类Calendar   1.static Calendar getInstance()使用默认时区和语言环境获得一个日历. 2. int get(int ...

  4. cocos2d-x v3.2 FlappyBird 各个类对象详细代码分析(7)

    今天我们介绍最后两个类 GameOverLayer类 GameLayer类 GameLayer类是整个游戏中最重要的类,由于是整个游戏的中央系统,控制着各个类(层)之间的交互,这个类中实现了猪脚小鸟和 ...

  5. [19/03/18-星期一] 常用类_Math(数学)类&Rondom(随机数)类

    一.Math(数学)类(单独一个Java.Math 包中) java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型.如果需要更加强大的数学运算能力, ...

  6. 说说C#的数学类,Math,浮点数(上)

    说说C#的数学类,Math,浮点数 C#语言支持下图所看到的的数值类型,各自是整数,浮点数和小数 可能不是非常清楚,可是细致看看还是能看清楚的. 在一个C#程序中,整数(没有小数点的数)被觉得是一个i ...

  7. Math 数学类

    /* Math 数学类, 主要是提供了很多的数学公式. abs(double a) 获取绝对值 ceil(double a) 向上取整 floor(double a) 向下取整 round(float ...

  8. cocos2dx 3.17.1 导演类

    进入导演类的头文件,首先看到的是一些头文件的引用:CCPlatformMacros(适配),CCRef(继承的父类),CCVector(3.0以后的新向量),CCScene(场景),CCMath(数学 ...

  9. cocos2d-x 的两大基类

    cocos2d-x 有两个重要的基类,一个管理引用计数的 Ref,别一个则定义许多基本属性的 Node. 在 cocos2d-x 中的基本概念 说到 create 函数的时候提到 cocos2d-x ...

随机推荐

  1. Mongodb数据模型

    描述表关系的方式: 方式一:嵌入式 > db.person.find({name:'zjf'}).pretty() { "_id" : ObjectId("592f ...

  2. Java介绍、环境的搭建及结构化程序

    一.Java 简介及环境配置: JDK和JRE的区别:JRE(Java Runtime Environment)Java运行时环境有些程序运行需要Java环境,因此JRE只是给客户端使用的. JDK( ...

  3. Docker从0开始之部署一套2048

    创建容器并运行程序 [root@localhost ~]# docker run -d -p 8888:80 daocloud.io/daocloud/dao-2048:master-a2c564e ...

  4. linux命令详解之ls命令

    ls命令概述 ls命令用于显示文件目录列表,和Windows系统下DOS命令dir类似.当执行ls命令时,默认显示的只有非隐藏文件的文件名.以文件名进行排序及文件名代表的颜色显示.当不加参数时,默认列 ...

  5. 杀掉nginx进程

    ps aux | grep nginx kill -INT 进程号(例如:2661)

  6. dockerfile:python-cuda-nvidia-cudnn

    centos7 FROM centos:7 MAINTAINER yon@DataExa.com RUN yum -y install make wget \ && wget -O / ...

  7. Unity3D_(游戏)跳一跳超简单制作过程

    跳一跳 工程文件界面 游戏界面 脚本 using DG.Tweening; using System.Collections; using System.Collections.Generic; us ...

  8. 如何将项目托管到Github上

    将本地项目放到GitHub上托管并展示 传送门 利用Github Pages展示自己的项目 传送门 git Please tell me who you are解决方法 传送门 git config ...

  9. IDEA 无法显示项目目录结构解决

    不要去网上看什么乱七八糟的骚教程,一点用都没有.直接按下列步骤操作: 1. 关闭IDEA, 2. 然后删除项目文件夹下的.idea文件夹3. 重新用IDEA工具打开项目

  10. leetcode-easy-others-20 Valid Parentheses

    mycode   95.76% class Solution(object): def isValid(self, s): """ :type s: str :rtype ...