当前位置:网站首页>Cocos creator direction and angle conversion
Cocos creator direction and angle conversion
2022-07-07 14:28:00 【Snail games】
1、 Let an object move in his direction
cc.Node Node rotation It's an angle , therefore , In order to achieve this goal , First of all, according to rotation Find out his dir Direction , It's easy to do. . Sample code :
update(dt){
// because Math The function accepts solitude , So we first convert the rotation of nodes into radians
var angle = this.node.rotation / 180 * Math.PI;
// Synthesis based on X The direction vector in the positive direction
var dir = cc.v2(Math.cos(angle),Math.sin(angle));
// Unit vector
dir.normalizeSelf();
// Move the position according to the direction vector
var moveSpeed = 100;
this.node.x += dt * dir.x * moveSpeed;
this.node.y += dt * dir.y * moveSpeed;
}
2、 Make one object face another
Through high school mathematics, we can know , A To B Vector = B spot - A spot . Then we just need to set the location of the target object - My place , You can get the direction vector .
The direction vector is converted to an angle , We need to recognize an implicit variable , The benchmark of this angle is X Affirmative direction . Using vectors to represent is (1,0).
cc.Vec2 Two functions are provided cc.Vec2.angle and cc.Vec2.signAngle, Compared with the former , The angle produced by the latter is symbolic , The former will always be positive . We use cc.Vec2.signAngle To convert the direction vector into radians . See the code for details .
function lookAtObj(target){
// Calculate the orientation
var dx = target.x - this.node.x;
var dy = target.y - this.node.y;
var dir = cc.v2(dx,dy);
// Calculate the included angle radian according to the orientation
var angle = dir.signAngle(cc.v2(1,0));
// Convert radians into Euler angles
var degree = angle / Math.PI * 180;
// Assign to node
this.node.rotation = degree;
}
边栏推荐
- Excuse me, why is it that there are no consumption messages in redis and they are all piled up in redis? Cerely is used.
- Applet directory structure
- The longest ascending subsequence model acwing 482 Chorus formation
- Hands on Teaching: XML modeling
- 股票开户首选,炒股交易开户佣金最低网上开户安全吗
- Substance painter notes: settings for multi display and multi-resolution displays
- 请问,PTS对数据库压测有好方案么?
- 【网络安全】sql注入语法汇总
- GAN发明者Ian Goodfellow正式加入DeepMind,任Research Scientist
- Leetcode——236. The nearest common ancestor of binary tree
猜你喜欢
Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning
Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
Cvpr2022 | backdoor attack based on frequency injection in medical image analysis
leetcode:648. 单词替换【字典树板子 + 寻找若干前缀中的最短符合前缀】
Pert diagram (engineering network diagram)
一个程序员的水平能差到什么程度?尼玛,都是人才呀...
2022年13个UX/UI/UE最佳创意灵感网站
MRS离线数据分析:通过Flink作业处理OBS数据
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
Mrs offline data analysis: process OBS data through Flink job
随机推荐
PD虚拟机教程:如何在ParallelsDesktop虚拟机中设置可使用的快捷键?
docker部署oracle
搜索引擎接口
【网络安全】sql注入语法汇总
Substance Painter筆記:多顯示器且多分辨率顯示器時的設置
LeetCode每日一题(636. Exclusive Time of Functions)
多商户商城系统功能拆解01讲-产品架构
MRS离线数据分析:通过Flink作业处理OBS数据
Mmkv use and principle
关于后台动态模板添加内容的总结 Builder使用
Notes de l'imprimante substance: paramètres pour les affichages Multi - écrans et multi - Résolutions
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
Bashrc and profile
Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
Million data document access of course design
PERT图(工程网络图)
Hands on Teaching: XML modeling
The longest ascending subsequence model acwing 1012 Sister cities
C # use TCP protocol to establish connection