Rotation question

Hey forum, I am building a prototype for a client, and in it I am having a car zip around a track, Because the client does not want the car to crash into anything, we are controlling the movements with a .fbx from Blender. I have built a project that takes the data from each frame of the animation and then binds that to an array that I can then apply to a model in my player. As I am trying to make the car as realistic as possible I have the wheels responding to the change in vector of each frame of the car’s movement. I have then contained with a parent entity to that I can rotate them around the axle as well as turn them in relation to the car’s movements. I have the chassis of the car contained in a parent entity as well so that I can apply intertia to the car as it turns.

All the parts of the car are contained in a parent entity that has the animation player component attached that moves this parent around the track.

Wha I am running into, is that I am trying to rotate the chassis based on the inertia of the turn. I am using rotateLocal(0,difference,0). The translation from Blender requires that the .dae model be rotated in the parent container for the chassis by 90 degrees to fit the axis change that occurs going from Blender to PC.
What I would like to do is set the model to a specific rotation that is the difference of change each frame off of 0, but what it is doing is actually rotating by rather than rotating to. Has anyone run into this before and if so could you please offer some advice?

 var change = Math.atan2(this.lastPos[2] - newPos[2],this.lastPos[0] - newPos[0]) * 180 / Math.PI;
        
        var directedAngle = (Math.atan2(this.lastPos[2], this.lastPos[0]) - Math.atan2(newPos[2], newPos[0])) * 180 / Math.PI;
        
        
        if(directedAngle < 0 ){
            console.log("turn right");       
        }
        
        if(directedAngle > 0 ){
            console.log("turn left");       
        }

        this.lastPos = [newPos[0],newPos[1],newPos[2]];
        this.wheel1.rotateLocal(0,-directedAngle,0);
        this.wheel2.rotateLocal(0,-directedAngle,0);
        
        this.bodyContainer.rotateLocal(0,-directedAngle,0);

rotateLocal is ‘rotate by’ as in it rotates the entity by the euler angles passed. It sounds like you want setRotation or setRotationLocal?

Brilliant, thanks! I wish that the documentation was a little more clear on that.

A quaterion question for you. If you don’t supply the correct “w” parameter it distorts the object. Is there a way to create a quaterion that will not distort if you don’t know the “w”?

Thanks.

What are you creating a quaternion from (e.g euler angles? angle around an axis etc?)

There’s a few setFrom* functions in pc.Quat that you could use: https://developer.playcanvas.com/en/api/pc.Quat.html

This whole set of mathematics I find very confusing. I have been searching for usable examples that can give some practical examples of Quats as they relate to PC. I am creating a prototype for a project for a client as I mentioned above. What I am running into now is that I have been called on to have a track that does a loop-de-loop on something is occuring with the Quaterions I am recording when my animation hits the apex of the loop. Everything suddenly flips until it reaches the base. What I am trying to understand is the amount of change between each frame of my animation, particularly as it relates to the y axis. I am using the turn along the y to adjust the turn of the tires, and the bank of the car due to inertia. I had found a group of extentions to Quat on here, Useful pc.Quat functions and was using var vector = new pc.Vec3().copy(enemy.getPosition()).sub(this.entity.getPosition()).normalize(); var requiredRotation = new pc.Quat().fromToRotation(this.entity.forward, vector) to track the difference. I don’t know why this works, but through hours of hacking at this I was able to get values that seemed to work. I have read the wikipedia article on Quaterions, and TBH it makes no sense to me. Do you know of any practical examples that can teach quaterions so I could stop posting all these questions about rotation?

A quaternion is a rotation around an axis.

The fromToRotation gets the angle for going from one look direction to another.

Try this article: https://developerblog.myo.com/quaternions/

1 Like