schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.63k stars 700 forks source link

How to change quaternion of a body with the use of key controls #490

Closed Fy-Spot closed 1 year ago

Fy-Spot commented 1 year ago

Hello Guys, my name is Banjo OLuwafiayokunola, I am 14 and new to Cannon.js I am trying to give onkey controls for my player(golem) in my game but the change of quaternion has been a big problem for me I look around the internet and i am now stuck and how can i put a debounce to my jumpscript please. Thank you

here is the code

function executeMovement() { if(keyMap[37] === true){ //rotate left angleY -= 0.002; rotationQuaternion.setFromEuler(new CANNON.Vec3(0,1,0), angleY); let quaternion = rotationQuaternion.mult(hitboxbody.quaternion); quaternion.normalize(); } if(keyMap[39] === true){ //rotate right rotationQuaternion.setFromAxisAngle( axisY, -rotateAngle ); hitboxbody.quaternion = rotationQuaternion.mult(hitboxbody.quaternion) } if(keyMap[38] === true){ //move front function walk(time) { time*=0.005 left_leg_pivot.rotation.x = (Math.sin(time))/2 right_hand_pivot.rotation.x = (Math.cos(90+time))/3 right_leg_pivot.rotation.x = (Math.cos(90+time))/3 left_hand_pivot.rotation.x = (Math.sin(time))/2

                    }

                    hitboxbody.velocity.x += Math.sin(hitboxbody.quaternion.y)
                    hitboxbody.velocity.z += Math.cos(hitboxbody.quaternion.y)

                    requestAnimationFrame(walk)

                }
                if(keyMap[40] === true){
                    //move back
                    function walk2(time) {
                        time*=0.005
                        left_leg_pivot.rotation.x = (Math.sin(time))/2
                        right_hand_pivot.rotation.x = (Math.cos(90+time))/3
                        right_leg_pivot.rotation.x = (Math.cos(90+time))/3
                        left_hand_pivot.rotation.x = (Math.sin(time))/2
                    }
                    hitboxbody.velocity.x -= Math.sin(hitboxbody.quaternion.y)
                    hitboxbody.velocity.z -= Math.cos(hitboxbody.quaternion.y)
                    requestAnimationFrame(walk2)
                }

                if(keyMap[32] === true){
                    //let isJumping = World_Gravity_Properties.isJumping
                    const jump = () => {

                        if (IsJumping) return{}
                        else{
                            if (hitboxbody.velocity.y <= 0.2 && hitboxbody.velocity.y >= 0)  {
                                IsJumping = false
                            }
                            let maxdist = 7.5
                            hitboxbody.velocity.y = maxdist
                            IsJumping = true

                        }

                    };
Fy-Spot commented 1 year ago

here what will explain the code better

set_onkey_controls(){ const{rh,lh,rl,ll,hd,body,left_hand_pivot,right_hand_pivot,right_leg_pivot,left_leg_pivot,playcam} = this const{golemMesh,Golem,hitbox,hitboxbody} = this

    let cubeshape = new CANNON.Box( new CANNON.Vec3( 1, 1, 1 ) );

    let clock = new THREE.Clock();
    let delta = clock.getDelta();

    let axisY =  new CANNON.Vec3( 0, 1, 0 );
    let rotationQuaternion = new CANNON.Quaternion();
    let rotateAngle = Math.PI / 3;
    let angleY = 0

    hitboxbody.addShape( cubeshape )