CocosCreator 旋转角度问题2 碰撞反弹

import Tools from "../Tools";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BallBehaviour extends cc.Component {
    private screenWidth:number = 0;
    private screenHeight:number = 0;
    private limitX:number = 0;
    private limitY:number = 0;
    private defSpeed:number = 0;
    private speedX:number = 0;
    private speedY:number = 0;
    callBack:any = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    onKeyDown(event:cc.Event.EventKeyboard)
    {
        switch(event.keyCode)
        {
            case cc.macro.KEY.a:
                this.unschedule(this.callBack);
            break;
            case cc.macro.KEY.b:
                this.schedule(this.callBack, 0.02, cc.macro.REPEAT_FOREVER);
            break;
        }
    }

    start () {
        this.screenWidth = Math.floor(cc.winSize.width);
        this.screenHeight = Math.floor(cc.winSize.height);
        this.limitX = this.screenWidth / 2;
        this.limitY = this.screenHeight / 2;
        this.init(cc.Vec2.ZERO, 500, 45);
    }

    // update (dt) {}

    init(pos, defSpeed, angle)
    {
        this.defSpeed = defSpeed;
        this.node.position = pos;
        this.speedX = Tools.getSpeedX(this.defSpeed, Math.abs(angle));
        this.speedY = Tools.getSpeedY(this.defSpeed, Math.abs(angle));
        this.node.rotation = 90 - angle;
        this.callBack = function()
        {
            this.moveLogic(0.02);
        }
        this.schedule(this.callBack, 0.02, cc.macro.REPEAT_FOREVER);

    }
    
    moveLogic(dt)
    {
        if( (this.speedY > 0 && this.node.y > this.limitY) || (this.speedY < 0 && this.node.y < - this.limitY) )
        {
            this.speedY = -this.speedY;
            this.setRotation();
        }
        else if( ( this.speedX > 0 && this.node.x > this.limitX) || (this.speedX < 0 && this.node.x < - this.limitX) )
        {
            this.speedX = -this.speedX;
            this.setRotation();
        }
        this.node.x += this.speedX * dt;
        this.node.y += this.speedY * dt;
    }

    setRotation()  //方法一
    {
        let dirVec:cc.Vec2 =cc.Vec2.ZERO;
        dirVec.x = this.speedX;
        dirVec.y = this.speedY;
        let upVec:cc.Vec2 = cc.Vec2.UP;
        let degree = Tools.calculateAngle(dirVec, upVec);
        this.node.rotation = 180 + degree;
    }

    setRotation2()  //方法二
    {
        let dirVec:cc.Vec2 = cc.Vec2.ZERO;
        dirVec.x = this.speedX;
        dirVec.y = this.speedY;
        let upVec:cc.Vec2 = cc.Vec2.UP;
        let radian = dirVec.signAngle(upVec);
        let degree = cc.misc.radiansToDegrees(radian);
        this.node.rotation= degree;
    }
}
const {ccclass, property} = cc._decorator;

@ccclass
export default class Tools {

    static calculateAngle(first:cc.Vec2, second:cc.Vec2)
    {
        let len_y = second.y - first.y;
        let len_x = second.x - first.x;
        let tan_yx = Math.abs(len_y / len_x);
        let temp = Math.atan(tan_yx) * 180/Math.PI;
        let angle = 0;
        if(len_y > 0 && len_x < 0){
            angle = temp - 90;
        }
        else if(len_y > 0 && len_x > 0){
            angle = -temp + 90;
        }
        else if(len_y < 0 && len_x < 0){
            angle = -temp - 90;
        }
        else if(len_y < 0 && len_x > 0){
            angle = temp + 90;
        }
        else if(len_y == 0 && len_x != 0){
            angle = len_x < 0 ? -90 : 90;
        }
        else if(len_x == 0 && len_y != 0){
            angle = len_y < 0 ? 180 : 0;
        }
        console.log('Temp', temp);
        console.log('Angle ', angle)
        return angle;
    }

    static getSpeedX(speedOrigin: number, moveAngle: any)
    {
        speedOrigin = Math.abs(speedOrigin);
        let speedX = 0;
        switch(moveAngle)
        {
            case 0:
                speedX = speedOrigin;
            break;
            case 90:
            break;
            case 270:
            break;
            case 180:
                speedX = -speedOrigin;
            break;
            default:
                speedX = this.getCosValue(moveAngle) * speedOrigin;
            break;
        }
        return speedX;
    }

    static getSpeedY(speedOrigin: number, moveAngle: number)
    {
        speedOrigin = Math.abs(speedOrigin);
        let speedY = 0;
        switch(moveAngle)
        {
            case 0:
            break;
            case 90:
                speedY = speedOrigin;
            break;
            case 270:
                speedY = - speedOrigin;
            break;
            case 180:   
            break;
            default:
                speedY = this.getSinValue(moveAngle) * speedOrigin;
            break;
        }
        return speedY;
    }

    static getSinValue(angle: number)
    {
       return Math.sin(angle * Math.PI/180);
    }

    static getCosValue(angle: number)
    {
       return Math.cos(angle * Math.PI/180);
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: cocos creator node 可以通过以下方式设置角度: - 使用 node.angle 属性 - 使用 node.setRotation() 方法 - 使用 node.rotation 属性(以弧度制表示) 例如: ``` node.angle = 30; node.setRotation(45); node.rotation = Math.PI / 2; ``` 其中 angle 属性和 setRotation() 方法都以角度制表示角度,而 rotation 属性则以弧度制表示角度。 ### 回答2: Cocos Creator是一款主要用于游戏开发的跨平台游戏引擎,其内置的场景编辑器可以方便地创建并编辑角色、场景和动画。在使用Cocos Creator进行开发时,我们可以使用Node节点来表示游戏中的角色或物体,并通过设置节点的角度来实现旋转效果。 在Cocos Creator中,一个Node节点的角度可以通过设置节点的rotation属性来实现,rotation属性是一个表示角度的浮点数,默认以弧度为单位。可以通过以下代码设置一个节点的旋转角度: node.rotation = 45; // 将节点的旋转角度设置为45度 此外,我们还可以通过设置节点的rotation属性来实现平滑的角度变换。可以使用以下代码实现节点的平滑旋转效果: cc.tween(node).to(2, { rotation: 45 }).start(); // 在2秒内将节点从当前角度平滑地旋转到45度 在一些情况下,我们可能希望直接设置节点的旋转角度,而不是在原有角度基础上进行旋转。可以使用以下代码实现直接设置节点的旋转角度: node.rotation = node.rotation + 45; // 将节点的旋转角度增加45度 需要注意的是,节点的旋转角度是相对于父节点的,即如果改变了父节点的旋转角度,子节点的旋转角度也会受到影响。可以使用以下代码设置一个节点相对于父节点的旋转角度: node.eulerAngles = cc.v3(0, 0, 45); // 将节点相对于父节点的旋转角度设置为45度 总之,通过对Cocos Creator Node节点的角度设置,我们可以实现游戏中角色和物体的旋转效果,使游戏更加生动和有趣。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值