当前位置:网站首页>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;
}
边栏推荐
- Cesium 已知一点经纬度和距离求另一个点的经纬度
- 云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
- Ascend 910实现Tensorflow1.15实现LeNet网络的minist手写数字识别
- 小米的芯片自研之路
- 一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
- 数据湖(九):Iceberg特点详述和数据类型
- IP address home location query
- 关于后台动态模板添加内容的总结 Builder使用
- 请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
- Million data document access of course design
猜你喜欢
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
docker部署oracle
CVPR2022 | 医学图像分析中基于频率注入的后门攻击
VSCode 配置使用 PyLint 语法检查器
内部排序——插入排序
Substance painter notes: settings for multi display and multi-resolution displays
云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
MRS离线数据分析:通过Flink作业处理OBS数据
常用数字信号编码之反向不归零码码、曼彻斯特编码、差分曼彻斯特编码
OAuth 2.0 + JWT protect API security
随机推荐
最长上升子序列模型 AcWing 1014. 登山
Parsing of XML files
Simple use of websocket
Beginner JSP
The longest ascending subsequence model acwing 1014 Mountaineering
First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
一文读懂数仓中的pg_stat
最长上升子序列模型 AcWing 1012. 友好城市
Assign a dynamic value to the background color of DataGrid through ivalueconverter
LeetCode 648. 单词替换
ES日志报错赏析-trying to create too many buckets
Leetcode——236. The nearest common ancestor of binary tree
Docker deploy Oracle
The longest ascending subsequence model acwing 1012 Sister cities
Wired network IP address of VMware shared host
Is the spare money in your hand better to fry stocks or buy financial products?
杭电oj2054 A == B ? ???
electron remote 报错
IP address home location query full version
Transferring files between VMware and host