当前位置:网站首页>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;
}
边栏推荐
- MRS离线数据分析:通过Flink作业处理OBS数据
- Excuse me, I have three partitions in Kafka, and the flinksql task has written the join operation. How can I give the join operation alone
- 课设之百万数据文档存取
- 一款你不容错过的Laravel后台管理扩展包 —— Voyager
- 小程序目录结构
- Ian Goodfellow, the inventor of Gan, officially joined deepmind as research scientist
- Substance painter notes: settings for multi display and multi-resolution displays
- Wired network IP address of VMware shared host
- WPF DataGrid realizes the UI interface to respond to a data change in a single line
- 低代码平台中的数据连接方式(下)
猜你喜欢

PERT图(工程网络图)

最长上升子序列模型 AcWing 482. 合唱队形
![GVIM [III] [u vimrc configuration]](/img/82/38355d7914e5fe490546347e57e35d.png)
GVIM [III] [u vimrc configuration]

潘多拉 IOT 开发板学习(HAL 库)—— 实验12 RTC实时时钟实验(学习笔记)

Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding

JS get the current time, month, day, year, and the uniapp location applet opens the map to select the location

Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning

Beginner JSP

libSGM的horizontal_path_aggregation程序解读

常用数字信号编码之反向不归零码码、曼彻斯特编码、差分曼彻斯特编码
随机推荐
找到自己的价值
Internal sort - insert sort
Cesium 已知一点经纬度和距离求另一个点的经纬度
leetcode:648. 单词替换【字典树板子 + 寻找若干前缀中的最短符合前缀】
Oracle non automatic submission solution
Substance painter notes: settings for multi display and multi-resolution displays
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
Analysis of arouter
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
最长上升子序列模型 AcWing 482. 合唱队形
NDK beginner's study (1)
Hangdian oj2092 integer solution
CVPR2022 | 医学图像分析中基于频率注入的后门攻击
Selenium Library
Data flow diagram, data dictionary
oracle 触发器实现级联更新
Reading and understanding of eventbus source code
electron remote 报错
昇腾体验官第五期随手记I
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join