A object-turning problem in script...
by Loler · in Torque Game Engine · 03/05/2007 (10:27 pm) · 1 replies
... I think something's amiss in turnyville. I'm trying to write a script to simply turn the player object towards another object, similarly to an AI turning towards the player. I am doing this as the first step to creating my own path following script.
Here is the code I am using.
//-----Rotate the control object towards the current node
%base = controlObject.getTransform();
%target = 1432.getTransform(); //nodestuff
//-----Normalize the vectors
vectorNormalize(%base);
vectorNormalize(%target);
//-----Get the angle between the vectors
%cosangle = vectorDot(%base,%target);
%turnangle = mAcos(%cosangle);
%matrix = matrixCreate(%base,%turnangle);
//-----Get the current %base position
%xpos = getWord(%matrix, 0);
%ypos = getWord(%matrix, 1);
%zpos = getWord(%matrix, 2);
//-----Set Rotation matrix
%xrot = getWord(%matrix, 3);
%yrot = getWord(%matrix, 4);
%zrot = getWord(%matrix, 5);
%angle = getWord(%matrix, 6);
//-----Apply the transformation
controlObject.setTransform(%xpos SPC %ypos SPC %zpos SPC %xrot SPC %yrot SPC %zrot SPC %angle);
//---------------------------------------------------------------------
If this seems completely off, it probably is. I have basically thrown it together to see if I could get any results, and I do believe I have, but not the intended ones.
I would really appreciate it if anyone could point out my errors, or tell me the correct way to go about this. I am aware this should be done in code, but I want to throw it together in script. I am also aware that the AI code has something similar, but I'd like to put it together myself in order to understand what is going on.
Thanks,
Loler
Here is the code I am using.
//-----Rotate the control object towards the current node
%base = controlObject.getTransform();
%target = 1432.getTransform(); //nodestuff
//-----Normalize the vectors
vectorNormalize(%base);
vectorNormalize(%target);
//-----Get the angle between the vectors
%cosangle = vectorDot(%base,%target);
%turnangle = mAcos(%cosangle);
%matrix = matrixCreate(%base,%turnangle);
//-----Get the current %base position
%xpos = getWord(%matrix, 0);
%ypos = getWord(%matrix, 1);
%zpos = getWord(%matrix, 2);
//-----Set Rotation matrix
%xrot = getWord(%matrix, 3);
%yrot = getWord(%matrix, 4);
%zrot = getWord(%matrix, 5);
%angle = getWord(%matrix, 6);
//-----Apply the transformation
controlObject.setTransform(%xpos SPC %ypos SPC %zpos SPC %xrot SPC %yrot SPC %zrot SPC %angle);
//---------------------------------------------------------------------
If this seems completely off, it probably is. I have basically thrown it together to see if I could get any results, and I do believe I have, but not the intended ones.
I would really appreciate it if anyone could point out my errors, or tell me the correct way to go about this. I am aware this should be done in code, but I want to throw it together in script. I am also aware that the AI code has something similar, but I'd like to put it together myself in order to understand what is going on.
Thanks,
Loler
Torque Owner Tim Hutcheson
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9480
I wrote the resource because I was having a lot of trouble with discontinuites in the rotation at certain cardinal values related to the euler angles and the problem of jitter etc, when stepping throught the turn.
My resource might be overkill. Hope it is helpful, this pops up occasionally on the forum and always catches my eye. :o)
To use it do something like this, where %bot is your player object and contains the id of your target, targetid. Completion is the name of a function you want executed when the rotation completes.
function turn(%bot, %completion) { %id = %bot.targetId; %t = %bot.getTransform(); %xp = getWord(%t, 0); %yp = getWord(%t, 1); %idTr = %id.getTransform(); %x = getWord(%idTr, 0); %y = getWord(%idTr, 1); %dx = %x-%xp; %dy = %y-%yp; %yaw = mAtan(%dx, %dy); rotateShape(%bot,%yaw,%completion); }