当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Selenium库

最长上升子序列模型 AcWing 1012. 友好城市

2022pagc Golden Sail award | rongyun won the "outstanding product technology service provider of the year"

Vscode configuration uses pylint syntax checker

Substance Painter笔记:多显示器且多分辨率显示器时的设置

Transferring files between VMware and host

AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?

leetcode:648. 单词替换【字典树板子 + 寻找若干前缀中的最短符合前缀】

CVPR2022 | 医学图像分析中基于频率注入的后门攻击

Assign a dynamic value to the background color of DataGrid through ivalueconverter
随机推荐
Leetcode——剑指 Offer 05. 替换空格
Mlgo: Google AI releases industrial compiler optimized machine learning framework
Cargo placement problem
Vscode configuration uses pylint syntax checker
wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
PLC:自动纠正数据集噪声,来洗洗数据集吧 | ICLR 2021 Spotlight
【愚公系列】2022年7月 Go教学课程 005-变量
MRS离线数据分析:通过Flink作业处理OBS数据
Navigation — 这么好用的导航框架你确定不来看看?
Differences between cookies and sessions
今日睡眠质量记录78分
Similarities and differences between switches and routers
数据湖(九):Iceberg特点详述和数据类型
oracle 非自动提交解决
潘多拉 IOT 开发板学习(HAL 库)—— 实验12 RTC实时时钟实验(学习笔记)
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
最长上升子序列模型 AcWing 1014. 登山
ES日志报错赏析-maximum shards open
Source code analysis of ArrayList
Selenium库