How to rotate node?

:sunrise:
I’m trying to learn how to turn a hero, I didn’t think it was so difficult…
I thought it would be something like this angle = +1 when I looked at
Screenshot 2024-04-16 at 22.14.05

when I press the left arrow the available range is from 89 to -89 in angle. my hero looks in one direction and goes in the other direction.

i try it

this.rotateHero(+1); or this.rotateHero(-1); (left or right without moving forward) – key ArrowLeft or – key ArrowRight

rotateHero(angle: number) {
        const rot = new Quat();
        Quat.fromEuler(rot, 0, 0, angle + this.hero.HeroNode.angle);
        Quat.normalize(rot, rot);
        this.hero.HeroNode.setRotation(rot);
        // this.hero.HeroNode.setRotationFromEuler(new Vec3(360, 360, angle + this.hero.HeroNode.angle))
    }

move script (forward movement depending on the angle of rotation) – key ArrowUp

this.hero.HeroNode.setPosition(
                    this.hero.HeroNode.position.x + deltaTime * this.hero.currentSpeed * Math.cos(this.hero.HeroNode.angle * Math.PI / 180),
                    this.hero.HeroNode.position.y + deltaTime * this.hero.currentSpeed * Math.sin(this.hero.HeroNode.angle * Math.PI / 180), -1000
                );

please, help me.

I dot get it. Your hero like a car, it moving automatically, you want to use direction key to control it’s heading direction?

Yes, my hero is a car. the left and right keys should only change the rotate property so that the hero turns.

The up key is responsible for moving forward depending on the angle.

Is this OK?

import { _decorator, Component, EventKeyboard, Input, input, KeyCode, Node, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CarCtrl')
export class CarCtrl extends Component {

    public rotateSpeed = 60;
    public moveSpeed: number = 100;
    public moveBackSpeed: number = 60;
    public pressingMap: Map<KeyCode, boolean> = new Map();
    start() {
        input.on(Input.EventType.KEY_DOWN,(e: EventKeyboard)=>{
            this.pressingMap.set(e.keyCode, true);
        }, this);
        input.on(Input.EventType.KEY_UP,(e: EventKeyboard)=>{
            this.pressingMap.set(e.keyCode, false);
        },this);
    }

    public move(dt: number){
        let radian = this.node.angle * Math.PI / 180;
        let x = Math.cos(radian);
        let y = Math.sin(radian);
        let d = v3(x * this.moveSpeed * dt, y * this.moveSpeed * dt, 0);
        this.node.setPosition(Vec3.add(d, this.node.position, d))
    }

    public moveBack(dt: number){
        let radian = (this.node.angle + 180) * Math.PI / 180;
        let x = Math.cos(radian);
        let y = Math.sin(radian);
        let d = v3(x * this.moveBackSpeed * dt, y * this.moveBackSpeed * dt, 0);
        this.node.setPosition(Vec3.add(d, this.node.position, d))
    }

    public turnLeft(dt: number){
        this.node.angle += dt * this.rotateSpeed;
    }

    public turnRight(dt: number){
        this.node.angle -= dt * this.rotateSpeed;
    }

    public isKeyPressing(keyCode: KeyCode){
        return this.pressingMap.get(keyCode);
    }

    protected update(dt: number): void {
        if(this.isKeyPressing(KeyCode.KEY_W)){
            this.move(dt);
        }

        if(this.isKeyPressing(KeyCode.KEY_A)){
            this.turnLeft(dt);
        }

        if(this.isKeyPressing(KeyCode.KEY_D)){
            this.turnRight(dt);
        }

        if(this.isKeyPressing(KeyCode.KEY_S)){
            this.moveBack(dt);
        }
    }
}

2 Likes
  1. initially the hero’s angle is set to 0
  2. but when I start moving the hero, I expect the hero to move at an angle of 0 degrees.
  3. but there is movement along an angle of 90 degrees.
    I solved this problem when I saw this code

then I decided to try this*- in the code and it helped solve the problem from points 1,2,3.
*- let radian = (this.hero.HeroNode.angle +90) * Math.PI / 180; I don’t know why this is so, I probably need to look for an error in the code… but it seems like I haven’t changed the angle anywhere before.

and for this* - thank you. I spent a lot of time yesterday looking for a solution, and the solution turned out to be very simple.
*- this.hero.HeroNode.angle -= deltaTime * (this.hero.currentSpeed/2);

I am glad to hear that.

It seems like that if you set rotation of your hero as 0. It looks like this.

If it is. As you can see the direction of tank is not same as the direction of node. So you need +90 to make tank’s direction same as node.

I did not change angle is becasue. My initially set of tank is like this.

2 Likes