Cocoscreator 向量计算

本文详细介绍了在Cocos2d-x中如何让一个对象朝向另一个对象,包括通过atan2函数计算角度,利用向量计算方向,并实现基于旋转角度的对象移动。同时,探讨了顺时针与逆时针旋转的区别及其在Cocos2d-x中的应用。


1. 起点A(0,0) 终点B(100,100),特别注意
    var angle = Math.atan2((this.B.y - this.A.y), (this.B.x - this.A.x));
    angle如果是正,顺时针旋转,B点减去A点,一定是一个正值,所以顺时针旋转,但是在cocos中一般要的是逆时针旋转,所以前面需要添加一个负号来实现逆时针旋转
    
    var angle = Math.atan2((this.A.y - this.B.y), (this.A.x - this.B.x));
    angle如果是负,逆时针旋转,A点减去B点,一定是一个负值,所以逆时针旋转。这个就刚刚好。cocos中一般要的是逆时针旋转
    
2. 让一个对象朝向另一个对象  旋转角度一
    cc.Class({
    extends: cc.Component,

    properties: {
        target:{
            default: null,
            type:cc.Node
        },
    },
    start () {
        let angle = this.getAngle();
        this.node.rotation = angle;
    },
    getAngle() {
        //第一种方法,用目标点减去起点,得出angle如果是正,顺时针旋转,但是在cocos中一般要的是逆时针旋转,所以前面需要添加一个负号来实现逆时针旋转
        var angle = -Math.atan2((this.target.y - this.node.y), (this.target.x - this.node.x));
        
        //第二种方法,用起点减去目标点,一定是一个负值,所以逆时针旋转。这个就刚刚好。cocos中一般要的是逆时针旋转
        var angle = Math.atan2((this.node.y - this.target.y), (this.node.x - this.target.x));
        
        var theta = angle * (180 / Math.PI);
        return theta;
    },
    });
    
2.让一个对象朝向另一个对象  旋转角度方法二
    getAngle() {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值