|
Post by mikehense on Feb 21, 2015 2:16:23 GMT
One of the most useful gifts ever to the 3DRAD Community... a set of utility functions to make short work of the confusing world of quaternions, LocalToParent() being at the top of the list...
LocalToParent can be used to "attach" non physics objects to each other, creating child objects wherever required...
It can also be used to move non physics objects in any direction by specifying the object you want to move as the same in both the parent and the child object parameters...
Simply copy and paste into your Script and call the functions to see the magic...
/// --------- JESTERMON'S LOCALTOPARENT() AND ADDITIONAL UTILITYFUNCTIONS
///------------------------------------------------------------------------ /// /// LOCAL TO PARENT(PARENT, OBJ, rotation, translation) /// /// Rotates and offsets a child object to a parent object. /// The parent object may be child object to rotate or move against itself ///------------------------------------------------------------------------
void LocalToParent(int PARENT, int OBJ, Vector3 rotation, Vector3 translation) { Vector3 parentLocation; Quaternion parentOrientation; Quaternion newOrientation; Quaternion objectOrientation; Vector3 forward, location, newLocation; float x,y,z,ud,lr,fb; x = rotation.x; y = rotation.y; z = rotation.z; fb = translation.z; lr = translation.x; ud = translation.y; iObjectLocation(PARENT, parentLocation); iObjectOrientation(PARENT,parentOrientation); iObjectOrientationSet(OBJ,parentOrientation); iVectorRotate(forward,Vector3(lr,ud,fb),parentOrientation); newLocation = parentLocation + forward; iObjectLocationSet(OBJ,newLocation); iQuaternionFromEulerAngles(newOrientation,x,y,z,"xyz"); iQuaternionMultiply(newOrientation,newOrientation,parentOrientation); iObjectOrientationSet(OBJ,newOrientation); }
///------------------------------------------------------------------------ /// Returns a lookat orientation from vector a to vector b ///------------------------------------------------------------------------
Quaternion QuatGetLookAt(Vector3 a, Vector3 b){ Quaternion orientation; Vector3 up = Vector3(0,1,0); Vector3 direction = b - a; iQuaternionLookAt(orientation,direction,up); return orientation; }
///------------------------------------------------------------------------ /// Returns a direction vector from an orientation ///------------------------------------------------------------------------
Vector3 DirectionVector(Quaternion q) { Vector3 direction; iVectorRotate(direction,Vector3(0,0,1),q); return direction; }
///------------------------------------------------------------------------ /// Returns an orientation from a direction vector ///------------------------------------------------------------------------
Quaternion VectorOrientation(Vector3 v) { Quaternion orientation; Vector3 v1,v2; iVectorLengthSet(v1,v,1); iVectorLengthSet(v2,v,2); orientation = QuatGetLookAt(v1,v2); return orientation; }
///------------------------------------------------------------------------ /// Returns the multiplacation of vector with a scalor ///------------------------------------------------------------------------
Vector3 VectorTimesScalar(Vector3 v1, float value) { Vector3 v; v.x = v1.x * value; v.y = v1.y * value; v.z = v1.z * value; return v; }
--Mike
|
|
|
Post by NicusorN5 on Feb 21, 2015 8:14:11 GMT
I should move this topic,you posted in "Other Engines".
|
|
|
Post by Power Supersport on Feb 21, 2015 8:50:12 GMT
Is that equivallent to connecting skinmesh to object?
|
|
|
Post by mikehense on Feb 21, 2015 15:15:44 GMT
yes... LocalToParent() can be used to connect one skinmesh to another... the Parent object and the Child object can both be skinmeshes...
the first set of Vector3 params are is the rotation of the child object... the second set is the distance from the Parent center...
--Mike
|
|
|
Post by Power Supersport on Feb 21, 2015 15:17:43 GMT
Right... Thanks...
|
|
|
Post by F1Andre on Mar 10, 2015 18:20:40 GMT
So if I initialize a vector I say: int newVec = Vector3(1, 2, 0); what willva quaternion look like?
|
|
|
Post by mikehense on Mar 30, 2015 15:27:02 GMT
that would probably throw up an exception... and error... you're trying to stuff a Vector3 into a variable thatr is being declared as an integer...
that Vector3, if used in the first set of parameters in LocalToParent, would rotate the x by 1 (degree?), y by 2, and z by zero... if used in the second Vector3 param... it would move it over 1 (meter?) right, 2 up, and zero forward or backwards relative to the orientation and location of the "parent" object...
you can also use LocalToParent() to cause an object to move... simply use the same object as the "parent" and "child" and the Vector3 parameters will be used to either rotate or move the object... like adding a lil motor to the object...
for example... if you want to move a skinmesh that is OBJ_0 forward... just add this code...
LocalToParent(OBJ_0, OBJ_0, Vector3(0,0,0), Vector3(0,0,0.1));
you can substitute the numbers by variables so that the one could be a speed, that can change as conditions require...
--Mike
|
|
|
Post by F1Andre on Mar 30, 2015 15:54:41 GMT
Thanks, I am glad I figured it out before I started my AI car(more of a path following with a hell of alot of problems). That quaternion is still a bit tricky but I am getting the hang of it. Just a quick question cause it seems like there is no easy way to do it. How can you turn the text object or displayed text. For example the text must turn with a F1 wheel(cause the speed display is on the wheel)
|
|
|
Post by mikehense on Mar 30, 2015 16:25:56 GMT
text objects are 2D objects and cannot be rotated like 3D objects... you're going to have to come up with another solution...
one way to accomplish this is to use a flat skinmesh textured with numbers representing the speed... each skinmesh will be frame by frame animations (simply different textures on each) with numbers 0 to 9... you will need 3 meshes... (assuming your car can't go faster than 999 mph, or KPH)... aligned right next to each other...
you will need some scripted logic to determine which frame to show... you know... a car traveling at 160 mph, the first speed digit would be spd/100... the second would be spd minus spd/100, divided by 10... and the third would be the remainder...
before you even start doing the logic, just get 3 small flat skinmeshes, and LocalToParent() em to the wheel to verify it is what you want...
|
|
|
Post by F1Andre on Mar 30, 2015 16:46:04 GMT
That was my initial idea but with sprites. I just don't think it would work that fast, I know that 3d rad runs its scripts 75 times a second but when you need to display 100 sprites in 3 seconds would decrease the frame rate dramatically.
|
|
|
Post by mikehense on Mar 30, 2015 16:48:11 GMT
100 sprites???
|
|
|
Post by F1Andre on Mar 30, 2015 17:01:43 GMT
Sorry I didn't explain that well. You will have 9 sprites as you said, and you can create more as imposters and destroy them(or have three sprites lined up and just set it to the correct digit ex. 132 where each digit represents one of the 3 sprites that lined up). In any of the above mentioned cases you will have to set sprite frames 100 times in the first 3 seconds of acceleration. Since a F1 car accelerate s to 100 in less than 4 seconds.
|
|
|
Post by mikehense on Mar 30, 2015 17:10:23 GMT
ok... i was only talking about the speedometer digits you said that you wanted to put on the steering wheel and have em rotate when the wheel is turned...
|
|
|
Post by F1Andre on Mar 30, 2015 17:12:18 GMT
Oooh sorry.
|
|