Hey Guys… Yo @Deltakosh … How can FLIP the Y -Axis of a Quaternion using the setting of the X,Y,Z,W values ???
I know i can convert the quaternion to euler then invert the Y -axis then convert back to quaternion… but that seems like a alot of work in the an update loop.
I cam trying to sync a physics world transform with the babylon mesh transform:
var tm = this.physicsBody.getWorldTransform(i);
var p = tm.getOrigin();
var q = tm.getRotation();
this.babylonMesh.position.set(p.x(), p.y(), p.z());
this.babylonMesh.rotationQuaternion.set(q.x(), q.y(), q.z(), q.w());
This actually updates my babylon mesh EXCEPT the quaternion is INVERTED on the y and my wheels are flipped around the wrong way… I need to FLIP that quaternion around… I thought i could just multiply the Z and W by -1.0 but that did not work…
I tried the code which should just flip the transform around… but it does not
Code is pretty simple… Each Frame…
Iterate wheel info… update the in internal wheel info… get the updated world transform and apply that position and rotation to the actual wheel meshes (TansformNode)… so the wheels can turn and bounces up and down with the suspension handling
protected after(): void {
if (this.wheelTransforms != null && this.wheelTransforms.length >= 4) {
let tm:any, p:any, q:any, i:number;
const n = this.vehicleController.getNumWheels();
for (i = 0; i < n; i++) {
this.vehicleController.updateWheelTransform(i, true);
tm = this.vehicleController.getWheelTransform(i);
p = tm.getOrigin();
q = tm.getRotation();
this.wheelTransforms[i].position.set(p.x(), p.y(), p.z());
this.wheelTransforms[i].rotationQuaternion.set(q.x(), q.y(), q.z(), q.w());
this.wheelTransforms[i].rotationQuaternion.multiplyInPlace(BABYLON.Quaternion.RotationYawPitchRoll(0, Math.PI, 0));
}
}
}
maybe transform them the direction you need initially before adding the physics at origin, and then bake them to make sure their origin axis is in the correct alignment?
Or do a secondary calculation that rotates them depending on their position offset from the parent that switches the Pitch value that you are doing?
Id go with the first one if its the physics engine giving you trouble.
Is from Bullet Physics API… Used in the addWheel method of the btRaycastVehicle class from Bullet Vehicle SDK
From the cpp examples the main difference for dealing with Z-UP vs Y-UP:
// By default, Bullet Vehicle uses Y as up axis.
// You can override the up axis, for example Z-axis up. Enable this define to see how to:
//#define FORCE_ZAXIS_UP 1
//
#ifdef FORCE_ZAXIS_UP
int rightIndex = 0;
int upIndex = 2;
int forwardIndex = 1;
btVector3 wheelDirectionCS0(0,0,-1);
btVector3 wheelAxleCS(1,0,0);
#else
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btVector3 wheelDirectionCS0(0,-1,0);
btVector3 wheelAxleCS(-1,0,0);
#endif
I got another one… How can i easily transform WORLD space position to local space position…
Using the same wheel config above… I have to set the wheelTransform.parent = null becuase the the positions coming from physics world WheelInfo is in WORLD space… How can i leave the wheel as a child of its parent and covert that world position to local position relative to the wheelTransform.parent…
var m = new BABYLON.Matrix();
transform.parent.getWorldMatrix().invertToRef(m);
var v = BABYLON.Vector3.TransformCoordinates(v, m);
transform.position.set(v.x, v.y, v.z);
But it messes up the quaternion rotation of the wheel if i transform the position coordinates…
Is there some kind of transform quaternion that i need to do on the rotation as well ???
Yo @Deltakosh … I am trying to turn the wheels of a car using my Physics Vehicle SDK (Babylon Toolkit Wrapper Around Ammo.btRaycastVehicle)
There is a function for each wheel: getWheelTransformWS this return the TRANSFORM in world space for the wheel. I then take the transform.getOrigin (the position) and transform.getRotation (the rotation)
and apply that to the wheels so they turn and bounce while driving.
This all works fine if the wheel has no parent . But if it has a parent, i have to set to null otherwise the wheel is in wrong position. I assume this is because the position and rotation are given in WORLD SPACE and since the wheel has a parent i would need to convert that world space to local space… I tried the inverse transform direction that didnt work…
How should i offset that world space… Subtract the parent position from the word space position or something like that… Can you show me calculation to do such a thing … Please
I got this to work and keep the wheel in the correct position relative to its parent:
var v = new BABYLON.Vector3(this.m_tempPosition.x(), this.m_tempPosition.y(), this.m_tempPosition.z());
var m = new BABYLON.Matrix();
transform.parent.getWorldMatrix().invertToRef(m);
var v = BABYLON.Vector3.TransformCoordinates(v, m);
transform.position.set(v.x, v.y, v.z);
But as soon as i add the rotation (Which comes from the WORLD SPACE transform)