当前位置:网站首页>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:
注:记得停止触摸时将眼球归为
希望能给大家带来帮助!!!有什么问题需要讨论的可以评论私信欢迎讨论~
边栏推荐
- STM32项目分享---MQTT智能门禁系统(含APP控制)
- Monitor the running of server jar and restart script
- 除了「加机器」,其实你的微服务还能这样优化
- 为什么服务端程序都需要先 listen 一下
- Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor
- Basic usage of two-dimensional array
- STM32 project Sharing -- mqtt intelligent access control system (including app control)
- 8000字讲透OBSA原理与应用实践
- Enumeration and annotation
- 2019q4 memory manufacturers' revenue ranking: Samsung fell 5%, only SK Hynix and micron maintained growth
猜你喜欢

An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!

除了「加机器」,其实你的微服务还能这样优化

ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现

The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"

Log4j 漏洞仍普遍存在,并持续造成影响

ThreadLocal principle and source code analysis (click in step by step, don't recite, learn ideas)

Log4j 漏洞仍普遍存在?

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

How to use Fiddler for weak network testing

STM32项目分享---MQTT智能门禁系统(含APP控制)
随机推荐
LVS+Keepalived高可用群集
Can JVM tuning be done with single core CPU and 1G memory?
In addition to "adding machines", in fact, your micro service can be optimized like this
Starrocks community structure comes out, waiting for you to upgrade!
Array expansion, sorting, nested statement application
Leetcode 301. delete invalid parentheses
How to deal with high concurrency deadlock?
8000字讲透OBSA原理与应用实践
Nine days later, we are together to focus on the new development of audio and video and mystery technology
8000 word explanation of OBSA principle and application practice
Talk about MySQL transaction two-phase commit
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
关系型数据库的设计思想,20张图给你看的明明白白
Log4j 漏洞仍普遍存在?
Huawei establishes global ecological development department: fully promote HMS global ecological construction
Regular expression exercise
直播app系统源码,上下叠层样式的轮播图
Pytoch distributed training
排序(冒泡排序)后面学习持续更新其它排序方法