Touch Move Relative To Camera?

Discussion in 'How Can I...?' started by OceSetThisCannotBeChanged, Jan 23, 2020.

  1. OceSetThisCannotBeChanged

    OceSetThisCannotBeChanged Boxer

    Joined:
    Sep 6, 2018
    Messages:
    21
    Likes Received:
    2


    Hey guys,
    as you can see in this video my touch move only works correctly when the camera is in the default position. Do you think there is a way to make the touch move relative to the camera angle? My intention is to only rotate the camera around the y axis. Any ideas?
     

    Attached Files:

    Last edited: Jan 23, 2020
  2. Smart Penguins

    Smart Penguins Boxer

    Joined:
    Jan 2, 2020
    Messages:
    62
    Likes Received:
    70
    I'm not sure if there is a nocode way to do that. But you can add this code to line 96 of your "Touch Move" Node.

    Code:
        let yRotation = this.scene().find("cameraman")[0].rotation().y * Math.PI / 180;
        let tempX =dx * Math.cos(yRotation) + dy * Math.sin(yRotation);
        dy =-dx * Math.sin(yRotation) + dy * Math.cos(yRotation);
        dx = tempX;
    Code:
    //
    let enabled = false;
    let scrX, scrY;
    let start, prev;
    let phys;
    let isTouched = false;
    let forward, right;
    let delta = new Vec3(0, 0, 0);
    let sens;
    let maxSpeed;
    let followRotation;
    
    function init() {
        this.enableTouch();
            followRotation = this.attribute('Follow Rotation');
        smooth = this.attribute('Smooth');
        smooth = 1 - Math.max(0, Math.min(0.99, smooth));
        sens = this.attribute('Sensitivity');
    
        maxSpeed = this.attribute('Max Speed');
        if (maxSpeed == null) {
            maxSpeed = 999999;
        }
        if (maxSpeed == 0) {
            warning('Touch Move: Max Speed must be greater than 0. Changed to 0.01');
            maxSpeed = 0.01;
        }
    
        if (this.attribute('Invert X')) sens.x *= -1;
        if (this.attribute('Invert Y')) sens.y *= -1;
    
        scrX = Number(this.attribute('Screen X'));
        scrY = Number(this.attribute('Screen Y'));
        forward = new Vec3(
            scrY == 0 ? 1 : 0,
            scrY == 1 ? 1 : 0,
            scrY == 2 ? 1 : 0
        );
        right = new Vec3(
            scrX == 0 ? 1 : 0,
            scrX == 1 ? 1 : 0,
            scrX == 2 ? 1 : 0
        );
    
        phys = this.entity().physics();
        if (phys && phys.type() != 'kDynamic') {
            phys = null;
            sens = sens.scale(0.025);
        }
    }
    
    
    function update(dt) {
        if (!enabled) return;
        let dLen = delta.scale(smooth).length();
        if (dLen == 0) return;
        let fac = (smooth / dLen) * Math.min(dLen, maxSpeed);
    
        if (phys) {
            addLinVel(delta.scale(fac));
        } else {
            let ent = this.entity();
            let nPos = ent.position().add(delta.scale(fac));
            ent.setPosition(nPos.x, nPos.y, nPos.z);
        }
        delta = delta.scale(1 - fac);
    }
    
    function signal(name, value) {
        enabled = value;
    }
    
    function getVelFromScreenMove(dx, dy) {
        return forward.scale(dy)
            .add(right.scale(dx));
    }
    
    function addLinVel(vel) {
        setLinVel(vel.add(phys.linearVelocity()));
    }
    
    function setLinVel(vel) {
        phys.setLinearVelocity(vel.x, vel.y, vel.z);
    }
    
    component.touchBegan = function (point) {
        isTouched = true;
        start = point;
        prev = point;
    }
    
    component.touchMove = function (point) {
        if (!enabled) return;
    
        dx = sens.x * (point.x - prev.x);
        dy = sens.y * (point.y - prev.y);
        //// ----- Code Added -----
        let yRotation = this.scene().find("cameraman")[0].rotation().y * Math.PI / 180;
        let tempX =dx * Math.cos(yRotation) + dy * Math.sin(yRotation);
        dy =-dx * Math.sin(yRotation) + dy * Math.cos(yRotation);
        dx = tempX;
        //// ----- Code Added -----
        let vel = getVelFromScreenMove(dx, dy);
    
        if(followRotation) {
            let quat = this.entity().rotationQuat();
            let m = Mat4.createRotation(quat);
            let value = Mat4.transformVector( m, vel);
            delta = delta.add(value);
        }
        else {
            delta = delta.add(vel);
        }
       
        prev = point;
    }
    
    component.touchEnded = function () {
        isTouched = false;
    }
    
       
        
     
  3. OceSetThisCannotBeChanged

    OceSetThisCannotBeChanged Boxer

    Joined:
    Sep 6, 2018
    Messages:
    21
    Likes Received:
    2
    Works great, thank you!
     

Share This Page