当前位置:网站首页>cocos:ccpDistance函数的简单运用以及实现眼球随着手指在眼眶中转动的功能
cocos:ccpDistance函数的简单运用以及实现眼球随着手指在眼眶中转动的功能
2022-07-27 19:21:00 【ImCode0222】
cocos:ccpDistance函数的简单运用以及实现眼球随着手指在眼眶中转动的功能
设备/引擎:Mac(11.6)/cocos
开发工具:Xcode
开发语言:c++
开发需求:实现眼球随着手指在屏幕上的移动而在眼球中转动
1.ccpDistance函数简单介绍
函数定义:
float
ccpDistance(const CCPoint& v1, const CCPoint& v2)
{
return (v1 - v2).getLength();
}
函数作用:返回坐标V1与坐标V2之间的距离
getLength()的函数定义
inline float getLength() const {
return sqrtf(x*x + y*y); //x即为两坐标的横坐标差值,y为两坐标的纵坐标差值
};
2.计算手指与眼球的坐标距离
float dis1 = ccpDistance(location, eyeLInPos);//location即为手指当前所处位置坐标
3.根据两点间的距离来决定眼球的移动距离
1)首先我们心里要大概明白手指与眼球的距离V1与眼球移动的距离V2之间的关系,也就是两者之间的倍数,而且我们也不能拿一两个数据来决定倍数(比如:50倍-V1/50,可能手指在某些位置时眼球会随之转动,但如果位置过近,则眼球几乎是不动的),
2)根据以上分析知道,单靠两点间的距离得到的偏移量是不准确的,还需要一个值才能比较合理的得到位置偏移量,那就是触摸点的横纵坐标与眼球的初始横纵坐标之间的差值
3)通过以上的分析,最后就是确定倍数了,我们可以在程序中测出触摸最近点与触摸最远点时的V1值,我们的目的是需要最后眼球的移动距离不会因为触摸点的远近而产生很大的差距,由此就可以得到一个等式,来求出到底需要多少倍,等式如下:
D1:最近的距离
D2:最远的距离
X:需要求出的值
X/D:保证值的可用性,值的范围保证在(0.01~0.09)
X/D1*(location.x-eyeLInPos.x) = X/D2*(location.x-eyeLInPos.x)
带入工程中的值得出X≈5,进而可得出以下代码:
eyeL->setPosition(ccp(eyeLInPos.x+5/V1*(location.x-eyeLInPos.x), eyeLInPos.y+5/V1*(location.y-eyeLInPos.y)));
//5/V1:
注:记得停止触摸时将眼球归为
希望能给大家带来帮助!!!有什么问题需要讨论的可以评论私信欢迎讨论~
边栏推荐
- 二维数组的基本用法
- How to realize a good knowledge management system?
- Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
- Go language learning notes - mutex start go language from scratch
- Software testing interview question: what is regression testing?
- How to customize logging of.Net6.0
- Leetcode 148. sorting linked list
- 【海洋科学】海洋气候指数【Climate Indices】数据集
- How to deal with high concurrency deadlock?
- Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
猜你喜欢

@Detailed introduction of requestparam annotation

Project analysis (what is it training that can't be given)

Common shortcut keys and setting methods of idea

Encapsulate an array into a class

异常-Exception

STM32项目分享---MQTT智能门禁系统(含APP控制)

每条你收藏的资讯背后,都离不开TA

The design idea of relational database is obvious to you in 20 pictures

If demand splitting is as simple as cutting a cake | agile practice

8000字讲透OBSA原理与应用实践
随机推荐
MySQL execution process and order
数组扩容、排序、嵌套语句应用
直播app系统源码,上下叠层样式的轮播图
matlab 绘制三坐标(轴)图
2022 2nd cyber edge cup cyber security competition Web
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?
Behind every piece of information you collect, you can't live without TA
@Component可以和@Bean 用在同一个类上吗?
First knowledge of esp8266 (I) -- access point and wireless terminal mode
MySQL执行过程及执行顺序
Pythia: Facebook's latest open source visual and language multitasking learning framework
How to buy stocks on mobile phones? Is it safe to open an account
九天后我们一起,聚焦音视频、探秘技术新发展
每条你收藏的资讯背后,都离不开TA
Interview question: what are the functions of fail safe mechanism and fail fast mechanism
STM32 project Sharing -- mqtt intelligent access control system (including app control)
Read Plato farm's eplato and the reason for its high premium
Matlab 绘制风速、风向统计玫瑰花图
Software test interview questions: the steps to write test cases by drawing cause and effect diagrams are___ And transforming the cause and effect diagram into a state diagram in five steps. What are