当前位置:网站首页>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;
}
边栏推荐
- 多商户商城系统功能拆解01讲-产品架构
- 小米的芯片自研之路
- 【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
- 一文读懂数仓中的pg_stat
- UML sequence diagram (sequence diagram)
- 今日睡眠质量记录78分
- Nllb-200: meta open source new model, which can translate 200 languages
- PLC:自动纠正数据集噪声,来洗洗数据集吧 | ICLR 2021 Spotlight
- The longest ascending subsequence model acwing 482 Chorus formation
- 手把手教会:XML建模
猜你喜欢

The longest ascending subsequence model acwing 482 Chorus formation

最长上升子序列模型 AcWing 482. 合唱队形

UML state diagram

js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点

Démontage de la fonction du système multi - Merchant Mall 01 - architecture du produit

Ian Goodfellow, the inventor of Gan, officially joined deepmind as research scientist

Substance Painter筆記:多顯示器且多分辨率顯示器時的設置

Selenium Library

Parsing of XML files

Substance painter notes: settings for multi display and multi-resolution displays
随机推荐
解析PHP跳出循环的方法以及continue、break、exit的区别介绍
Pert diagram (engineering network diagram)
Introduction to sakt method
The longest ascending subsequence model acwing 1014 Mountaineering
用例图
LeetCode 648. 单词替换
Cvpr2022 | backdoor attack based on frequency injection in medical image analysis
2022年13个UX/UI/UE最佳创意灵感网站
Substance Painter笔记:多显示器且多分辨率显示器时的设置
Differences between cookies and sessions
Environment configuration
一款你不容错过的Laravel后台管理扩展包 —— Voyager
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
一文读懂数仓中的pg_stat
Vscode configuration uses pylint syntax checker
leetcode:648. 单词替换【字典树板子 + 寻找若干前缀中的最短符合前缀】
Leetcode——236. 二叉树的最近公共祖先
Data connection mode in low code platform (Part 2)
请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。